Apache mod_rewrite不适用于FastCGI

时间:2014-04-11 21:40:39

标签: mod-rewrite apache2 fastcgi mod-proxy

我正在尝试使用HHVM配置Apache。作为其中的一部分,我需要配置重写规则。我已经在FastCGI模式下将HHVM作为守护进程启动了。我启用了Apache模块mod_proxymod_proxy_fcgimod_rewrite

首先,没有mod_rewrite,我有这个虚拟主机:

<VirtualHost *:80>
  DocumentRoot /app
  ProxyPass / fcgi://127.0.0.1:9000/app/
</VirtualHost>

我有一个文件/app/foo.php,如下所示:

<?php echo "HELLO\n";

因此我可以使用以下方式访问它:

$ curl http://localhost/foo.php
HELLO

现在,在配置重写规则后:

<VirtualHost *:80>
  DocumentRoot /app
  ProxyPass / fcgi://127.0.0.1:9000/app/

  RewriteEngine on
  RewriteRule ^(.*)$ /foo.php
</VirtualHost>

我期望发生的是现在所有请求都会导致执行foo.php文件,并输出HELLO

然而,发生的情况是我获得了HTTP 403,不仅仅是针对/foo.php的请求,而是针对任何请求:

$ curl http://localhost/foo.php
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /foo.php
on this server.</p>
<hr>
<address>Apache/2.4.6 (Ubuntu) Server at localhost Port 80</address>
</body></html>

$ curl http://localhost/blah
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /blah
on this server.</p>
<hr>
<address>Apache/2.4.6 (Ubuntu) Server at localhost Port 80</address>
</body></html>

Apache错误日志显示:

$ tail -2 /var/log/apache2/error.log 
[Fri Apr 11 21:30:20.645439 2014] [authz_core:error] [pid 5090:tid 140114499983104] [client 127.0.0.1:39056] AH01630: client denied by server configuration: /app/foo.php
[Fri Apr 11 21:30:23.281610 2014] [authz_core:error] [pid 5090:tid 140114616588032] [client 127.0.0.1:39057] AH01630: client denied by server configuration: /app/foo.php

在此之后,我设置了目录访问权限:

<VirtualHost *:80>
  DocumentRoot /app
  ProxyPass / fcgi://127.0.0.1:9000/app/

  <Directory /app>
    Require all granted
  </Directory>

  RewriteEngine on
  RewriteRule ^(.*)$ /foo.php
</VirtualHost>

现在Apache提供普通的/app/foo.php文件:

$ curl http://localhost/blah
<?php

echo "HELLO\n";

也就是说,现在它似乎尊重重写规则,但现在忽略了ProxyPass规则。

如何让它们一起工作?

2 个答案:

答案 0 :(得分:0)

想出这一个:使用ProxyPassMatch,不要打扰mod_rewrite

像这样:

<VirtualHost *:80>
  DocumentRoot /app

  ProxyPassMatch ^.*$ fcgi://127.0.0.1:9000/app/foo.php

  <Directory /app>
    Require all granted
  </Directory>
</VirtualHost>

答案 1 :(得分:0)

我遇到了类似的问题而没有使用mod_rewrite的奢侈。 如果在启用'mod_remoteip'的同时将mod_geoip选项'GeoIPScanProxyHeaders'设置为'On',则mod_geoip和mod_rewrite之间存在冲突。

'GeoIPScanProxyHeaders'是用于获取'mod_geoip'使用的客户端IP地址的设置。事实证明,如果加载'mod_remoteip',则不应在Apache 2.4上启用 mod_geoip选项'GeoIPScanProxyHeaders'。 'mod_remoteip'应该是首选,'mod_geoip'将使用'mod_remoteip'的发现。