【问题标题】:Redirect Old domain to New domain with ssl nginx使用 ssl nginx 将旧域重定向到新域
【发布时间】:2017-12-22 00:12:11
【问题描述】:

我正在尝试将旧的 ssl 域重定向到新的 ssl 域。但它正在向我发送重定向循环。

我已经在 nginx 虚拟主机中尝试过这个配置。

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name example.com www.example.com old-domain.com; 
    rewrite ^ https://www.example.com$request_uri permanent;
}

server {
    #SSL Configuration
    listen 443 ssl http2 default_server;
    listen [::]:443 ssl http2 default_server;
        include snippets/ssl-example.com.conf;
        include snippets/ssl-params.conf;

    root /home/username/example/public;

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

    server_name example.com www.example.com old-domain.com;

    rewrite ^ https://www.example.com$request_uri permanent;

    location / {
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

        location ~ /\.ht {
            deny all;
        }
}

请指导如何解决此问题。

【问题讨论】:

  • 您有旧域的单独证书文件吗?
  • 两个域的证书相同。

标签: .htaccess ssl nginx


【解决方案1】:

在您的问题中,第二个 rewrite 语句导致了重定向循环。正确的解决方案是将server 块分成两部分并从一个重定向到另一个,这与从http 重定向到https 的方式非常相似。为此,您可以使用第一个 server 块。

带有default_serverserver 块不需要server_name 指令,因为它无论如何都会响应所有不匹配的服务器名称。有关详细信息,请参阅this document

假设这些是此配置中唯一的 https 服务器,并且两个 server 块共享相同的 SSL 配置(正如您的评论所建议的那样),可以将 sn-ps 文件移到上面 由两个server 块继承

最后,server 主块中的 server_name 指令仅列出要重定向的服务器名称。

例如:

include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;
    return 301 https://www.example.com$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name www.example.com;

    ...
}

【讨论】:

  • 谢谢,我知道我做错了什么。这真的很有帮助。
猜你喜欢
  • 1970-01-01
  • 2021-04-14
  • 2013-02-11
  • 1970-01-01
  • 2020-06-15
  • 1970-01-01
  • 2016-06-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多