首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >nginx上的服务器名称冲突

nginx上的服务器名称冲突
EN

Stack Overflow用户
提问于 2017-12-16 16:01:23
回答 3查看 16.8K关注 0票数 4

不知道为什么会出现conflicting server name异常。

我是否接受前缀为WWW的请求。

return 301 https://$server_name$request_uri;将强制非https请求到https。

你知道如何解决这个异常吗?

Nginx.conf

代码语言:javascript
复制
server {
    listen      80 ;
    server_name myApp.co www.myApp.co;

    root  /home/deployer/workspace/myApp-web/dist;
    error_log /var/log/nginx/myApp_web_error.log warn;
    access_log /var/log/nginx/myApp_web_access.log;
    listen 443 ssl;
    listen [::]:443 ssl;

    ssl_certificate /etc/nginx/ssl/myApp_co.bundled.crt;
    ssl_certificate_key /etc/nginx/ssl/myApp.key;
    large_client_header_buffers 4 4800k;


    location / {
        try_files $uri $uri/ /index.html ; # make HTML5 workable
        gzip on;
        gzip_static on;
        gzip_min_length 1k;
        gzip_comp_level 6;
        gzip_types application/javascript text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
        gzip_vary off;
        gzip_disable "MSIE [1-6]\.";
    }

    location /api/v1 {
        proxy_pass http://localhost:7617/api/v1/;
    }
}

server {
     listen      80;
     server_name myApp.co www.myApp.co;
     return 301 https://$server_name$request_uri;
 }

异常日志

代码语言:javascript
复制
    2017/12/05 06:54:42 [warn] 6059#0: conflicting server name "myApp.co" on 0.0.0.0:80, ignored
    2017/12/05 06:54:42 [warn] 6059#0: conflicting server name "www.myApp.co" on 0.0.0.0:80, ignored
    2017/12/05 06:55:05 [warn] 6089#0: conflicting server name "myApp.co" on 0.0.0.0:80, ignored
    2017/12/05 06:55:05 [warn] 6089#0: conflicting server name "www.myApp.co" on 0.0.0.0:80, ignored
    2017/12/05 06:55:06 [warn] 6093#0: conflicting server name "myApp.co" on 0.0.0.0:80, ignored
EN

回答 3

Stack Overflow用户

发布于 2018-01-23 04:01:47

诊断:使用sudo nginx -t查看警告或错误的列表。

Reason:默认NGINX配置文件服务器块nginx.conf中的server_name myApp.co www.myApp.co;

Solution:保持nginx.conf文件不变,并在/etc/nginx/conf.d/default.conf中编辑服务器块(如果它不存在,请自己编辑)为您所需的服务器名称和配置。

看一看在CentOS上制作服务器块的this,你也可以在这个标题中搜索Ubuntu。

这是一个默认且未更改的nginx.conf文件:

代码语言:javascript
复制
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}
票数 4
EN

Stack Overflow用户

发布于 2017-12-16 22:13:12

您不能让两个服务器块在同一端口上使用相同的server_name进行侦听。

我认为在第一个服务器块中,您正在尝试接受https请求,因此您必须将端口号更改为443。

代码语言:javascript
复制
server {
   listen      443;
   server_name myApp.co www.myApp.co;

   root  /home/deployer/workspace/myApp-web/dist;
   error_log /var/log/nginx/myApp_web_error.log warn;
   access_log /var/log/nginx/myApp_web_access.log;

   ssl_certificate /etc/nginx/ssl/myApp_co.bundled.crt;
   ssl_certificate_key /etc/nginx/ssl/myApp.key;
   large_client_header_buffers 4 4800k;


   location / {
       try_files $uri $uri/ /index.html ; # make HTML5 workable
       gzip on;
       gzip_static on;
       gzip_min_length 1k;
       gzip_comp_level 6;
       gzip_types application/javascript text/plain application/x-
       javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
       gzip_vary off;
       gzip_disable "MSIE [1-6]\.";
   }

   location /api/v1 {
       proxy_pass http://localhost:7617/api/v1/;
   }
}

 server {
    listen      80;
    server_name myApp.co www.myApp.co;
    return 301 https://$server_name$request_uri;
 }
票数 3
EN

Stack Overflow用户

发布于 2022-01-23 17:04:08

如果您备份了sites-enabled配置,将其保留在相同的位置,并且包含sites-enabled目录(include /etc/nginx/sites-enabled/*;)中的所有配置,也会发生这种情况。备份配置和您更新的配置都具有相同的名称,并且都由nginx加载。

简单的修复方法:将备份文件移出sites-enabled目录;运行nginx -t以确认其工作正常,然后重新加载(systemctl reload nginx)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47843741

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档