Score:0

Redirect old url to new with NGINX conflicting server name error

nl flag

My website will change url, I'm trying to apply a redirect (old-name.example.com to new-name.example.com), 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 {
        server_name old-name.example.com;
        return 301 new-name.example.com$request_uri;
}

server {

    server_name new-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 = new-name.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;

    server_name new-name.example.com;
    return 404; # managed by Certbot
}
Ivan Shatsky avatar
gr flag
What's wrong with the answer I give you yesterday on the same question on [SO](https://stackoverflow.com/questions/72473085/redirect-https-to-https-with-nginx-conflicting-server-name-error)? What are you expecting from cross-posting your question? If you don't understand something, you can ask additional questions there.
SaltySteven avatar
nl flag
Sorry @IvanShatsky i don't get notification about this. i try to do my redirect with your scheme, and i got the following error: 414 Request-URI Too Large. Ahd the url goes like this: old-name.example.com/https:://www.new-name.example.com/http:://… and it goes on for a while, what can i do wrong?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.