无法同时使用Apache强制“https”和反向代理

无法同时使用Apache强制“https”和反向代理,apache,mod-rewrite,reverse-proxy,Apache,Mod Rewrite,Reverse Proxy,这件事让我绞尽脑汁。每当用户在我的站点上请求“http”时,我需要始终强制使用“https”,但同时我需要通过http代理从Apache传递到Tomcat。我不能让这两件事同时进行 我在httpd.conf中定义了https重定向: <VirtualHost *:80> ServerName myserver.foo.com Redirect / https://myserver.foo.com/ </VirtualHost> 注意:如果我改用ProxyPass

这件事让我绞尽脑汁。每当用户在我的站点上请求“http”时,我需要始终强制使用“https”,但同时我需要通过http代理从Apache传递到Tomcat。我不能让这两件事同时进行

我在httpd.conf中定义了https重定向:

<VirtualHost *:80> ServerName myserver.foo.com
    Redirect / https://myserver.foo.com/
</VirtualHost>
注意:如果我改用ProxyPass,这实际上是可行的,但是我需要能够向请求添加额外的GET参数,因此这里使用[p]标志方法

因此,当我点击下面的URL时,我得到了ApacheHTTP服务器页面,该页面的内容为NotFound。

在访问日志中,有一个404

[10/Nov/2014:01:45:21 -0600] "GET /testnew/MyApp/RunReport HTTP/1.1" 404 321
此外,重写日志中不会写入任何内容

据我所知,重写规则将在重定向之前执行,因此,即使上面的URL确实有效,但我不理解为什么无效,它也不会从http重定向到https。那么我如何才能做到这一点呢

我还尝试了只使用重写规则:

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方案和主机名,因此后续的重写规则无法匹配。如果我将完整URL添加到RewriteRule,它将匹配,但是完整URL将通过代理转换回http


我赢不了!在我看来,这将是一种相当常见的配置。我完全错过了什么吗?我已经看得太久了。

我可以通过将代理重写规则移动到*:443 VirtualHost下,并将http->https规则保留在全局级别,即

Listen 443
<VirtualHost *:443>
    SSLEnable
    SSLClientAuth None
    RewriteEngine On
    RewriteLog /opt/HTTPServer/logs/rewrite_log-443
    RewriteLogLevel 9

    RewriteRule ^/testnew/MyApp(.*)$  http://localhost:8080/test/MyApp$1?product=new [NC,P,QSA]

</VirtualHost>

...
...

RewriteEngine on
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$  https://%{HTTP_HOST}$1  [R=301]
现在工作得很好

Listen 443
<VirtualHost *:443>
    SSLEnable
    SSLClientAuth None
    RewriteEngine On
    RewriteLog /opt/HTTPServer/logs/rewrite_log-443
    RewriteLogLevel 9

    RewriteRule ^/testnew/MyApp(.*)$  http://localhost:8080/test/MyApp$1?product=new [NC,P,QSA]

</VirtualHost>

...
...

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