nginx上的服务器名称冲突

时间:2017-12-16 08:01:23

标签: nginx

不知道为什么我会遇到conflicting server name例外。

我接受前缀中带有WWW的请求。

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

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

Nginx.conf

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

异常日志

    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

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:1)

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

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

解决方案:保持nginx.conf文件不变,并将/etc/nginx/conf.d/default.conf内的服务器块(如果它不存在,请自行制作)编辑为所需的服务器名称和配置,代替。

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

这是默认且未触及的nginx.conf文件:

# 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 {
#        }
#    }

}