Nginx 301 域名重定向不起作用

Nginx 301 域名重定向不起作用

现在我已更新服务器配置以适应新域名,因此尝试将旧域名(example.com)重定向(或者我猜是转发)到新域名( )。但是,我的 Nginx 配置中的 301 重定向子句似乎无法正常工作,因为我仍然能够访问,它会重定向到,就像将此服务器指向新域名之前设置的配置一样。访问时会呈现,这很有意义,因为我已将 SSL 配置更改为指向的证书。我的 301 配置有问题吗?如果没有,是不是因为我仍然有一个指向我的 IP 地址的 和,从而允许通过旧域名访问该网站?example2.comexample.comhttps://www.example.comhttps://www.example.comYour connection is not privateexample2.comA record (example.com)CNAME record (www.example.com)

笔记:我没有问题访问example2.com哪个重定向到https://www.example2.com预期

这是我的 Nginx 配置:

server {
    listen 80;
    listen [::]:80;
    server_name example2.com www.example2.com example.com www.example.com;
    return 301 https://www.example2.com$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    include snippets/ssl-www.example2.com.conf;
    include snippets/ssl-params.conf;
    server_name example2.com;
    return 301 https://www.$server_name$request_uri;
}

server {
    listen 443 ssl http2 default_server;
    listen [::]:443 ssl http2 default_server;
    include snippets/ssl-www.example2.com.conf;
    include snippets/ssl-params.conf;

    server_name www.example2.com;

    client_max_body_size 100M;

    location ~ ^/\.well-known {
        root /var/www/ghost;
        allow all;
    }

    location / {
        proxy_pass http://127.0.0.1:2368;
        proxy_buffering off;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Referer "";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_http_version 1.1;
    }
}

答案1

首先,除非您特别想使用 IPv6,否则无需设置它。请按如下方式使用您的配置:

server {
    listen 80;
    server_name example2.com www.example2.com example.com www.example.com;
    return 301 https://www.example2.com$request_uri;
}

server {
    listen 443 ssl http2;
    server_name example2.com;
    return 301 https://www.example2.com$request_uri;
}

server {
    listen 443 ssl http2;
    server_name www.example2.com;

    ssl_dhparam /etc/nginx/ssl/dhparam.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:ECDHE-RSA-AES128-GCM-SHA256:AES256+EECDH:DHE-RSA-AES128-GCM-SHA256:AES256+EDH:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";
    add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";

    ssl_certificate         /etc/nginx/ssl/example2.com.crt;
    ssl_certificate_key     /etc/nginx/ssl/example2.com.key;

    client_max_body_size 100M;

    location ~ ^/\.well-known {
        root /var/www/ghost;
        allow all;
    }

    location / {
        proxy_pass http://127.0.0.1:2368;
        proxy_buffering off;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Referer "";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_http_version 1.1;
    }
}

另外,不要忘记创建 /etc/nginx/ssl 文件夹和 dhparam.pem 文件。

sudo mkdir /etc/nginx/ssl && sudo openssl dhparam -dsaparam -out /etc/nginx/ssl/dhparam.pem 4096

相关内容