【发布时间】:2014-11-10 08:31:06
【问题描述】:
真的为这件事绞尽脑汁。每当用户在我的网站上请求“http”时,我都需要始终强制使用“https”,但同时我需要代理从 Apache 到 Tomcat 的传递(通过 http)。我无法让这两个部分协同工作。
我在 httpd.conf 中定义了 https 重定向:
<VirtualHost *:80> ServerName myserver.foo.com
Redirect / https://myserver.foo.com/
</VirtualHost>
那么对于代理:
RewriteEngine on
RewriteLog /opt/HTTPServer/logs/rewrite_log
RewriteLogLevel 9
RewriteRule ^/testnew/MyApp(.*)$ http://localhost:8080/test/MyApp$1?product=new [NC,P,QSA]
RewriteRule ^/testold/MyApp(.*)$ http://localhost:8080/test/MyApp$1?product=old [NC,P,QSA]
注意:如果我改用 ProxyPass,这确实确实有效,但我需要能够向请求添加额外的 GET 参数,因此使用 [ P] 标志方法在这里。
所以当我点击以下 URL 时,我会看到 Apache HTTP Server 页面,上面写着“Not Found”。 http://myserver.foo.com/testnew/MyApp/RunReport
在访问日志中,有一个 404
[10/Nov/2014:01:45:21 -0600] "GET /testnew/MyApp/RunReport HTTP/1.1" 404 321
此外,不会将任何内容写入重写日志。
据我了解,RewriteRules 将在重定向之前执行,因此即使上面的 URL 确实有效(我不明白为什么它不起作用),它也不会从 http 重定向到 https。那么我该如何实现呢?
我也尝试过仅使用 RewriteRules:
RewriteEngine on
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301]
RewriteRule ^/testnew/MyApp(.*)$ http://localhost:8080/test/MyApp$1?product=new [NC,P,QSA]
RewriteRule ^/testold/MyApp(.*)$ http://localhost:8080/test/MyApp$1?product=old [NC,P,QSA]
但是,在第一次重定向之后,URL 被转换为包含 https 方案和主机名,因此后续的 RewriteRules 无法匹配。如果我将完整的“https://myserver.foo.com”添加到 RewriteRule,它会匹配,但是完整的 URL 会通过代理转换回 http。
我赢不了!在我看来,这将是一个相当常见的配置。我完全错过了什么吗?我已经看这个太久了。
【问题讨论】:
标签: apache mod-rewrite reverse-proxy