简体   繁体   English

使用ssl nginx将旧域重定向到新域

[英]Redirect Old domain to New domain with ssl nginx

I am trying to redirect old ssl domain to new ssl domain. 我正在尝试将旧的SSL域重定向到新的SSL域。 But it is sending me redirection loop. 但这给我发送了重定向循环。

I have tried this configuration in nginx virtual host. 我已经在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;
        }
}

Please guide How can I fix this issue. 请指导如何解决此问题。

In your question, the second rewrite statement is causing a redirection loop. 在您的问题中,第二个rewrite语句正在导致重定向循环。 The correct solution is to split the server block into two and redirect from one to the other, much the same way you redirect from http to https . 正确的解决方案是将server块分成两部分,然后从一个重定向到另一个,与从http重定向到https方式几乎相同。 You can use the first server block for this purpose. 为此,您可以使用第一个server块。

The server block with default_server does not need a server_name directive, as it responds to all unmatched server names anyway. 带有default_serverserver块不需要server_name指令,因为它始终会响应所有不匹配的服务器名称。 See this document for details. 有关详细信息,请参见此文档

Assuming that these are the only https servers in this configuration, and that both server blocks share the same SSL configuration (as your comment suggests), the snippets files could be moved above to be inherited by both server blocks. 假设这些服务器是此配置中唯一的https服务器,并且两个server块都共享相同的SSL配置(如您的注释所建议),则摘要文件可以移到上方,从而由两个server继承

And finally, the server_name directive in the main server block only lists the server names that are not to be redirected. 最后,主server块中的server_name指令仅列出被重定向的服务器名称。

For example: 例如:

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;

    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM