0

问题描述
blog更换域名,旧域名old.com,新域名new.com,使用Let's Encrypt泛域名证书同时开启新旧域名https,然后Nginx配置301重定向,目的是把www.old.com old.com www.new.com都重定向到https://new.com
配置文件如下:

server {
        root /var/www/blog;

        index index.html index.htm index.nginx-debian.html;

        server_name www.new.com new.com www.old.com old.com;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }
    listen [::]:443 ssl ipv6only=on; 
    listen 443 ssl; 
    ssl_certificate /etc/letsencrypt/live/new.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/new.com/privkey.pem; 
    include /etc/letsencrypt/options-ssl-nginx.conf; 
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; 

}

server {
    if ($host = old.com) {
        return 301 https://new.com$request_uri;
    } 


    if ($host ~ ^[^.]+\.old\.com$) {
        return 301 https://new.com$request_uri;
    } 


    if ($host = new.com) {
        return 301 https://$host$request_uri;
    } 


    if ($host ~ ^[^.]+\.new\.com$) {
        return 301 https://new.com$request_uri;
    } 


        listen 80 default_server;
        listen [::]:80 default_server;

        server_name  www.new.com new.com www.old.com old.com;
    return 404; 

}    
        

经上面配置,目前浏览器内通过http访问www.new.com www.old.com old.com都可以重定向到https://new.com,但如果通过https访问时,却不能重定向到https://new.com,而是直接打开所输入的域名,如浏览器输入https://www.old.com,则打开的网址也就是https://www.old.com,而不能重定向跳转到https://new.com
如我想实现把https://www.old.com https://old.com https://www.new.com 也都重定向到https://new.com,该如何配置,求到牛支招!

1kuxyz 1
2019-06-12 提问
1 个回答
0

443端口配置中没有301配置,肯定不跳转了。

撰写答案

推广链接