Apache代理子域根请求

4

描述

  • 内部Tomcat服务器侦听8080端口上的Web应用程序:

    "http://internal:8080/foo-webservice/"
    "http://internal:8080/foo-website/"

  • 外部Apache服务器代理子域的请求:

    "http://foo.domain.com/"

  • 任何子域根目录的请求将被代理到Tomcat上的foo-website Web应用程序。

  • 其他请求将被代理到适当的路径/ Web应用程序。

用例A

  • 请求:
    "http://foo.domain.com/index.html"

  • 代理到:
    "http://internal:8080/foo-website/index.html"

用例B

  • 请求:
    "http://foo.domain.com/webservice/listener.html?param1=foo&param2=bar"

  • 代理到:
    "http://internal:8080/foo-webservice/listener.html?param1=foo&param2=bar"

VirtualHost定义

  • Current virtual host definition which satisfies Use Case B:

    <VirtualHost *:80>
        ServerName foo.domain.com
    
        ProxyRequests Off
    
        <Proxy *>
            Order deny,allow
            Allow from all
        </Proxy>
    
        ErrorLog /var/log/apache2/foo_error.log
        LogLevel warn
        CustomLog /var/log/apache2/foo_access.log combined
    
        # RewriteRules
        # ?
    
        # ProxyPass
        ProxyPreserveHost On
        ProxyPass        / http://internal:8080/
        ProxyPassReverse / http://internal:8080/
    </VirtualHost>
    

尝试 1

    # RewriteRules
    RewriteEngine On
    RewriteRule ^/(.*) http://internal:8080/foo-website/$1 [P]
  • 使用案例A已得到满足
  • 使用案例B失败

尝试2

    # RewriteRules
    RewriteEngine On
    RewriteRule ^/$ http://internal:8080/foo-website/$1 [P]
  • 满足使用案例B
  • 使用案例A不完全满足
  • 在foo-website中加载了index.html,但js、img或css文件夹中的任何文件都没有被加载。
3个回答

3

ProxyPass规则按顺序匹配

 ProxyPass        /webservice/ http://internal:8080/foo-webservice/
 ProxyPassReverse /webservice/ http://internal:8080/foo-webservice/

 ProxyPass        /website/ http://internal:8080/foo-website/
 ProxyPassReverse /website/ http://internal:8080/foo-website/

 ProxyPass        / http://internal:8080/foo-website/
 ProxyPassReverse / http://internal:8080/foo-website/

没有重写规则,这不够好吗?

谢谢,我在进行重写时陷入了困境,但其实不必这样做。 - Gordo

0

我认为你需要使用第一次尝试,但在每个RewriteRule指令的方括号末尾包含QSA(查询字符串附加)标志。


我尝试过: RewriteRule ^/(.*) http://internal:8080/foo-website/$1 [QSA,P] 但结果仍然相同。 - Gordo

0
我认为尝试2的问题(js、img或css文件夹中没有任何文件被映射)表明我的方法是错误的。
现在我的解决方案是将任何请求重定向到根目录,到foo-website网站应用程序。 ServerName foo.domain.com
ProxyRequests Off
Order deny,allow Allow from all ErrorLog /var/log/apache2/foo_error.log LogLevel warn CustomLog /var/log/apache2/foo_access.log combined
# RewriteRules RewriteEngine On RewriteRule ^/$ /foo-website/ [R]
# ProxyPass ProxyPreserveHost On ProxyPass / http://internal:8080/ ProxyPassReverse / http://internal:8080/ 这不是我最初想要的,但我认为这是解决办法。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接