如何修复:Nginx 冲突的服务器名称

如何修复:Nginx 冲突的服务器名称

我有三个 nginx 服务器块:

一:/etc/nginx/站点可用/默认

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;
        }
}

二:/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

}

三:/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;
        }

}

当我运行:$ sudo nginx -t

出现此答案:

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

您如何修复它?

答案1

查看完整文件/etc/nginx/sites-enabled/example1.com。由于某种原因,您只发布了文件的一部分。但您可以在输出中看到完整文件nginx -T

当您运行 certbot 时,它会在文件底部添加第二个server块,用于服务端口 80 并重定向到 HTTPS。它会将 HTTPS 和端口 443 添加到原始服务器块中。但它并没有从中删除端口 80 侦听器。您可以通过删除使新 HTTPS 服务器块侦听端口 80 的行来自行修复此问题。

相关内容