我正在使用Rackspace负载平衡器,它使我能够在pipe理面板中设置我的ssl键/ pem。 一切工作正常,我可以使用http和https协议。 但是,如果我尝试使用以下方法将httpredirect到https:
server{ listen *:80; server_name mydomain.com www.mydomain.com; rewrite ^ https://mydomain.com$request_uri? permanent;
…我得到一个redirect循环。 我意识到我没有听443端口,但那是因为负载平衡器为我处理。 我也尝试包装重写if ($scheme ~* http){无效。
我的问题的另一部分是,我想从url中删除www,我可以做一个单一的重写? 上面的重写不应该照顾这个吗?
谢谢你的帮助!
sciurus是正确的,因为当负载均衡器卸载SSL时,Rackspace的云负载均衡器会将X-Forwarded-Proto设置为https。 为了避免在nginx中出现redirect循环,您应该能够将以下内容添加到虚拟主机configuration中的location部分:
if ($http_x_forwarded_proto = "http") { rewrite ^/(.*)$ https://mydomain.com/$1 permanent; }
这应该避免无限redirect循环,同时将非https请求redirect到https。
通过使用nginx的内置服务器variables$request_uri和$server_name您可以在不使用正则expression式的情况下执行此操作。 将以下内容添加到服务器的location块,即可完成:
if ($http_x_forwarded_proto = "http") { return 301 https://$server_name$request_uri; }
这假定您的负载均衡器正在将$http_x_forwarded_proto标头和请求一起发送到您的后端实例。 其他常见的头文件包括$http_x_forwarded_scheme和$scheme 。
更多信息可以在nginx 陷阱和常见错误文档中find: https : //www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#taxing-rewrites
负载均衡器总是通过http与您通话。 发生了什么事情
步骤2-4不断重复,直到浏览器检测到redirect循环并放弃。
编辑:要解决此问题,只有在X-Forwarded-Proto标头设置为http时才执行重写。 这个头文件是Rackspace的负载平衡器如何告诉你的Web服务器接收请求的协议。