Redirect 使用ssl的nginx重定向循环
这是一个非常类似的问题,但讨论还没有让我找到答案。我正在学习如何使用nginx和ssl,所有东西都在常规的http://example.com端完美工作,但是当路由到https://example.com/admin时,我看到: 此网页有一个重定向循环 这是我的配置文件:Redirect 使用ssl的nginx重定向循环,redirect,ssl,nginx,redirect-loop,Redirect,Ssl,Nginx,Redirect Loop,这是一个非常类似的问题,但讨论还没有让我找到答案。我正在学习如何使用nginx和ssl,所有东西都在常规的http://example.com端完美工作,但是当路由到https://example.com/admin时,我看到: 此网页有一个重定向循环 这是我的配置文件: map $uri $example_org_preferred_proto { default "http"; ~^/(images|css|javascript)/ "none";
map $uri $example_org_preferred_proto {
default "http";
~^/(images|css|javascript)/ "none";
~^/admin/ "https";
}
server {
listen 80;
root /usr/share/nginx/www/example.com/blog;
server_name example.com;
if ($example_org_preferred_proto = "https")
return 301 https://example.com$request_uri;
}
location ~ / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:2368;
}
}
server {
listen 443;
ssl on;
root /usr/share/nginx/www/example.com/blog;
server_name example.com;
ssl_certificate /usr/share/nginx/<redacted>.crt;
ssl_certificate_key /usr/share/nginx/<redacted>.key;
if ($example_org_preferred_proto = "http") {
return 301 http://example.com$request_uri;
}
location ~ / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:2368;
}
}
map$uri$example\u org\u preferred\u proto{
默认为“http”;
~^/(图像| css | javascript)/“无”;
~^/admin/“https”;
}
服务器{
听80;
root/usr/share/nginx/www/example.com/blog;
server_name example.com;
如果($example\u org\u preferred\u proto=“https”)
返回301https://example.com$request_uri;
}
地点~/{
代理集头X-Real-IP$remote\u addr;
$remote\u addr的代理\u集\u头X转发;
代理设置头主机$Host;
代理通行证http://127.0.0.1:2368;
}
}
服务器{
听443;
ssl-on;
root/usr/share/nginx/www/example.com/blog;
server_name example.com;
ssl_certificate/usr/share/nginx/.crt;
ssl\u certificate\u key/usr/share/nginx/.key;
如果($example\u org\u preferred\u proto=“http”){
返回301http://example.com$request_uri;
}
地点~/{
代理集头X-Real-IP$remote\u addr;
$remote\u addr的代理\u集\u头X转发;
代理设置头主机$Host;
代理通行证http://127.0.0.1:2368;
}
}
基本上,我想要实现的是拥有一个通常未加密运行的站点,但当我指向我的管理页面时,浏览器会重定向到https并加密我的登录
注意:映射思想来自于并且似乎比使用重写要好得多。当nginx遇到
https
协议时,它认为它仍然使用http
作为协议,并且没有与其他头一起转发,请尝试添加:
proxy_set_header X-Forwarded-Proto $scheme;
在您的位置块中修复它。我已经尝试了其他答案,但没有任何效果。然后我意识到,由于我使用Cloudflare,问题可能不在服务器上,而在Cloudflare上。请注意,当我将SSL设置为
Full(Strict)
时,一切都正常工作
几个小时以来,我一直在努力解决这个问题。谢谢,解决了我的问题!谢谢,我在这里遇到了同样的问题,在更新cloudflare设置后,它工作正常。如果您使用的是
cloudflare
,请参阅@Zaki Aziz答案-