簡體   English   中英

無法同時使用Apache強制執行“ https”和反向代理

[英]Can't force 'https' and reverse proxy with Apache at the same time

真讓我為之震驚。 每當用戶在我的站點上請求“ 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。

我贏不了! 在我看來,這似乎是相當普遍的配置。 我完全錯過了什么嗎? 我已經看了太久了。

通過將代理RewriteRules移動到*: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]

現在工作精美。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM