Apache 2.4 不使用适用于 2.2 的规则通过 mod_rewrite 进行代理

Posted

技术标签:

【中文标题】Apache 2.4 不使用适用于 2.2 的规则通过 mod_rewrite 进行代理【英文标题】:Apache 2.4 not proxying via mod_rewrite using rules that work on 2.2 【发布时间】:2015-12-20 12:23:49 【问题描述】:

我在 Ubuntu 14.04.2 LTS 机器上安装了 Apache 2.4 并添加了我的网站,除了某些图像的代理重写规则外,一切正常。我设置的规则如下:

RewriteEngine on
RewriteRule /images/here/(.*)$ http://my.site.com/images/here/$1 [P,QSA,L]

但是当我尝试访问文件时说:

http://other.site.com/images/here/image.jpg

我在浏览器中收到 404 错误。我打开了重写日志记录,在日志中它似乎与规则匹配并正确应用了代理:

[Wed Sep 23 09:47:16.387531 2015] [rewrite:trace2] [pid 29527:tid 139709285484288] mod_rewrite.c(468): [client 10.0.5.61:51099] 10.0.5.61 - - [other.site.com/sid#7f109fe93400][rid#7f109fe070a0/initial] init rewrite engine with requested uri /images/here/image.jpg
[Wed Sep 23 09:47:16.387700 2015] [rewrite:trace3] [pid 29527:tid 139709285484288] mod_rewrite.c(468): [client 10.0.5.61:51099] 10.0.5.61 - - [other.site.com/sid#7f109fe93400][rid#7f109fe070a0/initial] applying pattern '/images/here/(.*)$' to uri '/images/here/image.jpg'
[Wed Sep 23 09:47:16.387736 2015] [rewrite:trace2] [pid 29527:tid 139709285484288] mod_rewrite.c(468): [client 10.0.5.61:51099] 10.0.5.61 - - [other.site.com/sid#7f109fe93400][rid#7f109fe070a0/initial] rewrite '/images/here/image.jpg' -> 'http://my.site.com/images/here/image.jpg'
[Wed Sep 23 09:47:16.387748 2015] [rewrite:trace2] [pid 29527:tid 139709285484288] mod_rewrite.c(468): [client 10.0.5.61:51099] 10.0.5.61 - - [other.site.com/sid#7f109fe93400][rid#7f109fe070a0/initial] forcing proxy-throughput with http://my.site.com/images/here/image.jpg
[Wed Sep 23 09:47:16.387757 2015] [rewrite:trace1] [pid 29527:tid 139709285484288] mod_rewrite.c(468): [client 10.0.5.61:51099] 10.0.5.61 - - [other.site.com/sid#7f109fe93400][rid#7f109fe070a0/initial] go-ahead with proxy request proxy:http://my.site.com/images/here/image.jpg [OK]

我检查了 mod_rewrite、mod_proxy 和 mod_proxy_http 是否都已启用。我还尝试使用服务器上的curlwget 访问图像,它可以使图像正常,所以这不是服务器无法访问图像的网络问题。

同样的规则适用于 CentOS 6 和 7 机器上的 Apache 2.2。

我现在不知道为什么它不起作用。也许 2.4 中还需要其他东西?

谢谢。

【问题讨论】:

【参考方案1】:

你有两个小问题:

Apache2.4 需要不同的字符串起始锚点:

RewriteRule /images/here/

应该是:

RewriteRule ^images/here/

您的标志可能会导致问题:

[P] 已经隐含了 [L],参见http://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_p [QSA] 可能会导致您的代理出现问题,除非您绝对需要,否则请完全删除它。

【讨论】:

感谢您的回复,但这仍然有效。我唯一可以开始工作的是删除所有标志,然后将其 302 转换为图像,这对我来说是可行的,但是我真的很想了解为什么代理不起作用。

以上是关于Apache 2.4 不使用适用于 2.2 的规则通过 mod_rewrite 进行代理的主要内容,如果未能解决你的问题,请参考以下文章

Apache 升级 2.2 -> 2.4 问题

mod_ssl 2.8.31 是不是适用于 Apache 2.2?

Apache重写规则仅适用于新连接

htaccess 文件在 apache 2.2 中有效,但在 apache 2.4 中无效 - aws beanstalk 托管

apache编译安装 httpd 2.2 httpd 2.4

apache mod_rewrite适用于任何可能性的规则