How to Fix: Nginx Conflicting Server Name

Renomu Reza asked:

I have three nginx server blocks:

One: /etc/nginx/sites-available/default

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        #try_files $uri $uri/ =404;
        try_files $uri $uri/ /index.php$is_args$args;
    }

        location = /favicon.ico { log_not_found off; access_log off; }
        location = /robots.txt { log_not_found off; access_log off; allow all; }
        location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
        expires max;
        log_not_found off;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

        location ~ /\.ht {
                deny all;
        }
}

Two: /etc/nginx/sites-available/example1.com

server {
        listen 80;
        listen [::]:80;

        root /var/www/example1.com/html;
        index index.php index.html index.htm index.nginx-debian.html;

        server_name example1.com www.example1.com;

        location / {
                #try_files $uri $uri/ =404;
                try_files $uri $uri/ /index.php$is_args$args;

        }

        location = /favicon.ico { log_not_found off; access_log off; }
        location = /robots.txt { log_not_found off; access_log off; allow all; }
        location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
        expires max;
        log_not_found off;
        }

        location ~ \.php$ {
 include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

        location ~ /\.ht {
                deny all;
        }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example1.com/fullchain.pem; # managed$
    ssl_certificate_key /etc/letsencrypt/live/example1.com/privkey.pem; # manag$
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

Three: /etc/nginx/sites-available/example2.com

server {
        listen 80;
        listen [::]:80;

        root /var/www/example2.com/html;
        index index.php index.html index.htm index.nginx-debian.html;

        server_name example2.com www.example2.com;

        location / {
                #try_files $uri $uri/ =404;
                try_files $uri $uri/ /index.php$is_args$args;

        }

        location = /favicon.ico { log_not_found off; access_log off; }
        location = /robots.txt { log_not_found off; access_log off; allow all; }
        location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
        expires max;
        log_not_found off;
        }

        location ~ \.php$ {
 include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

        location ~ /\.ht {
                deny all;
        }

}

When i run: $ sudo nginx -t

This answer appears:

nginx: [warn] conflicting server name "example1.com" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "www.example1.com" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "example1.com" on [::]:80, ignored
nginx: [warn] conflicting server name "www.example1.com" on [::]:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

How do you fix it?

My answer:


Look at the complete file /etc/nginx/sites-enabled/example1.com. For some reason you only posted part of the file. But you can see the complete file in the nginx -T output.

When you ran certbot it added a second server block to the bottom of the file for serving port 80 and redirecting to HTTPS. It added HTTPS and port 443 to the original server block. But it didn’t remove the port 80 listener from it. You can fix this yourself by removing the lines which make your new HTTPS server block listen on port 80.


View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.