nginx中,80端口转发到443后域名多次被重定向问题
server{ listen 80; server_name www.baidu.com #你的域名 #所有http请求80端口的都转发到https,访问443端口 return 301 https://$host$request_uri; # 仅在直接通过 HTTP 访问时重定向(排除代理层) #if ($http_x_forwarded_proto != 'https') { # return 301 https://$host$request_uri; # } }
server { listen 443 ssl; server_name www.baidu.com; access_log /www/wwwlogs/accessGloble443.log; ssl_certificate /www/server.crt; ssl_certificate_key /www/server.key; location /{ access_log /www/wwwlogs/accessGloble443default.log; index index.html; root /www/server/wwwroot; } location /api { proxy_pass https://127.0.0.1:7766; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $host; access_log /www/wwwlogs/accessGloble443Api.log; } }
配置是这么一个配置,一看,是没有问题。但是问题就是在这个没有问题的情况下,访问自己的域名还是出现了多次被重定向
可花了不少时间,
最后发现是
-
Cloudflare Flexible SSL:
若使用 Cloudflare 的 “Flexible SSL” 模式,代理层会以 HTTP 协议与后端通信,导致X-Forwarded-Proto: http
,触发重定向循环。
解决方案:将 Cloudflare 的 SSL 模式改为 Full (strict)。