【发布时间】:2015-01-24 16:45:48
【问题描述】:
第一部分
我在 htaccess 文件中有以下重写规则(在 OpehShift 服务器上):
RewriteEngine on
# Must NOT be SSL
RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [or]
RewriteCond %{REQUEST_URI} /
RewriteCond %{REQUEST_URI} !^/user/login
RewriteCond %{REQUEST_URI} !^/user/register
RewriteCond %{REQUEST_URI} !^/user/profile
RewriteCond %{REQUEST_URI} !^/user/captcha
RewriteRule (.*) http://%{SERVER_NAME}/$1 [last,redirect=permanent]
# MUST be SSL
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/user/login [or]
RewriteCond %{REQUEST_URI} ^/user/register [or]
RewriteCond %{REQUEST_URI} ^/user/captcha [or]
RewriteCond %{REQUEST_URI} ^/user/profile
RewriteRule (.*) https://%{SERVER_NAME}/$1 [last]
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
目的是通过 SSL 处理一些路径,而所有其他路径应该可以通过普通 http 访问。最初设置这样的配置是一个挑战,但最终解决了 - 你可以在 OpenShift forums 上找到它的历史。
这在过去的某个时刻(可能是一些 OpenShift 更新)之前一直运行良好,当时它开始在 https 页面上循环循环:例如,当用户在他的个人资料页面上(使用 https)并单击链接以获取安全路径之外的页面,它一次又一次地尝试重定向到https。
根据规则,如果不是登录、注册、个人资料或验证码页面,他们应该将https 更改为http,但这不会再发生了。可能是什么问题?提前致谢。
第二部分
尽管我接受了@anubhava 的回答(因为这似乎是 Apache 的问题,而不是我的问题,正如他在数小时的宝贵调试后所认为的那样),我仍在努力解决这个问题,并且任何进一步的解决方案/解决方法都非常重要欢迎。
同时问题归结为以下代码:
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ - [env=ps:https]
RewriteCond %{HTTP:X-Forwarded-Proto} https
RewriteRule ^(.*)$ - [env=ps:http]
# known public pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/site/page/about
RewriteRule (.*) %{ENV:ps}://%{SERVER_NAME}/$1?ps=%{ENV:ps}&step=1 [last,redirect=permanent]
现在,当我转到 about 页面时,我得到了以下 http 标头:
HTTP/1.1 301 Moved Permanently
Date: Thu, 27 Nov 2014 15:07:27 GMT
Server: Apache/2.2.15 (Red Hat)
Location: https://mydomain.rhcloud.com/site/page/about?ps=http&step=1
Content-Length: 385
Content-Type: text/html; charset=iso-8859-1
Accept-Ranges: none
Keep-Alive: timeout=15, max=99
Connection: Keep-Alive
请注意Location 有https 原型,但ps 参数输出为http(如预期的那样)。如果这两个值都取自 ps 变量,这会怎样?恕我直言,这看起来很奇怪。
简而言之:从 https 到 http 的重定向在 OpenShift 服务器上不起作用。尽管规则说协议应该是http 服务器发送https - 因此循环重定向。
第三部分
也许我应该提到 OpenShift 站点使用 HAProxy 作为前端。它的配置不包含任何 ssl 特定设置。但我找到了this related answer,可能会读到:
最后,您将不会真正将人们从 HTTPS 连接中重定向出去,这被认为是不安全的,并且不支持。
那么,HAProxy 是否总是隐含地将协议覆盖到https 以进行从安全页面进行的重定向?那么如何解决这个问题呢?
【问题讨论】:
标签: .htaccess mod-rewrite https openshift haproxy