gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:34:15 25 4
gpt4 key购买 nike

我正在尝试将旧的 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;
}
}

请指导如何解决此问题。

最佳答案

在您的问题中,第二个 rewrite 语句导致重定向循环。正确的解决方案是将 server block 拆分为两个,然后从一个重定向到另一个,与从 http 重定向到 https 的方式大致相同.为此,您可以使用第一个 server block 。

带有 default_serverserver block 不需要 server_name 指令,因为它无论如何都会响应所有不匹配的服务器名称。参见 this document了解详情。

假设这些是此配置中唯一的 https 服务器,并且两个 server block 共享相同的 SSL 配置(如您的评论所建议的),片段文件可以被移动到上面以被两个 server block 继承

最后,主 server block 中的 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;

...
}

关于.htaccess - 使用 ssl nginx 将旧域重定向到新域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45144534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com