反向代理中的Apache URL重写

Jer*_*och 15 mod-rewrite web-server reverse-proxy mod-proxy apache-2.2

我在 Karaf 托管的应用程序前部署 Apache(Apache 和 Karaf 在不同的服务器上)。我希望 Apache 作为反向代理运行并隐藏部分 URL。

直接从应用服务器获取应用登录页面的 URL 是http://app-server:8181/jellyfish. 页面由在 Karaf 中运行的 Jetty 实例提供服务。当然,这种行为通常会被防火墙阻止,除了反向代理服务器。

在关闭防火墙的情况下,如果您点击此 URL,则 Jetty 会加载登录页面。浏览器的地址栏正确更改为http://app-server:8181/jellyfish/login?0,一切正常。

我想要的是http://web-server(即从根)映射到应用程序服务器上的 Jetty,应用程序的名称 ( jellyfish) 被抑制。例如,浏览器将更改为显示http://web-server/login?0在地址栏中,并且所有后续 URL 和内容都将使用网络服务器的域提供服务,而不会出现jellyfish混乱。

我可以使用以下配置(代码段)让 Apache 作为简单的反向代理运行:-

ProxyPass /jellyfish http://app-server:8181/jellyfish
ProxyPassReverse / http://app-server:8181/
Run Code Online (Sandbox Code Playgroud)

...但这需要浏览器的 URL 包含jellyfish并转到根 URL ( http://web-server) 会给出 404 Not Found。

我花了很多时间尝试使用mod_rewrite和不使用它的[P]标志来解决这个问题,但没有成功。然后我尝试了该ProxyPassMatch指令,但我似乎也无法完全正确。

这是当前配置,已加载到/etc/apache2/sites-available/Web 服务器上。请注意,有一个本地托管的图像目录。我还保留了mod_rewrite 代理漏洞利用保护,并抑制了一些mod_security会产生误报的规则。

<VirtualHost *:80>
    ServerAdmin admin@drummer-server
    ServerName drummer-server

    ErrorLog ${APACHE_LOG_DIR}/error.log
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /images/ "/var/www/images/"

    RewriteEngine On
    RewriteCond  %{REQUEST_URI}  !^$
    RewriteCond  %{REQUEST_URI}  !^/
    RewriteRule  .*              -    [R=400,L]

    ProxyPass /images !

    ProxyPassMatch ^/(.*) http://granny-server:8181/jellyfish/$1
    ProxyPassReverse / http://granny-server:8181/jellyfish

    ProxyPreserveHost On

    SecRuleRemoveById 981059 981060

    <Directory "/var/www/images">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>

</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

如果我转到http://web-server,我会被重定向到http://web-server/jellyfish/home404,并抱怨试图访问/jellyfish/jellyfish/home- 注意浏览器的地址栏不包含 double /jellyfish

HTTP ERROR 404

Problem accessing /jellyfish/jellyfish/home. Reason:

    Not Found
Run Code Online (Sandbox Code Playgroud)

而且,如果我去http://web-server/login,我会被重定向到,http://web-server/jellyfish/login?0但这会给出 404,并抱怨试图访问/jellyfish/jellyfish/login.

HTTP ERROR 404

Problem accessing /jellyfish/jellyfish/login. Reason:

    Not Found
Run Code Online (Sandbox Code Playgroud)

所以,我猜我以某种方式通过了规则两次。home对于第一个示例中的 URL 位来自何处,我也有点困惑。

有人能指出我正确的方向吗?

谢谢,J。

Jer*_*och 11

这就是我让它工作的方式。除了根据我对原始问题的评论所做的更改之外,我还需要从添加尾部斜杠的规则中排除.js和排除.css

<VirtualHost *:80>
    ServerAdmin admin@localhost
    ServerName mydomain.com
    ServerAlias www.mydomain.com

    ErrorLog ${APACHE_LOG_DIR}/error.log
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined

    RewriteLog ${APACHE_LOG_DIR}/rewrite.log
    RewriteLogLevel 9

    Alias /images/ "/var/www/images/"

    RewriteEngine On

    # rewrite rule to prevent proxy exploit
    RewriteCond  %{REQUEST_URI}  !^$
    RewriteCond  %{REQUEST_URI}  !^/
    RewriteRule  .*              -    [R=400,L]

    # consolidate non-www requests onto the www subdomain
    RewriteCond  %{HTTP_HOST}    ^yourdomain\.com$
    RewriteRule  ^(.*)           http://www.yourdomain.com/$1  [R=301,L]

    # Add a trailing slash to the URL (ignoring images, CSS and JavaScript)
    RewriteCond  %{REQUEST_URI}  !^/(images)(.*)$
    RewriteCond  %{REQUEST_URI}  !^/(.*)(.js|.css)$
    RewriteCond  %{REQUEST_URI}  !(.*)/$
    RewriteRule  ^(.*)$          http://%{HTTP_HOST}$1/ [R=301,L]

    # proxy to the Jellyfish server (ignoring images)
    RewriteCond  %{REQUEST_URI}  !^/(images)(.*)$
    RewriteRule  ^(/.*)$         http://app-server:8181/jellyfish$1  [P]
    ProxyPassReverse  /          http://app-server:8181/jellyfish/

    # suppress mod_security rules that were giving false positives
    SecRuleRemoveById 981059 981060

    <Directory "/var/www/images">
            Options Indexes MultiViews FollowSymLinks
            AllowOverride None
            Order allow,deny
            Allow from all
    </Directory>

</VirtualHost>
Run Code Online (Sandbox Code Playgroud)