gpt4 book ai didi

nginx - CentOs Nginx 重定向 https ://www to https://

转载 作者:行者123 更新时间:2023-12-04 19:41:36 24 4
gpt4 key购买 nike

我在 centos 中使用 Nginx。我面临重定向问题。

我想将所有请求重定向到 https://some-domain.com/url
我能够重定向

http://www.some-domain.com to https://some-domain.com
www.some-domain.com to https://some-domain.com
http://some-domain.com to https://some-domain.com

但我无法重定向 https://www.some-domain.com
配置文件:
server
{
listen 443 ssl;

server_name some-domain.com www.some-domain.com;
ssl_certificate /etc/nginx/ssl/some-domain.com.chained.crt;
ssl_certificate_key /etc/nginx/ssl/some-domain.com.key;

if ($host = https://www.some-domin.com) {
return 301 https://some-domin.com$request_uri;
}
}

最佳答案

最好将两个服务器括号分开以避免使用“if”。您的问题是您向主机添加了“https://”,而您需要比较的只是 www.some-domain.com。

这个例子更简单:

#Server bracket for https connections that come with host www.some-domain.com
server
{
listen 443 ssl;
server_name www.some-domain.com;
ssl_certificate /etc/nginx/ssl/some-domain.com.chained.crt;
ssl_certificate_key /etc/nginx/ssl/some-domain.com.key;

#redirects to https://non-www

location / {
return 301 https://some-domin.com$request_uri;
}
}



# and then you can set a server bracket for non-www https connections.
# nginx will sort the connections depending on host for itself
server
{
listen 443 ssl;

server_name some-domain.com;
ssl_certificate /etc/nginx/ssl/some-domain.com.chained.crt;
ssl_certificate_key /etc/nginx/ssl/some-domain.com.key;

#Here it arrives 443 and without www, do what you wanted here
}

关于nginx - CentOs Nginx 重定向 https ://www to https://,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59053034/

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