Multiple domains on one server points to wrong sites

23,604

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

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 ....

Share:
23,604
Staffan Estberg
Author by

Staffan Estberg

Creative developer and filmmaker based in Bangkok.

Updated on February 21, 2020

Comments

  • Staffan Estberg
    Staffan Estberg over 4 years

    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.

  • Adriano Monecchi
    Adriano Monecchi about 10 years
    Thanks @JClarke. This solved the issue for me. I just commented "include /etc/nginx/conf.d/*.conf;" line on my nginx.conf file and the error message was gone!