使用 Apache 代理的 Wordpress 进行无限重定向

使用 Apache 代理的 Wordpress 进行无限重定向

我们正在尝试在公共服务器上设置 Apache,以使用另一台服务器上的 WordPress 代理对 Apache 的请求,但我们得到了无限的重定向循环。

以 www.example.com/about 开头的请求应代理到内部服务器 wp.example.corp。例如

www.example.com/about/about/team => proxy => wp.example.corp/about/team

更复杂的是,我们的 WordPress 安装在名为“wp”的子目录中。因此,在 WordPress 服务器上,我们有以下设置:

以下是公共服务器上 Apache 配置的一些相关部分:

<VirtualHost *:80>
  #...
  ProxyPreserveHost On
  ProxyRequests off
  ProxyErrorOverride On
  <Proxy *>
    Order allow,deny
    Allow from all
  </Proxy>


  RewriteCond %{REQUEST_URI} ^/about/?$
  RewriteRule ^/about/?$ http://wp.example.corp/ [P,L]

  RewriteCond %{REQUEST_URI} ^/about/.+
  RewriteRule ^/about/(.*) http://wp.example.corp/$1 [P,L]

  #...
</VirtualHost>

WordPress 服务器上的 Apache 配置非常简单:

<VirtualHost *:80>
  #...
  DocumentRoot /var/lib/publicpages

  <Directory /var/lib/publicpages/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride FileInfo Indexes Limit Options
  </Directory>
</VirtualHost>

以下是 WordPress 服务器根目录中的 .htaccess 文件:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

将 LogLevel 设置为调试后,我可以看到请求到达公共服务器并使用 301 重定向进行响应:

x.x.x.x, y.y.y.y (y.y.y.y) - - [25/Jan/2012:16:44:28 -0600] "GET /about/about/team/ HTTP/1.1" 301 20 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.106 Safari/535.2"
x.x.x.x, y.y.y.y (y.y.y.y) - - [25/Jan/2012:16:44:28 -0600] "GET /about/about/team/ HTTP/1.1" 301 20 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.106 Safari/535.2"

我可以看到相同的请求到达了 WordPress 服务器,该服务器以 301 重定向进行响应:

z.z.z.z - - [25/Jan/2012:16:44:28 -0600] "GET /about/team/ HTTP/1.1" 301 20 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.106 Safari/535.2"
z.z.z.z - - [25/Jan/2012:16:44:29 -0600] "GET /about/team/ HTTP/1.1" 301 20 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.106 Safari/535.2"

我还在 WordPress 服务器上启用了完整的重写日志功能,并看到以下内容一遍又一遍重复:

x.x.x.x - - [26/Jan/2012:09:54:04 --0600] [www.example.com/sid#80e06c20][rid#80fa4200/initial] (3) [perdir /var/lib/publicpages/] add path info postfix: /var/lib/publicpages/about -> /var/lib/publicpages/about/team
x.x.x.x - - [26/Jan/2012:09:54:04 --0600] [www.example.com/sid#80e06c20][rid#80fa4200/initial] (3) [perdir /var/lib/publicpages/] strip per-dir prefix: /var/lib/publicpages/about/team -> about/team
x.x.x.x - - [26/Jan/2012:09:54:04 --0600] [www.example.com/sid#80e06c20][rid#80fa4200/initial] (3) [perdir /var/lib/publicpages/] applying pattern '^index\.php$' to uri 'about/team'
x.x.x.x - - [26/Jan/2012:09:54:04 --0600] [www.example.com/sid#80e06c20][rid#80fa4200/initial] (3) [perdir /var/lib/publicpages/] add path info postfix: /var/lib/publicpages/about -> /var/lib/publicpages/about/team
x.x.x.x - - [26/Jan/2012:09:54:04 --0600] [www.example.com/sid#80e06c20][rid#80fa4200/initial] (3) [perdir /var/lib/publicpages/] strip per-dir prefix: /var/lib/publicpages/about/team -> about/team
x.x.x.x - - [26/Jan/2012:09:54:04 --0600] [www.example.com/sid#80e06c20][rid#80fa4200/initial] (3) [perdir /var/lib/publicpages/] applying pattern '.' to uri 'about/team'
x.x.x.x - - [26/Jan/2012:09:54:04 --0600] [www.example.com/sid#80e06c20][rid#80fa4200/initial] (4) [perdir /var/lib/publicpages/] RewriteCond: input='/var/lib/publicpages/about' pattern='!-f' => matched
x.x.x.x - - [26/Jan/2012:09:54:04 --0600] [www.example.com/sid#80e06c20][rid#80fa4200/initial] (4) [perdir /var/lib/publicpages/] RewriteCond: input='/var/lib/publicpages/about' pattern='!-d' => matched
x.x.x.x - - [26/Jan/2012:09:54:04 --0600] [www.example.com/sid#80e06c20][rid#80fa4200/initial] (2) [perdir /var/lib/publicpages/] rewrite 'about/team' -> 'index.php'
x.x.x.x - - [26/Jan/2012:09:54:04 --0600] [www.example.com/sid#80e06c20][rid#80fa4200/initial] (3) [perdir /var/lib/publicpages/] add per-dir prefix: index.php -> /var/lib/publicpages/index.php
x.x.x.x - - [26/Jan/2012:09:54:04 --0600] [www.example.com/sid#80e06c20][rid#80fa4200/initial] (2) [perdir /var/lib/publicpages/] trying to replace prefix /var/lib/publicpages/ with /
x.x.x.x - - [26/Jan/2012:09:54:04 --0600] [www.example.com/sid#80e06c20][rid#80fa4200/initial] (5) strip matching prefix: /var/lib/publicpages/index.php -> index.php
x.x.x.x - - [26/Jan/2012:09:54:04 --0600] [www.example.com/sid#80e06c20][rid#80fa4200/initial] (4) add subst prefix: index.php -> /index.php
x.x.x.x - - [26/Jan/2012:09:54:04 --0600] [www.example.com/sid#80e06c20][rid#80fa4200/initial] (1) [perdir /var/lib/publicpages/] internal redirect with /index.php [INTERNAL REDIRECT]

我错过了什么?

答案1

我们放弃了,只是让两个 Apache 服务器中的 URL/路径匹配。例如

www.example.com/about/about/team => proxy => wp.example.corp/about/about/team

虽然这并不完全使固定这个问题,它有效地回避了问题并消除了冗余的 301 重定向。

答案2

您可以使用 ProxyPass 来执行此操作,而不是使用重写规则。

ProxyPreserveHost On
ProxyPass /about/ http://wp.example.corp/
ProxyPassReverse /about/ http://wp.example.corp/

相关内容