隐藏 URL 中的 tomcat 上下文路径

隐藏 URL 中的 tomcat 上下文路径

我已经为 Tomcat 设置了一个 Apache 反向代理,它可以正常工作,这样我就可以加载我的 URL 并连接到 Tomcat 后端。我现在尝试做的是重写用户在浏览器中看到的 URL。这是我的代码 -

LoadModule      proxy_html_module       modules/mod_proxy_html.so
LoadModule  proxy_ajp_module    modules/mod_proxy_ajp.so

# DON'T TURN ProxyRequests ON!  Bad things will happen
ProxyRequests off

# Enabling mod_proxy_html
ProxyHTMLEnable On
ProxyHTMLLogVerbose On
LogLevel Debug

SSLProxyEngine on

# Setup mod_proxy_ajp load balancer to the 2 happ hosts
<Proxy balancer://happcluster>
BalancerMember ajp://happ01.domain.com:8009 loadfactor=1
BalancerMember ajp://happ02.domain.com:8009 loadfactor=1
ProxySet lbmethod=bytraffic
ProxySet stickysession=JSESSIONID
</Proxy>

# rewrite setup

RewriteEngine   On
RewriteLog      /var/log/httpd/rewrite_log
RewriteLogLevel        8

RewriteCond     %{HTTP_HOST} !^www.* [NC]
RewriteCond     %{HTTP_HOST} ^app\.domain\.com$ [NC]
RewriteCond     %{REQUEST_URI} !^/app1/.*$
RewriteCond     %{REQUEST_URI} !^.*\.ico$ [NC]
RewriteRule     ^(/.*) balancer://happcluster/app$1 [P]

# Necessary to have mod_proxy_html do the rewriting
RequestHeader      unset  Accept-Encoding

# Be prepared to rewrite the HTML/CSS files as they come back
# from Tomcat
SetOutputFilter proxy-html
SetOutputFilter INFLATE;DEFLATE

# Rewrite JavaScript and CSS files in addition to HTML files
ProxyHTMLExtended On

# Output Strict XHTML (add "Legacy" to the end of the line below
# to output Transitional XHTML)
ProxyHTMLDoctype XHTML

# Rewrite HTTP headers and HTML/CSS links for everything else
ProxyPassReverse /app1/ /
ProxyPassReverseCookiePath /app1/ /
ProxyHTMLURLMap /app1/ /

但当我输入http://app.domain.com进入浏览器,URL 被重写为http://app.domain.com/app1/.....

我正在使用 mod_proxy_ajp 将请求负载平衡到 2 个后端 tomcat 服务器,这似乎有效。

有任何想法吗?

编辑

下面是我的重写日志的片段,显示了后端的代理 -

[app.domain.com/sid#2b0c5d2d4ef0][rid#2b0c5d0d73c0/initial] (2) init rewrite engine with requested uri /
[app.domain.com/sid#2b0c5d2d4ef0][rid#2b0c5d0d73c0/initial] (3) applying pattern '^(/.*)' to uri '/'
[app.domain.com/sid#2b0c5d2d4ef0][rid#2b0c5d0d73c0/initial] (4) RewriteCond: input='app.domain.com' pattern='!^www.*' [NC] => matched
[app.domain.com/sid#2b0c5d2d4ef0][rid#2b0c5d0d73c0/initial] (4) RewriteCond: input='app.domain.com' pattern='^app\.domain\.com$' [NC] => matched
[app.domain.com/sid#2b0c5d2d4ef0][rid#2b0c5d0d73c0/initial] (4) RewriteCond: input='/' pattern='!^/app1/.*$' => matched
[app.domain.com/sid#2b0c5d2d4ef0][rid#2b0c5d0d73c0/initial] (4) RewriteCond: input='/' pattern='!^.*\.ico$' [NC] => matched
[app.domain.com/sid#2b0c5d2d4ef0][rid#2b0c5d0d73c0/initial] (2) rewrite '/' -> 'balancer://happcluster/app1/'
[app.domain.com/sid#2b0c5d2d4ef0][rid#2b0c5d0d73c0/initial] (2) forcing proxy-throughput with balancer://happcluster/app1/
[app.domain.com/sid#2b0c5d2d4ef0][rid#2b0c5d0d73c0/initial] (1) go-ahead with proxy request proxy:balancer://happcluster/app1/ [OK]
[app.domain.com/sid#2b0c5d2d4ef0][rid#2b0c5d0d53b0/initial] (2) init rewrite engine with requested uri /app1/login.jsp;jsessionid=F6D9765F6A737DB373D9C3AA60211B68.worker1
[app.domain.com/sid#2b0c5d2d4ef0][rid#2b0c5d0d53b0/initial] (3) applying pattern '^(/.*)' to uri '/app1/login.jsp;jsessionid=F6D9765F6A737DB373D9C3AA60211B68.worker1'
[app.domain.com/sid#2b0c5d2d4ef0][rid#2b0c5d0d53b0/initial] (4) RewriteCond: input='app.domain.com' pattern='!^www.*' [NC] => matched
[app.domain.com/sid#2b0c5d2d4ef0][rid#2b0c5d0d53b0/initial] (4) RewriteCond: input='app.domain.com' pattern='^app\.domain\.com$' [NC] => matched
[app.domain.com/sid#2b0c5d2d4ef0][rid#2b0c5d0d53b0/initial] (4) RewriteCond: input='/app1/login.jsp;jsessionid=F6D9765F6A737DB373D9C3AA60211B68.worker1' pattern='!^/app1/.*$' => not-matched
[app.domain.com/sid#2b0c5d2d4ef0][rid#2b0c5d0d53b0/initial] (3) applying pattern '^(/.*)' to uri '/app1/login.jsp;jsessionid=F6D9765F6A737DB373D9C3AA60211B68.worker1'
[app.domain.com/sid#2b0c5d2d4ef0][rid#2b0c5d0d53b0/initial] (4) RewriteCond: input='app.domain.com' pattern='!^www.*' [NC] => matched
[app.domain.com/sid#2b0c5d2d4ef0][rid#2b0c5d0d53b0/initial] (4) RewriteCond: input='app.domain.com' pattern='^app\.domain\.com$' [NC] => matched
[app.domain.com/sid#2b0c5d2d4ef0][rid#2b0c5d0d53b0/initial] (4) RewriteCond: input='/app1/login.jsp;jsessionid=F6D9765F6A737DB373D9C3AA60211B68.worker1' pattern='!^/app1/.*$' => not-matched
[app.domain.com/sid#2b0c5d2d4ef0][rid#2b0c5d0d53b0/initial] (1) pass through /app1/login.jsp;jsessionid=F6D9765F6A737DB373D9C3AA60211B68.worker1

以下是我的 access_log 中的一个片段 -

"GET /app1/login.jsp HTTP/1.1" 200 607
"GET / HTTP/1.1" 302 -
"GET /app1/login.jsp;jsessionid=F6D9765F6A737DB373D9C3AA60211B68.worker1 HTTP/1.1" 200 607

答案1

这种做法注定会失败,因为 Tomcat 还会在其生成的 HTML 中嵌入自相关 URL。还有一个第三方模块可以修复这个问题,但即使使用重写规则通常也表明你做错了。唯一真正有效的技术是在 Apache 和 Tomcat 上使用相同的 URL。在这种情况下,只需找出 Tomcat 上下文 URL 是什么,然后将它们全部 ProxyPass。

我同意 Shane Madden 的观点,你应该研究 mod_proxy_ajp 而不是 mod_jk。它的配置要容易得多,功能完全相同,而且是 Apache 的一部分,而不是单独的部分。

相关内容