我有一台使用 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"
此外,我发现了另一件奇怪的事情,它可能会为我的问题提供见解,它被发布这里。
答案1
您可以将 ProxyPass 和 ProxyPassReverse 指令转换为 RewriteRule 指令,并使用 [P] 来代理请求,如下所示:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteRule (.*) ajp://localhost:8009/$1 [P]
答案2
好吧,我终于自己搞明白了。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"
答案3
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule rewrite_module modules/mod_rewrite.so
RewriteEngine On
RewriteRule ^/$ /index.html [L]
# RequestHeader set REMOTE_USER %{LA-U:REMOTE_USER}e
RewriteCond %{REQUEST_URI} !^/fusioncp
ReWriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteRule ^(.*) ajp://localhost:18009$1 [P]