http到https apache重定向..无限循环

时间:2017-01-04 19:45:26

标签: apache redirect

环境:CentOS
Apache版本:Apache / 2.2.15

我一直试图强迫我的网站成为https但遇到无限重定向

<VirtualHost *:443>
ServerName example.com
ServerAlias example 

SetEnv HTTPS on
ProxyPreserveHost On
ProxyPass / http://localhost/
ProxyPassReverse / http://localhost/

SSLEngine on
SSLCertificateFile /ssl/star_ecom_sanmar_com.crt
SSLCertificateKeyFile /ssl/star_ecom_sanmar_com.key
SSLCertificateChainFile /ssl/DigiCertCA.crt
SSLProxyEngine on

<VirtualHost *:80>
ServerName example.com localhost

ServerAlias example
DocumentRoot ......

DirectoryIndex index.shtml index.html index.jsp
AddType text/html .shtml
AddHandler server-parsed .shtml
AddType application/x-httpd-php .php .php4

CustomLog /var/log/httpd/access_log combined env=!dontlog
ErrorLog /var/log/httpd/error_log

RewriteEngine on
RewriteCond "%{HTTPS}" !="on"
RewriteRule ^/(.*)$ https://example.com/$1 [R=301,L]
....

即使完成重定向到https后,上面的重写规则也会始终执行,从而导致无限循环,这使我认为{HTTPS}的值永远不会更新。 {SERVER_PORT}的情况也是如此,即使请求来自https,也始终为80。 下面是php

的输出

&#13;
&#13;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html><head>
<style type="text/css">
body {background-color: #ffffff; color: #000000;}
body, td, th, h1, h2 {font-family: sans-serif;}
pre {margin: 0px; font-family: monospace;}
a:link {color: #000099; text-decoration: none; background-color: #ffffff;}
a:hover {text-decoration: underline;}
table {border-collapse: collapse;}
.center {text-align: center;}
.center table { margin-left: auto; margin-right: auto; text-align: left;}
.center th { text-align: center !important; }
td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;}
h1 {font-size: 150%;}
h2 {font-size: 125%;}
.p {text-align: left;}
.e {background-color: #ccccff; font-weight: bold; color: #000000;}
.h {background-color: #9999cc; font-weight: bold; color: #000000;}
.v {background-color: #cccccc; color: #000000;}
.vr {background-color: #cccccc; text-align: right; color: #000000;}
img {float: right; border: 0px;}
hr {width: 600px; background-color: #cccccc; border: 0px; height: 1px; color: #000000;}
</style>
<title>phpinfo()</title><meta name="ROBOTS" content="NOINDEX,NOFOLLOW,NOARCHIVE" /></head>
<body><div class="center">
<h2>PHP Variables</h2>
<table border="0" cellpadding="3" width="600">
<tr class="h"><th>Variable</th><th>Value</th></tr>
<tr><td class="e">_SERVER["dontlog"]</td><td class="v">1</td></tr>
<tr><td class="e">_SERVER["SCRIPT_URL"]</td><td class="v">test.php</td></tr>
<tr><td class="e">_SERVER["SCRIPT_URI"]</td><td class="v">test.php</td></tr>
<tr><td class="e">_SERVER["HTTP_HOST"]</td><td class="v">example.com</td></tr>
<tr><td class="e">_SERVER["HTTP_UPGRADE_INSECURE_REQUESTS"]</td><td class="v">1</td></tr>
<tr><td class="e">_SERVER["HTTP_USER_AGENT"]</td><td class="v">Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36</td></tr>
<tr><td class="e">_SERVER["HTTP_ACCEPT"]</td><td class="v">text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8</td></tr>
<tr><td class="e">_SERVER["HTTP_ACCEPT_ENCODING"]</td><td class="v">gzip, deflate, sdch, br</td></tr>
<tr><td class="e">_SERVER["HTTP_ACCEPT_LANGUAGE"]</td><td class="v">en-US,en;q=0.8</td></tr>
<tr><td class="e">_SERVER["HTTP_X_FORWARDED_FOR"]</td><td class="v">::1</td></tr>
<tr><td class="e">_SERVER["HTTP_X_FORWARDED_HOST"]</td><td class="v">example.com</td></tr>
<tr><td class="e">_SERVER["HTTP_X_FORWARDED_SERVER"]</td><td class="v">example.com</td></tr>
<tr><td class="e">_SERVER["HTTP_CONNECTION"]</td><td class="v">Keep-Alive</td></tr>
<tr><td class="e">_SERVER["PATH"]</td><td class="v">/sbin:/usr/sbin:/bin:/usr/bin</td></tr>
<tr><td class="e">_SERVER["SERVER_SIGNATURE"]</td><td class="v">&lt;address&gt;Apache/2.2.15 (CentOS) Server at example.com Port 80&lt;/address&gt;
</td></tr>
<tr><td class="e">_SERVER["SERVER_SOFTWARE"]</td><td class="v">Apache/2.2.15 (CentOS)</td></tr>
<tr><td class="e">_SERVER["SERVER_NAME"]</td><td class="v">example.com</td></tr>
<tr><td class="e">_SERVER["SERVER_ADDR"]</td><td class="v">::1</td></tr>
<tr><td class="e">_SERVER["SERVER_PORT"]</td><td class="v">80</td></tr>
<tr><td class="e">_SERVER["REMOTE_ADDR"]</td><td class="v">::1</td></tr>
<tr><td class="e">_SERVER["DOCUMENT_ROOT"]</td><td class="v">.....</td></tr>
<tr><td class="e">_SERVER["SERVER_ADMIN"]</td><td class="v">root@localhost</td></tr>
<tr><td class="e">_SERVER["SCRIPT_FILENAME"]</td><td class="v">test.php</td></tr>
<tr><td class="e">_SERVER["REMOTE_PORT"]</td><td class="v">57954</td></tr>
<tr><td class="e">_SERVER["GATEWAY_INTERFACE"]</td><td class="v">CGI/1.1</td></tr>
<tr><td class="e">_SERVER["SERVER_PROTOCOL"]</td><td class="v">HTTP/1.1</td></tr>
<tr><td class="e">_SERVER["REQUEST_METHOD"]</td><td class="v">GET</td></tr>
<tr><td class="e">_SERVER["QUERY_STRING"]</td><td class="v"><i>no value</i></td></tr>
<tr><td class="e">_SERVER["REQUEST_URI"]</td><td class="v">test.php</td></tr>
<tr><td class="e">_SERVER["SCRIPT_NAME"]</td><td class="v">test.php</td></tr>
<tr><td class="e">_SERVER["PHP_SELF"]</td><td class="v">test.php</td></tr>
<tr><td class="e">_SERVER["REQUEST_TIME"]</td><td class="v">1483559123</td></tr>
</table><br />
</div></body></html>
&#13;
&#13;
&#13;

请告知

2 个答案:

答案 0 :(得分:0)

改变这个:

RewriteCond "%{HTTPS}" !="on"

对此:

RewriteCond %{HTTPS} off

不要自己用SetEnv设置它,mod_rewrite会处理它。

该重定向位于您的<VirtualHost>端口80中,但是如果进入https我不会看到它是如何被处理的(由于这个原因,甚至不需要检查)因为其他虚拟在这种情况下将处理主机。你确定你没有.htaccess有一些你忽略的规则吗?否则,请发布完整的虚拟主机块。并清除缓存或尝试使用其他浏览器。

答案 1 :(得分:0)

修正..这都是因为配置错误..

ProxyPass / http://localhost/
ProxyPassReverse / http://localhost/

在内部调用端口80 VirtualHost(ServerName是localhost),因此重定向循环。 通过用

替换上述内容来解决这个问题
ProxyPass / ajp://localhost:8409/
ProxyPassReverse / ajp://localhost:8409/