Apache-2.2

為什麼當 AJP 為我對 Tomcat 的所有請求提供服務時,一些重寫規則會失敗?

  • January 25, 2015

我有一個使用 mod_proxy_ajp 的 Apache 伺服器讓 Jboss/Tomcat5.5 處理所有請求。這是我配置 Apache 2.2.17 的方式,並且在大多數情況下,它可以工作:

# Proxy pass all work to Tomcat, mod_jk 1.2.31
<Location />
   # ProxyPass / http://localhost:8080/
   ProxyPass ajp://localhost:8009/
   ProxyPassReverse ajp://localhost:8009/
</Location>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
#  and , to handle the redirect of the root dir
Redirect permanent / https://%{HTTP_HOST}%/myapp

不幸的是,雖然啟用了 ProxyPass,但除了上述規則之外,我無法讓任何 mod_rewrite 規則起作用。我該如何處理這種情況?

我正在嘗試創建類似於此 RedirectMatch 規則的重寫規則(僅當我關閉 ProxyPass 時才有效):

RedirectMatch ^/(?i)myagency   "/myapp?agency=MyAgency%20LA"

此外,我發現的另一件奇怪的事情可能會為我的問題提供見解,發佈在這裡

好的,我終於自己弄清楚了。AJP 協議的工作方式與 HTTP 類型規則不同,因此它們不會混合使用。為了解決這個問題,我不得不停止將所有內容重定向到 AJP,而只使用 AJP 重定向應用程序。這是答案:

# don't ProxyPass through the "/" dir location,
# since it breaks the mod_rewrite rules
<Location /myapp>
   ProxyPass ajp://localhost:8009/myapp
   ProxyPassReverse ajp://localhost:8009/myapp
</Location>
RewriteEngine On
# rule to redirect from http to https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
# rule to redirect /  to the app context since nothing is served at /
Redirect /  /myapp?name=
# supplemental rules to handle URI shortcuts
RedirectMatch ^/(?i)name1   /myapp?name=NameOne
RedirectMatch ^/(?i)name2   "/myapp?name=Name%20Two"

您可以將 ProxyPass 和 ProxyPassReverse 指令轉換為 RewriteRule 指令,使用

$$ P $$代理請求,如下所示:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteRule (.*) ajp://localhost:8009/$1 [P]

引用自:https://serverfault.com/questions/264057