Home > database >  Redirect HTTPS to HTTPS with NGINX conflicting server name error
Redirect HTTPS to HTTPS with NGINX conflicting server name error

Time:06-07

My website will change url, I'm trying to apply a redirect (HTTPS to HTTPS), but I'm getting the following error:

nginx: [warn] conflicting server name "old-name.example.com" on 0.0.0.0:443, ignored nginx.

Here is my nginx config file on /etc/nginx/sites-enabled/myconf.conf:

server {
        
            server_name old-name.example.com;
        
            location / {
                add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
                #
                # Custom headers and headers various browsers *should* be OK with but aren't
                #
                add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,x-auth';
                #
                # Tell client that this pre-flight info is valid for 20 days
                #
    
                [some config....]
            }
        
            listen 443 ssl; # managed by Certbot
    
           [ssl config...]
}
        
        
server {
            if ($host = old-name.example.com) {
                return 301 https://$host$request_uri;
            } # managed by Certbot
        
        
                listen 80;
        
                server_name old-name.example.com;
            return 404; # managed by Certbot
}
        
server {
        listen 443 ssl;
        server_name old-name.example.com;
        return 301 $scheme://www.new-name.com$request_uri;
}

CodePudding user response:

Using duplicate server name listening on the same port is senseless, the second server block will never be chosen to handle the request. You probably need something like

server {
    server_name www.new-name.com;
    listen 443 ssl;
    ... ssl config for the www.new-name.com
    location / {
        ... serve content
    }
}
server {
    server_name www.new-name.com;
    listen 80;
    return 301 https://www.new-name.com$request_uri;
}
server {
    server_name old-name.example.com;
    listen 80;
    listen 443 ssl;
    ... ssl config for the old-name.example.com
    return 301 https://www.new-name.com$request_uri;
}
  • Related