Multiple domains on one server points to wrong sites - nginx

Using Nginx, I've created a multiple domain setup for one server consisting of four individual sites. When I start Nginx I get an error message and the sites seem to get mixed up as typing in one url leads to one of the other sites.
The error message displayed -
Restarting nginx: nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
nginx.
I've set up all four domains in a similar manner in their respective file under /sites-available -
server {
listen 80;
root /var/www/example.com/public_html;
index index.php index.html index.htm;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ /index.html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I've checked and there is no default file in /sites-enabled. Guessing there might be a faulty setting in the Nginx main config but not sure as to what to look for.

Your nginx.conf loads its external server files from the path you have in your include directives.
If you have a file in include /etc/nginx/conf.d/*.conf; and its symlinked to include /etc/nginx/sites-enabled it's going to load the file twice which would cause that error.

I was having the same problem with my Ubuntu/nginx/gunicorn/django 1.9 sites on my local machine. I had two nginx files in my /etc/nginx/sites-enabled. Removing either one allowed to remaining site to work. Putting both files in ended up always going to one of the two sites. I'm not sure how it chose.
So after looking at several stack overflow questions without finding a solution I went here: http://nginx.org/en/docs/http/request_processing.html
It ends up that you can have multiple servers in one sites-enabled file, so I changed to this:
server {
listen 80;
server_name joelgoldstick.com.local;
error_log /var/log/nginx/joelgoldstick.com.error.log debug;
location / {
proxy_pass http://127.0.0.1:8002;
}
location /static/ {
autoindex on;
alias /home/jcg/code/python/venvs/jg18/blog/collect_static/;
}
}
server {
listen 80;
server_name cc-baseballstats.info.local;
error_log /var/log/nginx/baseballstats.info.error.log debug;
location / {
proxy_pass http://127.0.0.1:8001;
}
location /static/ {
autoindex on;
alias /home/jcg/code/python/venvs/baseball/baseball_stats/collect_static/;
}
}
I can now access both of my sites locally

Check out the /etc/nginx/sites-enabled/ directory if there is any temp file such as ~default. Delete it and problem solved.
Credit: #OmarIthawi nginx error "conflicting server name" ignored

In my case no sites-enabled nor double includes ....
The solution was avoiding more than one reference (if you consider all of the conf.d files as a whole) to "listen 80" and "server_name" references ...
In my case, default.conf and kibana.conf both included references to this guys ... I commented the one in default and problem solved !
My 2 cents ....

Related

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

routing subdomain to subdirectory on nginx

Please help as explicitly as possible. I have set up a domain on a home server running nginx on Ubuntu 15, and I have the dns pointed to it. I can use the domain to access the site and if I append /subdirectory to it, I am able to launch the pages inside the subdirectories. What I am trying to do is get the subdomains to go directly to the correct root. Ie: mysite.com = /index.htm, subdomain.mysite.com = ./subdirectory where files are located.
I have tried every suggestion including those popular and those criticized, and either I get an error restarting Nginx or I get a "server not found" error. I´ve tried setting up cname aliases with my DNS server, and that doesn´t work either.
The working config file is below:
##
server {
server_name "~^(?<sub>.+)\.domain\.tld$";
index index.php index.html index.htm;
root //media/user/ednet/$sub;
ssl_certificate REMOVED FOR SECURITY
ssl_certificate_key REMOVED FOR SECURITYY
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!Anull:!md5;
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
# root //media/user/ednet;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm;
#=========================Locations=============================
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
# root /usr/share/nginx/html;
}
#============================================================PHP=========================================================
location ~ \.php$ {
try_files $uri =404;
# fastcgi_split_path_info ^(.+\.php) (/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
====================
I´ve tried proxies, redirects, and everything else I can think of--including reading the instructions in the Nginx manual and the wiki.
Thanks
A quick "server not found" by the browser suggests that DNS is not set up. Your subdomains need to resolve to your server's public IP address using either:
specific CNAME record pointing to the main domain specific A record
pointing to the server's IP address wild (catch-all) A record
pointing to the server's IP address
Changes to DNS can take many hours to propagate.
An error restarting nginx suggests a syntax error in the configuration file.
Your configuration file contains no obvious syntax errors, but there are a few strange elements:
root directive appears twice
index directive appears twice
root directive has two leading // where usually one will do
You have IPv4 and IPv6 configured which is fine if you have an IPv6 connection.
You have this server marked as a default_server which means that it will be used even if the server_name regex does not match. Therefore, if you present to the server with a subdomain that returns files from the root, this implies that the regex failed to match but the server block was used by default. In which case, check the regex.
In summary, the first priority is to fix the DNS. Nothing will work unless the subdomains resolve to the IP address of the server.

nginx server sometime returns 404 Not Found for valid a URL

I am using a nginx server with PHP-FPM for web development. While sending continuous AJAX calls from the browser to the server, I sometimes get 404 Not Found errors for a valid URL. (When I open the URL in a new browser tab, it is showing the page properly.)
I am unable to debug why nginx is behaving like this. I don't know if it is dropping connections. What should I do?
I am using default installation of nginx and have not made any changes to it.
This is my nginx.conf
server {
listen IP address with PORT ssl;
server_name SERVER Name;
root /u01/projectfolder;
ssl on;
ssl_certificate /etc/nginx/ssl/36287365.net.cert;
ssl_certificate_key /etc/nginx/ssl/36287365.net.key;
index index.php index.html;
log_not_found off;
charset utf-8;
location /rainbow {
try_files $uri $uri/ /rainbow/index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9101;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I have also added the log at php side index.php but for 404 requests logs are not generated. I guess the request is not reached to php way fastcgi and nginx.
Please help
This may happen if you have more than one server listening on the same port, make sure only one PHP server listens on 9101.
I've experienced this with two different Node.js servers running simultaneously, listening to the same port - about 50% of the requests returned HTTP error 404 while the rest did OK 200.
You can verify that it's not Nginx's fault by generating the same request using CURL directly on the machine, bypassing Nginx. If this still occurs, it's not their fault.

403 forbidden on wordpress index with nginx, the rest of the pages work fine

I'm setting up my blog on a new EC2 instance because one of the sites on the server that's currently hosting it is being DDoSed.
I'm having some trouble with nginx, because I can either see all the pages fine but 403 on the index, or see the index but 404 on the pages (depending on the config I'm using)
Here's my nginx config:
server {
listen 80;
server_name www.test.com;
server_name test.com;
root /www/blog;
include conf.d/wordpress/simple.conf;
}
And simple.conf:
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
if I change the try_files $uri $uri/ /index.php?$args; to index index.php, the front page will work fine and the rest will be 404. If I leave it like that, the front page is 403.
Here's the error log:
2013/08/07 19:19:41 [error] 25333#0: *1 directory index of "/www/blog/" is forbidden, client: 64.129.X.X, server: test.com, request: "GET / HTTP/1.1", host: "www.test.com"
That directory is 755 on the nginx user:
drwxr-xr-x 6 nginx nginx 4096 Aug 7 18:42 blog
Is there anything obvious I'm doing wrong ?
Thanks !
Add index index.php; In the server block, if it doesn't work then you need to remove the $uri/ because you don't want to do a autoindex on
EDIT: Just noticed that you already figured out your problem, so I'll add the reasoning behind it, the reason why you needed autoindex on; is because without it nginx will follow the try_files rules,
Check if there's a file called /, and of course it fails.
Check if there's a directory called / (by adding root it would = /www/blog/), this check will succeed, so it tries to list the content of the folder.
Since you didn't specify autoindex on; so by default nginx should forbid directory listing, thus it would return a 403 forbidden error.
The rest of the site works fine because it fails the $uri/ test or doesn't reach it, because you probably don't have a folder called image.jpg or stylesheet.css etc.
Looks like I needed the inded index.php in the server {} definition and not in the location {}
It seems that you are not allowing arguments to be sent to the CMS so this will not show this uris that would bring information from the database and redirect you to the 403 page.

nginx - two subdomain configuration

I'm new to Nginx and I'm trying to get subdomains working.
What I would like to do is take my domain (let's call it example.com) and add:
sub1.example.com,
sub2.example.com, and also have
www.example.com available.
I know how to do this with Apache, but Nginx is being a real head scratcher.
I'm running Debian 6.
My current /etc/nginx/sites-enabled/example.com:
server {
server_name www.example.com example.com;
access_log /srv/www/www.example.com/logs/access.log;
error_log /srv/www/www.example.com/logs/error.log;
root /srv/www/www.example.com/public_html;
location / {
index index.html index.htm;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name;
}
}
It is working to serve example.com and www.example.com.
I have tried to add a second server block in the same file like:
server {
server_name www.example.com example.com;
access_log /srv/www/www.example.com/logs/access.log;
error_log /srv/www/www.example.com/logs/error.log;
root /srv/www/www.example.com/public_html;
server {
server_name sub1.example.com;
access_log /srv/www/example.com/logs/sub1-access.log;
error_log /srv/www/example.com/logs/sub1-error.log;
root /srv/www/example.com/sub1;
}
location / {
index index.html index.htm;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name;
}
}
No luck. Any ideas? I'd super appreciate any feedback.
The mistake is putting a server block inside a server block, you should close the main server block then open a new one for the sub domains
server {
server_name example.com;
# the rest of the config
}
server {
server_name sub1.example.com;
# sub1 config
}
server {
server_name sub2.example.com;
# sub2 config
}
You just need to add the following line in place of your server_name
server_name xyz.com *.xyz.com;
And restart Nginx. That's it.
Add A field for each in DNS provider with sub1.example.com and sub2.example.com
Set the servers. Keep example.com at last
As below
server {
server_name sub1.example.com;
# sub1 config
}
server {
server_name sub2.example.com;
# sub2 config
}
server {
server_name example.com;
# the rest of the config
}
Restart Nginx
sudo systemctrl restart nginx
You'll have to create another nginx config file with a serverblock for your subdomain. Like so:
/etc/nginx/sites-enabled/subdomain.example.com
There is a very customizable solution, depending on your server implementation:
Two (or more) SUBdomains in a single nginx "sites" file? Good if you own a wildcard TLS certificate, thus want to maintain ONE nginx config file. All using same services BUT different ports? (Think of different app versions running concurrently, each listening locally to differents ports)
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name ~^(?<sub>.+).example.com;
# default app (the "default" ports, good for the "old" app)
set $app 19069;
set $app-chat 19072;
# new app
# new.example.com
if ( $sub = "new" ) {
set $app 18069;
set $app-chat 18072;
}
# upstreaming
location / {
proxy_redirect off;
proxy_pass http://127.0.0.1:$app;
}
location /longpolling {
proxy_pass http://127.0.0.1:$app-chat;
}
I know the performance will "suck", but then again, since the decision was to go with one server for all it's like complaining that an econobox cannot haul as much people as a bus because the little car has a "heavy" roof rack on top of it.
A regex expert could potentially improve the performance for such a custom solution, specially since it could ommit the CPU expensive "if" conditional statements.
Maybe this could help someone having a challenge with this, this got me grinding the whole day.
First, if you have SSL installed, remove it (delete better still), this would help reset the previous configuration that's disrupting the subdomain configuration.
in etc/nginx/.../default file
create a new server block
server {
listen 80;
server_name subdomain.domain.com;
location /{
proxy_pass http://localhost:$port;
}
}

Resources