gpt4 book ai didi

.htaccess - Nginx:在 https 上将非 www 重定向到 www

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

如果我在 https 上,Nginx 无法将非 www 重定向到 www:

https://domain.comhttps://www.domain.com

我当前在 .conf 中的设置是:

server {
listen 80;
server_name www.domain.com domain.com;
return 301 https://www.domain.com$request_uri;
}
server {
listen 443;
server_name domain.com;
return 301 https://www.domain.com$request_uri;
}
server {
listen IP_ADDRESS:443 ssl;
server_name www.domain.com;
...
}

http://domain.comhttps://www.domain.comhttp://www.domainhttps://www.domain.com有效,但 https 上的非 www 到 www 无效。

如果我在第二个 server block 上添加 IP_ADDRESS,我会在 Chrome 中收到一个错误(SSL 错误)并且两者(www 和非 www)都停止工作。

更新:

感谢 Steffen(下面的回答),我将自签名证书更新为 *.domain.com 而不是 domain.com

.conf 文件以这种格式更新:

ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;

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

最佳答案

我遇到过类似的情况,这就是我解决重定向问题的方法

https://example.com -----> https://www.example.com

 server {
listen 443;
server_name example.com;
if ($host = example.com) {
rewrite ^(.*) https://www.example.com:443$request_uri? permanent;
}
}

希望这对您有所帮助!

Using if condition in nginx

Directive if has problems when used in location context, in some cases it doesn't do what you expect but something completely different instead. In some cases it even segfaults. It's generally a good idea to avoid it if possible. The only 100% safe things which may be done inside if in location context are: return ...; rewrite ... last;

关于.htaccess - Nginx:在 https 上将非 www 重定向到 www,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22792394/

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