squid
搭建反向代理服务器
需求描述
公司的对外域名www.cl.com解析为反向代理服务器的IP地址:173.16.16.1
当从Internet访问站点www.cl.com时,实际看到的网页内容来源于局域网中的Web服务器群:
192.168.2.11、192.168.2.12 ……
基本实现步骤
修改squid.conf文件,并重新加载该配置
http_port 218.29.30.31:80 vhost
cache_peer 192.168.2.11 parent 80 0 originserver weight=5 max-conn=30
cache_peer 192.168.2.12 parent 80 0 originserver weight=5 max-conn=30
cache_peer 192.168.2.13 parent 80 0 originserver weight=5 max-conn=30
cache_peer 192.168.2.14 parent 80 0 originserver weight=1 max-conn=8
cache_peer Web服务器地址 服务器类型 http端口 icp端口 [可选项]
—— 实现该案例还有一个前提条件:如果各Web服务器处于局域网内,则要能够访问Internet
需要指出的是,透明代理与反向代理不能同时应用,监听端口改为80是为了对应于标准web端口,便于用户使用
cache_peer配置项可以用于指定真正的Web服务器的位置
其中,服务器类型对应到目标主机的缓存级别,上游Web主机一般使用“parent”(父服务器);icp端口用于连接相邻的ICP(Internet Cache Protocol)缓存服务器(通常为另一台Squid主机),如果没有,则使用0;可选项是提供缓存时的一些附件参数,例如“originserver”表示该服务器作为提供Web服务的原始主机,“weight=n”指定服务器的优先权重,n为整数,数字越大优先级越高(缺省为1);“max-conn=n”指定反向代理主机到该web服务器的最大连接数。
验证反向代理的实施效果
在上游Web服务器(192.168.2.11~192.168.2.14)中开启httpd服务
在Internet中的客户机(218.29.30.29)中访问反向代理服务器主机(http://218.29.30.31),应能够看到实际由上游Web服务器提供的网页内容
查看反向代理服务器的访问日志信息
[root@localhost ~]# tail -1 /var/log/squid/access.log
1231256531.038 35 218.29.30.29 TCP_MISS/200 2869 GET http://218.29.30.31/index.php? - FIRST_UP_PARENT/192.168.2.11 p_w_picpath/gif
附:基于反向代理实现虚拟主机映射
[root@localhost ~]# vi /etc/squid/squid.conf
http_port 218.29.30.31:80 vhost
cache_peer 192.168.2.11 parent 80 0 originserver name=cl
cache_peer 192.168.2.12 parent 80 0 originserver name=xhy
cache_peer_domain cl www.cl.com
cache_peer_domain xhy www.xhy.com
以上配置实现访问www.cl.com访问192.168.2.11
访问www.xhy.com访问192.168.2.12