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 |
DocumentRoot "D:\PHP\WWW\test1"
ServerName www.a.com
ServerAlias
|
代理后的配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
DocumentRoot "D:\PHP\WWW\test1"
ServerName www.a.com
ServerAlias
#反向代理配置
ProxyPassMatch ^/.*$ http: //www.test2.com
ProxyPassReverse ^/.*$ http: //www.test2.com
|
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 |
DocumentRoot "D:\PHP\WWW\test1"
ServerName www.a.com
ServerAlias
#反向代理配置
ProxyPassMatch ^/.*\.shtml$ http: //www.test2.com
ProxyPassReverse ^/.*\.shtml$ http: //www.test2.com
|
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 |
DocumentRoot "D:\PHP\WWW\test1"
ServerName www.a.com
ServerAlias
#反向代理配置
ProxyPass /hexun http: //www.test2.com
ProxyPassReverse /hexun http: //www.test2.com
|
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 |
DocumentRoot "D:\PHP\WWW\test1"
ServerName www.a.com
ServerAlias
#反向代理配置
ProxyRequests off
Order allow,deny
Allow from all
ProxyPass http: //www.test2.com
ProxyPassReverse http: //www.test2.com
|
apache服务器反向代理的类型大致也就上面4中类型。
文章转自:http://www.kaotop.com/it/858509.html