conflicting server name on nginx

Not know why I got conflicting server name exception.

I accept the request with WWW in the prefix or not.

And return 301 https://$server_name$request_uri; is to force non https request to https.

Any idea how to solve this exception?

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

Exception log

    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

Solution 1:

Diagnosis: use sudo nginx -t to see the list of the warnings or errors.

Reason: server_name myApp.co www.myApp.co; in the default NGINX configuration file server block, nginx.conf.

Solution: Leave nginx.conf file untouched and edit server blocks inside of /etc/nginx/conf.d/default.conf (if it's not existed make it on your own) to your desired server name and configurations, instead.

Take a look at this to make your server blocks on CentOS and you can search that title for Ubuntu too.

This is a default and untouched nginx.conf file:

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

}

Solution 2:

You cannot have two server blocks listening at the same port and with the same server_name.

I think that in the first server block you are trying to accept https requests, so you must change port number to 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;
 }