Apache-2.2

apache proxypassreverse ServerName 不起作用

  • February 18, 2015

我有一個二次開發的 wordpress 網站。(Centos 6.6 x86 ,LAMP) 由於網站的規模不斷擴大。(300GB+)。現在我把它分成3個伺服器。

域名:www.example.com

server1  46.192.22.01  /var/www/public_html
                                           /music
                                           /video


server2  46.192.22.02  /var/www/public_html
                                           /article
                                           /photo

server3  172.192.22.03  /var/www/public_html 
                                          /products
                                          /showroom

域 DNS 中已經設置了 3 個 ip。我想讓它們成為一個集群。通過不同的 URL 自定義訪問我的網站,apache 將選擇正確的伺服器來登陸正確的頁面文件。

www.example.com/music/xxx mapping to server with 172.192.22.01
www.example.com/article/xxx mapping to server with 172.192.22.02
www.example.com/products/xxx mapping to server with 172.192.22.03

現在我使用了 apache proxypassreverse。

/etc/httpd/conf/httpd.conf在伺服器 1

ServerName www.example.com:80
<VirtualHost *:80>
   ServerName www.example.com
   ServerAlias www.example.com
   DocumentRoot /var/www/html
   <directory "/var/www/html">
       Options Indexes FollowSymLinks
       AllowOverride all
       Order Deny,Allow
       Deny from all
       Allow from all
   </directory>
   <Proxy *>
       Order deny,allow
       Allow from all
   </Proxy>
   ProxyRequests Off
   ProxyPass /article http://46.192.22.02/article
   ProxyPassReverse /article http://46.192.22.02/article
   ProxyPass /photo http://46.192.22.02/photo
   ProxyPassReverse /photo http://46.192.22.02/photo
   ProxyPass /products http://46.192.22.03/products
   ProxyPassReverse /products http://46.192.22.03/products
   ProxyPass /showroom http://46.192.22.03/showroom
   ProxyPassReverse /showroom http://46.192.22.03/showroom
</VirtualHost>

/etc/httpd/conf/httpd.conf在伺服器 2

ServerName www.example.com:80
<VirtualHost *:80>
   ServerName www.example.com
   ServerAlias www.example.com
   DocumentRoot /var/www/html
   <directory "/var/www/html">
       Options Indexes FollowSymLinks
       AllowOverride all
       Order Deny,Allow
       Deny from all
       Allow from all
   </directory>
   <Proxy *>
       Order deny,allow
       Allow from all
   </Proxy>
   ProxyRequests Off
   ProxyPass /music http://46.192.22.01/music
   ProxyPassReverse /music http://46.192.22.01/music
   ProxyPass /video http://46.192.22.01/video
   ProxyPassReverse /video http://46.192.22.01/video
   ProxyPass /products http://46.192.22.03/products
   ProxyPassReverse /products http://46.192.22.03/products
   ProxyPass /showroom http://46.192.22.03/showroom
   ProxyPassReverse /showroom http://46.192.22.03/showroom
</VirtualHost>

/etc/httpd/conf/httpd.conf在伺服器 3

ServerName www.example.com:80
<VirtualHost *:80>
   ServerName www.example.com
   ServerAlias www.example.com
   DocumentRoot /var/www/html
   <directory "/var/www/html">
       Options Indexes FollowSymLinks
       AllowOverride all
       Order Deny,Allow
       Deny from all
       Allow from all
   </directory>
   <Proxy *>
       Order deny,allow
       Allow from all
   </Proxy>
   ProxyRequests Off
   ProxyPass /music http://46.192.22.01/music
   ProxyPassReverse /music http://46.192.22.01/music
   ProxyPass /video http://46.192.22.01/video
   ProxyPassReverse /video http://46.192.22.01/video
   ProxyPass /article http://46.192.22.02/article
   ProxyPassReverse /article http://46.192.22.02/article
   ProxyPass /photo http://46.192.22.02/photo
   ProxyPassReverse /photo http://46.192.22.02/photo
</VirtualHost>

現在我可以打開我的網站,每個文件夾 www.example.com/music/xxx www.example.com/music/article, www.example.com/music/products,

但是echo $_SERVER["SERVER_NAME"]echo $_SERVER['HTTP_HOST']所有返回的IP地址。httpd.conf 中的 ServerName 似乎不起作用。我在哪裡設置錯誤?

似乎proxy balancer snippet缺少一個。根據此文件,平衡器設置如下所示:

ProxyPass /special-area http://special.example.com smax=5 max=10
ProxyPass / balancer://mycluster/ stickysession=JSESSIONID|jsessionid nofailover=On
<Proxy balancer://mycluster>
   BalancerMember ajp://1.2.3.4:8009
   BalancerMember ajp://1.2.3.5:8009 loadfactor=20
   # Less powerful server, don't send as many requests there,
   BalancerMember ajp://1.2.3.6:8009 loadfactor=5
</Proxy> 

引用自:https://serverfault.com/questions/644614