Nginx 服务器块配置中的服务器名称冲突

Nginx 服务器块配置中的服务器名称冲突

获取 certbot 证书后,执行 nginx -t 时出现以下错误:

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

这是我的服务器块配置:

server {

    server_name naos-soultrap.online www.naos-soultrap.online;

    root /home/pierre/public/naossoultrap;

    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

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


}
server {
    if ($host = www.naos-soultrap.online) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = naos-soultrap.online) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


     listen 80;
     listen [::]:80;
     server_name naos-soultrap.online www.naos-soultrap.online;
     return 404; # managed by Certbot
}
server {
    if ($host = naos-soultrap.online) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;

    server_name naos-soultrap.online www.naos-soultrap.online;
    return 404; # managed by Certbot


}

答案1

您的警告实际上告诉您配置出了什么问题:

nginx: [warn] conflicting server name "naos-soultrap.online" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "www.naos-soultrap.online" on 0.0.0.0:80, ignored 

您的两个服务器配置(总共有三个)具有冲突的名称。

第一个服务器块用于 https/443:

server {

    server_name naos-soultrap.online www.naos-soultrap.online;
    root /home/pierre/public/naossoultrap;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    listen [::]:443 ssl http2 ipv6only=on; # managed by Certbot
    listen 443 ssl http2; # managed by Certbot
    
    ssl_certificate /etc/letsencrypt/live/escapingthematrix.online/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/escapingthematrix.online/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
} 

之后,您将拥有两个具有相同服务器名称的 http/80 服务器块,如果我们仔细观察,我们会注意到这里的第二个 http/80 服务器块是多余的,实际上并没有在第一个服务器 http/80 块之上添加任何新配置。我认为您可以安全地将其删除。

server { 
    if ($host = www.naos-soultrap.online) { 
        return 301 https://$host$request_uri; 
    } # managed by Certbot

    if ($host = naos-soultrap.online) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    listen [::]:80;
    server_name naos-soultrap.online www.naos-soultrap.online;
    return 404; # managed by Certbot
} 

server { 
    if ($host = naos-soultrap.online) { 
        return 301 https://$host$request_uri; 
    } # managed by Certbot

    listen 80;

    server_name naos-soultrap.online www.naos-soultrap.online;
    return 404; # managed by Certbot
}

相关内容