setup vhost with nginx on mamp free version - nginx

I'd like to setup vhosts on mamp, while usin nginx instead of the default apache server.
I browsed stack overflow and the web, but all I could find was either about pure nginx (speaking about the sites-available folder which isn't created by default on mamp) or about vhost with apache on mamp or about vhosts with nginx on mamp PRO.
I did manage to switch to nginx, but all I can see now are 403 errors (and when I go back to apache server, all my vhosts are working). I did add the lines in my hosts file, but I can't manage to get the vhosts working. here is my MAMP/conf/nginx/nginx.conf
http {
...
server {
...
location / {
index index.html index.php;
}
location my-vhost-name.com {
root /Users/myName/Document/projectParentFolder/projectFolder;
index index.html index.php;
}
}
}
And when I go to my-vhost-name.com, I have the 403 error message from nginx.
Thank you for your help.

In the MAMP/conf/nginx directory, I created a sites-enabled folder for configs for individual sites.
I added a config file for the site example.com in MAMP/conf/nginx/sites-enabled
I added config vars to MAMP/conf/nginx/sites-enabled/example.com:
server {
charset utf-8;
client_max_body_size 128M;
sendfile off;
listen 80;
server_name example.com;
root /Applications/MAMP/htdocs/example.com/;
index index.php;
location / {
# Redirect everything that isn't a real file to index.php
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass
unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI.sock;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
}
}
At the end of the config main file (MAMP/conf/nginx/nginx.conf), I connect all the configs from the sites-enabled folder:
include sites-enabled/*;
}
Restart MAMP

Related

I add nginx config of domain but my domain is not going online

I want to add a domain manually with cli of centos 7 and created a config file in sites-available directory:
server {
listen 80;
server_name hustana.ir www.hustana.ir;
location / {
root /home/www/public_html;
index index.html index.htm;
try_files $uri $uri/ =404;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
In my server there is another domains that are online and works fine.
if its help I should tell that at start my server was worked by Plesk Obtisian control panel
but now I wanna add another domain to server just but it`s not working.
I changed my domain NS from domain control center.
additional description:
I guess it`s good to say that if I delete the config block of one of my older domains , domain still stay online but it displays just a blank white page.
I tried add to include this config file from another directory directly into nginx.conf:
server {
listen 80;
server_name hustana.ir;
root /home/www/public_html;
index index.php;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
You need to enable your site as mentioned here.
Check the A-record of your domain. It must point to your server IP.

NGINX + Passenger w/ Rails + WordPress permalinks

The environment is as follows:
I have https://website.com and a blog at https://website.com/blog
The root path points to a Passenger-hosted Rails app, and the blog subdirectory points to a WordPress app via php-fpm
Everything works fine with my Nginx config, but when I try to change the permalink structure to anything other than "Plain", I get a 404 page from the Rails app as if the location blocks aren't utilized. I tried looking at the error log in debug mode, and I do see it attempting to try_files, but ultimately it fails with the Rails 404 page.
It may be worth noting that the entire site is behind Cloudflare. Not sure if it could be something with that, though I kind of doubt it.
Here is the almost-working Nginx config I'm using:
server {
listen 80 default_server;
server_name IP_ADDRESS;
passenger_enabled on;
passenger_app_env production;
passenger_ruby /home/ubuntu/.rbenv/shims/ruby;
root /web/rails/public;
client_max_body_size 20M;
location ^~ /blog {
passenger_enabled off;
alias /web/blog;
index index.php index.htm index.html;
# Tried the commented line below, but then nothing works.
# try_files $uri $uri/ /blog/index.php?$args;
# The line below works, but peramlinks don't.
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
# Tried the commented line below, but then nothing works
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# The line below works, but peramlinks don't.
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
}
I wanted to comment in short but I don't have enough reputation for that.
I used the following block and worked for me. I added an add_header directive just to debug that if my request is reaching the correct block.
location ^~ /blog {
try_files $uri $uri/ /index.php?$args;
add_header reached blog;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass php;
}
}
If your server is behind CloudFlare, you can try with /etc/hosts entry on your local machine if you're using Ubuntu/Mac. Which will stop the DNS lookup and site will directly be accessed from the IP address.
Check if any redirects are happening due to any other Nginx configuration.
Also, you have mentioned in the question that site is https:// while your server block has only listen 80 meaning non HTTPS.
Check for the response headers with
curl -XGET -IL site-name.tld
which may help you more debugging the situation.
Difference between alias and root directives https://stackoverflow.com/a/10647080/12257950

Why is my second site in nginx.conf not working?

I have a local NginX testing server on my Windows 10 machine. This is just for creating and testing websites, it is not served to the internet.
I've been testing one site successfully at localhost for a while, but now I want to add a second test site. I thought I could achieve this by duplicating the server{} block in the nginx.conf file and changing the name of the server_name and a few other parameters, but that it doesn't seem to work. When I try to load my second test site in Chrome, I get this error:
This site can’t be reached
local_test_2’s server DNS address could not be found.
My site at localhost still works, though.
Why is my second test site not working?
Here's my current nginx.conf file:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type text/html;
sendfile on;
keepalive_timeout 65;
server {
#Server basics
server_name localhost;
listen 80;
index index.html index.php;
root c:/nginx/html;
location / {
try_files $uri $uri/ /index.php?_url=$uri&$query_string;
}
location ~ .(php|htm|html)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME c:/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
}
server {
#Server basics
server_name local_test_2;
listen 80;
index index.html index.php;
root "C:\Users\User Name\Documents\Test\example.com";
location / {
try_files $uri $uri/ /index.php?_url=$uri&$query_string;
}
location ~ .(php|htm|html)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME c:/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
}
}
Update:
My C:\Windows\System32\drivers\etc\hosts file has the following:
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
The current 'localhost' specification is commented out. Should I change this file?
You need to add local_test_2 in your windows host file: at
C:\Windows\System32\drivers\etc\hosts
In host file add below line at the last
127.0.0.1 local_test_2
Also you can check reference to setup new host in nginx at: Setting up Nginx on local machine
The local_test_2 is a url that you created for testing purpose. Since you didn't buy it from some registrar, no DNS provider will be able to resolve the url to the ip address.
Every operating system has a hosts file(in linux it will be /etc/hosts) which can be used to map the urls to ip address without the use of some online DNS service. So in your case you can append the following line,
127.0.0.1 local_test_2
which tells to route all requests to local_test_2 to the same machine(127.0.0.1). No other changes are required in the hosts file.
Refer this link for more details on hosts files and different files used in different operating systems.

Hosts file and nginx redirect from http to https on development server

I am developing a website, and I just installed ssl on the production website (I have never done this before). When I load the development website the page redirects to https and breaks because https isn't installed on the development site.
Development url: http://local.ezel.io
Production url: https://ezel.io
The Nginx (production):
server{
listen 80;
server_name ezel.io;
root /var/www/ezel.io/public;
location ~ /.well-known {
allow all;
}
rewrite ^ https://$server_name$request_uri? permanent;
}
The Nginx (development):
server {
listen 80;
server_name local.ezel.io;
root /home/ryan/Documents/www/ezel.io/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
On my development machine, I also have the following in my hosts file:
127.0.0.1 local.ezel.io
What would be causing me to go from http://local.ezel.io to https://local.ezel.io?
I think the problem is that you enabled https://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security once and now your browser insists on trying HTTPS.
Try this: http://classically.me/blogs/how-clear-hsts-settings-major-browsers
Also, try pinging local.ezel.io to ensure it's really your localhost and not actually ezel.io.

NGINX and Clickonce

I am trying to get my NGINX website to work with Microsoft clickonce. I have the web site able to display the standard microsoft clickonce install page, but when I get to the Install page on my website and click the install button, nothing happens. I believe at that point it is supposed to download an EXE, which it doesn't do. Does anyone have any experience in getting clickonce to work with NGINX and what is required? Any information about setting correct directives, location, configuration, mime types or whatever else may be required.
Here is my current config file. I am new to NGINX, so perhaps it is something I am doing wrong with the location, or...?
server {
listen 80;
root /usr/share/nginx/www/MyWebSite;
index index.php index.html index.htm;
server_name MyWebSite.com www.MyWebSite.com;
client_max_body_size 100M;
access_log /var/log/nginx/MyWebSite.access.log;
error_log /var/log/nginx/MyWebSite.error.log;
location / {
try_files $uri $uri/ /index.php?$args;
}
location /Application/MyApp/ {
try_files /Application/MyApp/publish.htm /Application/MyApp/setup.exe /application/MyApp/MyApp.application;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
types {
application/x-ms-application .application;
}
}

Resources