apache2 mod_proxy 与 mod_rewrite 结合使用无法按预期工作

apache2 mod_proxy 与 mod_rewrite 结合使用无法按预期工作

我正在尝试结合使用 mod_rewrite 和 mod_proxy 来传递来自不同内部网络服务器的内容(http://webserver.internal.com/foo)通过(假)子目录“foo”(http://example.com/foo)。

使用带有 ProxyPass 指令的 mod_proxy 我已经能够使用 /etc/apache2/mods-enabled/proxy.conf 中的配置来执行此操作

ProxyPass /foo http://webserver.internal.com/foo
ProxyPassReverse /foo http://webserver.internal.com/foo

现在我有一个额外的要求,强制重写所有对动态资源的请求http://example.com到 https:// example.com,同样独立运行也行得通,但不能与上面提到的 ProxyPass 规则一起使用。看来代理配置总是优先于重写规则。

据我的研究表明,在我的重写规则中使用 mod_proxy 应该很简单,只需添加

RewriteCond %{REQUEST_URI} ^/foo RewriteRule ^/foo(.*)http://webserver.internal.com/foo1美元[P]

到我的重写规则,重写的 URI 将自动通过 mod_proxy 传递。我打开了 RewriteLog,LogLevel 为 9,但我无法看到 ^/foo 的匹配和随后执行的 Proxy-RewriteRule。没有任何类似 go-ahead 代理的东西,也没有提到代理,甚至 RewriteLog 中也没有错误条目。

我在 apache2 配置中激活了以下代理模块

proxy 
proxy_connect 
proxy_http

我尝试用另一个示例测试我的 RewriteCondition - 这次匹配 ^/foo 并重定向到 www.google.com

RewriteCond %{REQUEST_URI} ^/foo
RewriteRule ^(.*)$ http://www.google.com/ [R=302,L]

一切按预期运行,RewriteLog 中显示以下结果:

RewriteCond:input='/foo' pattern='^/foo' => 匹配重写'foo' -> 'http://www.google.com/' 明确强制重定向http://www.google.com/ 逃跑http://www.google.com/重定向 重定向至http://www.google.com/[重定向/302]

下面是我在虚拟主机的目录部分中使用的重写配置。

    <Directory /my/app/webroot >
            RewriteEngine On
            RewriteBase /

            #redirect all http requests to https - WORKING
            # incoming request was forwarded as http from ELB
            RewriteCond %{HTTP:X-Forwarded-Proto} =http
            # avoid https rewriting for static files like images, javascripts, files and templates
            # for folders /img, /js, /css, /files and /templates under webroot
            RewriteCond %{REQUEST_URI} !^/(img|js|css|files|templates)
            RewriteRule (.*) https:// example.com%{REQUEST_URI}  [R=301,L]

            # requests to /foo *should* be handled by mod_proxy - NOT WORKING
            RewriteCond %{REQUEST_URI} ^/foo
            RewriteRule ^/foo(.*) http://webserver.internal.com/foo$1 [P]

            #pretty URLS for web-application - WORKING
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
    </Directory>

有问题的机器是一个基本的 debian 服务器安装,带有 apache2 和 php,并托管在弹性负载均衡器后面的亚马逊云中。

如果您读到这里——首先——感谢您的耐心!

如果有人能帮助解决这个问题就太好了。也许这只是我忽略的一点。

提前谢谢了!

答案1

错误的:

RewriteRule ^/foo(.*) http://webserver.internal.com/foo$1 [P]

正确地说:

RewriteRule ^foo(.*)$ http://webserver.internal.com/foo$1 [P,L]

会有问题,写邮件吧。

相关内容