【问题标题】:Apache mod_rewrite does not work with FastCGIApache mod_rewrite 不适用于 FastCGI
【发布时间】:2014-05-26 05:27:19
【问题描述】:

我正在尝试使用 HHVM 配置 Apache。作为其中的一部分,我需要配置重写规则。我已将 HHVM 作为 FastCGI 模式下的守护进程启动。我已启用 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 规则。

如何让它们一起工作?

【问题讨论】:

    标签: mod-rewrite apache2 fastcgi mod-proxy


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      像这样:

      <VirtualHost *:80>
        DocumentRoot /app
      
        ProxyPassMatch ^.*$ fcgi://127.0.0.1:9000/app/foo.php
      
        <Directory /app>
          Require all granted
        </Directory>
      </VirtualHost>
      

      【讨论】:

        猜你喜欢
        • 2011-07-04
        • 2010-11-09
        • 2013-07-25
        • 1970-01-01
        • 2016-04-21
        • 1970-01-01
        • 1970-01-01
        • 2012-09-30
        • 2013-11-20
        相关资源
        最近更新 更多