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

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.

Related

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

Server on virtualmin keeps redirecting to wrong website com WP multisite

When I enabled MultiSite Wordpress, it redirected to one of the Virualmin sites
I tried to put it as a subdomain, I registered it as DNS.
As a subdirectory I tried to include it in the NGINX settings.
######### subdirectory #######
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location /layout-1/ {
index index.php;
try_files $uri $uri/ /layout-1/index.php?q=$uri&$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
########## use this or this depends on the configuration ######
1 - ### fastcgi_pass unix:/run/php/php7.3-fpm.sock;
2 - ### fastcgi_pass localhost:8009;
########################################################
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_read_timeout 3000;
;
Should the BIND have an external or internal IP in the domain? I use only one IP for all servers, and in BIND all domains are with external IP. (The question is whether it should be external or internal IP).
Would NGINX have any configuration? How to remove the IP and just put (listen IP: 80) instead of (listen 288.218.198.981:80)
But which configuration would work in general? So you can always avoid complex edits ... For example ... After creating an internal subdomain ...
You have to use all your bind directives in nginx in the same manner. You can use the bind directive in nginx in 2 ways:
listen server_ip:80;
listen 80;
The ideea is that you have to use it one way or another everywhere in your nginx vhosts. If you mix them (one nginx vhost uses listen ip:80; and other listen 80;), it will result in some domains not redirecting or loading properly.
If your server has a private ip then use the private ip. If the server has public ip then use the public ip. You just have to decide which syntax you're going to use.
If you have multiple ip addresses assigned to your server I would recommend you to use the listen ip:80; method

setup vhost with nginx on mamp free version

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

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.

My nginx + fastcgi configuration downloads php files instead of executing them

I'm using this configuration on a fresh install of php5-fpm and nginx on ubuntu 13.04:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.php index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ /index.html;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
error_page 404 /404.html;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
But, my web browser is seeing php as text instead of the executed results. Where should I look to troubleshoot?
Your php code is being displayed directly because it's not being sent to the php engine, that means the location block is being matched and the php file is being served, but the php file isn't being captured by the php block, so your problem is in the php block.
In that block you have 2 fastcgi_pass, one with a port (9000) and the other to a unix socket, you can't have both together, but since you've tagged your question with fastcgi so I'll assume you are using fastcgi, try commenting this line
#fastcgi_pass unix:/var/run/php5-fpm.sock;
It sounds like you are getting the wrong Content-Type header set. You can check this with various tools. For example, open the Developer Tools "Network" tab in Chrome, and then request the page. You'll see the "Content Type" returned in one of the columns, and you can click on the request in the left column to see the full Response headers. I suspect you'll find the header being returned is either "text/plain" or "application/octet-stream" instead of text/html, which is probably want you want.
Nginx usually sets a default Content-Type header based on the extension. This is done with the types directive, which I don't see mentioned above, so you may wish to check your settings there to confirm that the php extension is mapped to text/html. Explicitly setting a Content-Typeheader in your application may also help.
I was able to fix this by updating my nginx vhost by changing
default_type application/octet-stream;
to
default_type text/html;

Resources