Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
1 ตอบกลับ
7167 มุมมอง

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
}


อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

There is a conflict with the server names in your NGINX configuration. 

Remove the duplicated server block for old-name.example.com

server {    server_name old-name.example.com;
    return 301 new-name.example.com$request_uri;
}

Update the existing server block for old-name.example.com to handle both HTTP and HTTPS redirects.

server {

    listen 80;
    listen 443 ssl;
 
    server_name old-name.example.com;
 
    if ($scheme = http) {
        return 301 https://new-name.example.com$request_uri;
    }
   
    return 404;
}

Update the existing server block for new-name.example.com to handle HTTP and HTTPS requests.

server {

    listen 80;
    listen 443 ssl;
 
    server_name new-name.example.com;
 
    location / {
        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,x-auth';
        [some config...]
    }
 
    [ssl config...]
}

Remember to replace old-name.example.com and new-name.example.com with your actual domain names. After making these changes, Restart NGINX,  Now You can verify your URL with this tool, Redirect Checker to get a detailed redirection path and its status code.

I hope it helps you


อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
1
ก.ค. 18
8474
3
พ.ค. 24
18155
3
พ.ค. 24
15255
0
ก.ย. 23
1684
1
ธ.ค. 23
34686