【问题标题】:Trouble with serving local content while using RewriteRule [P]使用 RewriteRule 时无法提供本地内容 [P]
【发布时间】:2013-09-13 19:11:58
【问题描述】:

我正在尝试编写将在新服务器上提供文件的规则,同时将不存在的文件代理到旧服务器。代理规则工作正常,将丢失的文件和目录发送到旧服务器。

我遇到的问题是 new-server.example.com/blah(/blah/ 存在于新服务器上)被传递到代理规则并路由到旧服务器。

尝试跳过的原因是因为 new-server.example.com/blah 落入“代理部分”并通过了 new-server.example.com/blah/index.php。新服务器上不存在 Index.php,但 index.html 存在。 “代理部分”不遵循 DirectoryIndex 中的所有索引文件可能性,它只查看第一个。

所以我最终得到错误消息,“在此服务器上找不到请求的 URL /blah/index.php。”

如何编写才能使其真正起作用? -- 谢谢。

RewriteEngine On
# skip, proxy section
RewriteCond /var/www/html%{REQUEST_FILENAME}     -f  [OR]
RewriteCond /var/www/html%{REQUEST_FILENAME}     -d
RewriteRule .? - [S=4]

# proxy section
RewriteCond /var/www/html%{REQUEST_FILENAME}       !-f
RewriteCond /var/www/html%{REQUEST_FILENAME}       !-d
RewriteRule ^/(.*) http://old-server.example.com/$1 [P]
ProxyPassReverse / http://old-server.example.com/


# rewrite log
pass through /index.php
init rewrite engine with requested uri /
applying pattern '.?' to uri '/'
RewriteCond: input='/var/www/html/' pattern='-f' => not-matched
RewriteCond: input='/var/www/html/' pattern='-d' => matched
pass through /
init rewrite engine with requested uri /index.php
applying pattern '.?' to uri '/index.php'
RewriteCond: input='/var/www/html/index.php' pattern='-f' => matched
pass through /index.php
init rewrite engine with requested uri /blah/
applying pattern '.?' to uri '/blah/'
RewriteCond: input='/var/www/html/blah/' pattern='-f' => not-matched
RewriteCond: input='/var/www/html/blah/' pattern='-d' => matched
pass through /blah/
init rewrite engine with requested uri /blah/index.php
applying pattern '.?' to uri '/blah/index.php'
RewriteCond: input='/var/www/html/blah/index.php' pattern='-f' =>    not-matched
RewriteCond: input='/var/www/html/blah/index.php' pattern='-d' => not-matched
applying pattern '^/(.*)' to uri '/blah/index.php'
RewriteCond: input='/var/www/html/blah/index.php' pattern='!-f' => matched
RewriteCond: input='/var/www/html/blah/index.php' pattern='!-d' => matched
rewrite '/blah/index.php' -> 'http://old-server.example.com/blah/index.php'
forcing proxy-throughput with http://old-server.example.com/blah/index.php
go-ahead with proxy request proxy:http://old-server.example.com/blah/index.php [OK]

【问题讨论】:

    标签: apache mod-rewrite reverse-proxy


    【解决方案1】:

    这是 Apache 自 2.0 版以来的工作方式:它收到一个请求,通过所有 RewriteRules 运行该请求,然后如果生成的请求是一个目录,它会尝试查看第一个 DirectoryIndex 是否可以工作,所以它通过 RewriteRules 运行它并尝试提供结果。如果第一个 DirectoryIndex 条目不起作用(即它到达 Apache 将提供 404 的点),则服务器再次尝试下一个条目,依此类推。显然,您的问题是代理规则锁定不存在的第一个 DirectoryIndex 值并尝试通过反向代理发送它。

    我还没有找到在服务器范围内修复此行为的通用方法,但有一种解决方法。使用 httpd.conf 文件中的 <Location> 部分或 .htaccess 文件并覆盖该位置的 DirectoryIndex

    在您的情况下,创建文件 /var/www/html/blah/.htaccess 并将以下内容放入其中:

    DirectoryIndex index.html
    

    【讨论】:

      猜你喜欢
      • 2020-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多