Apache-2.2
使用 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 伺服器上,我們有以下設置:
- WordPress地址:http ://www.example.com/about/wp
- 網站地址:http ://www.example.com/about
以下是公共伺服器上 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 伺服器上啟用了完整的 Rewrite 日誌記錄,並一遍又一遍地看到以下內容:
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]
我錯過了什麼?
我們放棄了,只是讓兩個 Apache 伺服器中的 URL/路徑匹配。例如
www.example.com/about/about/team => proxy => wp.example.corp/about/about/team
雖然這並不能完全解決問題,但它有效地迴避了問題並消除了多餘的 301 重定向。
您可以使用 ProxyPass 來執行此操作,而不是使用重寫規則。
ProxyPreserveHost On ProxyPass /about/ http://wp.example.corp/ ProxyPassReverse /about/ http://wp.example.corp/