在 PHP 中检测 HTTPS 请求
我遇到的问题与需要保留网站的某些网址受 HTTPS 保护而其余网址则改为 HTTP 有关。
通常,您有 $_SERVER['HTTP_HTTPS']
或 $_SERVER['HTTPS']
(取决于您的 Apache 风格)。 您还可以检查端口 - 正常流量为 80,HTTPS 为 443。
我的问题是证书位于负载均衡器上,并且所有这些变量都不可用,并且网络服务器看到 http://www.foo.com 在端口 80 上。解决此问题的一种方法是告诉负载均衡器在不同端口上发送流量,但我想知道是否还有其他方法来检测来自负载均衡器?
The problem that I am having has to do with the need to keep some urls of a website protected by HTTPS and the rest kicked to HTTP.
Normally, you have $_SERVER['HTTP_HTTPS']
or $_SERVER['HTTPS']
(depending on your flavor of Apache). You also can check the port - it's 80 for normal traffic and 443 for HTTPS.
My problem is that the certificate sits on the loadbalancer, and all these variables are unavailable, and the webserver sees http://www.foo.com on port 80. One way to fix this is to tell the loadbalancer to send the traffic on a different port, but I wonder if there are other ways to detect HTTPS coming from the load balancer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果任何人在 Amazon AWS Elastic Load Balancer 背后遇到相同的问题,解决方案很简单,因为
$_SERVER
变量将包括:因此,要获取协议,您可以使用:
If anybody has the same issue behind an Amazon AWS Elastic Load Balancer, the solution is simple because the
$_SERVER
variable will include:So, to get the protocol, you could use:
如果负载均衡器是 SSL 连接的另一端,则您无法获取除负载均衡器明确提供的信息之外的更多信息。 我会去添加一个 http 标头,它可能已经这样做了,转储所有 HTTP 标头并查看。
作为另一种解决方案,您可以根据 URL 在负载均衡器上进行重定向。
If the load balancer is the other end of the SSL connection, you cannot get any more info than the load balancer explicitly provides. I would go for adding a http header, it may already be doing that, dump all the HTTP headers and look.
As another solution, you can do the redirection on the load balancer based on URL.
$_SERVER['HTTP_X_FORWARDED_PROTO'] 对于 joomla 用户来说似乎是一个很好的解决方案,因为如果您的负载均衡器执行重定向并且您将force_ssl 设置设置为 1 或 2 那么您将以无限循环结束,因为 joomla 总是看到 http:
the $_SERVER['HTTP_X_FORWARDED_PROTO'] seems to be a good solution for joomla users because if your loadbalancer does the rediretion and you set the force_ssl setting to 1 or 2 then you will end in an infinite loop because joomla always sees http: