gpt4 book ai didi

redirect - nginx http 到 https 导致重定向过多

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

以下 nginx 设置适用于 https://DNSNAMEhttp://DNSNAME

upstream prod {
server server_ip_1;
server server_ip_2;
}

server {
listen 80;
listen 443 ssl;

root /usr/share/nginx/www;
index index.html index.htm;

server_name localhost;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;

location / {
proxy_pass https://prod;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
}
}

但我需要始终将域设置为 https://并初始化 DNSname。所以当我尝试像下面这样重定向时。它导致太多重定向并且无法加载页面。任何人都请帮助解决这个问题。

server {
listen 80;
server_name DNSName;
return 301 https://$server_name$request_uri;
}

最佳答案

我将 80 和 443 分成两个独立的服务,类似这样。

upstream prod {
server server_ip_1;
server server_ip_2;
}

server {
listen 80;
server_name localhost;
return 301 https://$server_name$request_uri;
}

server {
listen 443 ssl;

root /usr/share/nginx/www;
index index.html index.htm;

server_name localhost;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;

location / {
proxy_pass https://prod;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
}
}

它对我有用。谢谢!

关于redirect - nginx http 到 https 导致重定向过多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32316879/

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