apache如何開啟反向代理?這篇完整教你如何配置
什麼是反向代理?
伺服器根據客戶端的請求,從其關聯的一組或多組後端伺服器(如Web伺服器)上獲取資源,然後再將這些資源返回給客戶端,客戶端只會得知反向代理的IP地址,而不知道在代理伺服器後面的伺服器簇的存在。
反向代理的特點:反向代理是供很多客戶端都通過它間接訪問不同後端伺服器上的資源,而不需要知道這些後端伺服器的存在,而以為所有資源都來自於這個反向代理伺服器。
下面就跟大家分享下apache下各種反向代理的配置規則
apache的反向代理是通過proxy模組來實現的,因此,在配置代理前,需要在apache伺服器內把proxy的一些相關模組載入進來,否則代理配置是無效的。
這些模組的載入也很簡單,找到apache的配置檔案 httpd.conf (apache的安裝目錄/conf/httpd.conf),在httpd.conf中分別找到下面3行內容,把它們前面的‘#’去掉,然後儲存。
1 2 3 |
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
Include conf/extra/httpd-vhosts.conf
|
接下來就可以配置反向代理了。
www.test1.com(代理規則配置站) www.test2.com(被代理的站)
假設現在我們用 www.test1.com 來代理 www.test2.com
反向代理規則配置:在 apache的安裝目錄/conf/vhosts.conf 中對應的站點內配置
【每次配置好代理規則後一定要重啟伺服器】
1、全站反向代理
配置全站反向代理後,在瀏覽器訪問www.test1.com的任何連結 最後顯示的都是www.test2.com 相關的內容,例如:訪問 www.test1.com/news.html 實際顯示的內容則是 www.test2.com/news.html 的內容
代理前www.test1.com 的配置
1 2 3 4 5 6 7 8 9 10 11 12 |
<VirtualHost *:80>
DocumentRoot "D:\PHP\WWW\test1"
ServerName www.a.com
ServerAlias
<Directory "D:\PHP\WWW\test1" >
Options FollowSymLinks ExecCGI
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>
|
代理後的配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<VirtualHost *:80>
DocumentRoot "D:\PHP\WWW\test1"
ServerName www.a.com
ServerAlias
<Directory "D:\PHP\WWW\test1" >
Options FollowSymLinks ExecCGI
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
#反向代理配置
ProxyPassMatch ^/.*$ http: //www.test2.com
ProxyPassReverse ^/.*$ http: //www.test2.com
</VirtualHost>
|
2、指定檔案型別代理配置
假設現在只想 www.test1.com 下的以.shtml結尾的訪問代理www.test2.com,配置如下(只有訪問以'.shtml'結尾的 www.test1.com 連結才會顯示 www.test2.com 站點的內容)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<VirtualHost *:80>
DocumentRoot "D:\PHP\WWW\test1"
ServerName www.a.com
ServerAlias
<Directory "D:\PHP\WWW\test1" >
Options FollowSymLinks ExecCGI
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
#反向代理配置
ProxyPassMatch ^/.*\.shtml$ http: //www.test2.com
ProxyPassReverse ^/.*\.shtml$ http: //www.test2.com
</VirtualHost>
|
3、二級目錄代理配置
假設現在只想讓 www.test1.com 的 news 目錄代理 www.test2.com ,配置如下(只有在訪問 http://www.test1.com/news/ 下的頁面才會顯示 www.test2.com 站點的內容 )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<VirtualHost *:80>
DocumentRoot "D:\PHP\WWW\test1"
ServerName www.a.com
ServerAlias
<Directory "D:\PHP\WWW\test1" >
Options FollowSymLinks ExecCGI
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
#反向代理配置
ProxyPass /hexun http: //www.test2.com
ProxyPassReverse /hexun http: //www.test2.com
</VirtualHost>
|
4、指定某個具體的頁面代理配置
指定某個具體頁面代理,也就是隻有在訪問這個指定的頁面時才會顯示代理內容,訪問其他頁面任然顯示自己原本的內容。
假設現在只想讓 http://www.test1.com/contact.html 這個頁面代理www.test2.com,配置如下(只有在訪問 http://www.test1.com/contact.html 是才會顯示 www.test2.com 站點的內容 )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<VirtualHost *:80>
DocumentRoot "D:\PHP\WWW\test1"
ServerName www.a.com
ServerAlias
<Directory "D:\PHP\WWW\test1" >
Options FollowSymLinks ExecCGI
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
#反向代理配置
ProxyRequests off
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
<Location /contact.html>
ProxyPass http: //www.test2.com
ProxyPassReverse http: //www.test2.com
</Location>
</VirtualHost>
|
apache伺服器反向代理的型別大致也就上面4中型別。