Jan 5

[原]配置Squid 2.6实现反向代理 晴

linuxing , 18:23 , 网络服务 » 常见服务 , 评论(1) , 引用(0) , 阅读(36170) , Via 本站原创 | |
    由于我把Web服务器放到VPS里面运行了,所以,需要在HW里面做Web转发。方法有很多,如iptables转发等。综合考虑后,决定使用Squid进行反向代理,一方面可以加速、一方面管理和监控都更方便。HW使用Centos 5.2,可以通过yum安装Squid 2.6 Stable6。配置时,发现2.6对比2.5还是有点区别的,下面主要讲解不同的地方。
    Squid 2.5版的配置,见:[原]典型的Squid配置[原]用squid实现身份认证的代理

一、网络架构
OpenVZ设定如下:
引用
HW:121.12.172.184:80 ←提供Squid服务
↓   ↓   ↓   ↓   ↓   ↓
HW:127.0.0.1:80 ←80端口也提供Web服务
VE112:192.168.100.112:80 ←提供Web服务

※ 因一些遗留问题,HW上也需要提供Web服务,但其在127.0.0.1上进行监听80端口,与Squid不冲突,Squid也会把请求转到该http服务上。
开始配置Squid前,请确认已经成功完成OpenVZ的搭建工作,并且打开HW的路由转发。
安装Squid 2.6:
引用
# yum install squid*
# rpm -q squid
squid-2.6.STABLE6-5.el5_1.3

二、Squid配置
Squid 2.6的主配置文件还是在/etc/squid/squid.conf,内容包括注释特别多,不再一一说明,列一下吧:
引用
# grep -v '^$' squid.conf|grep -v '^#'
#设置监听地址和端口,并支持vhost(虚拟主机)和vport(虚拟端口)模式
#在2.5版本,我是没有找到转发到转发到非80端口的方法,2.6上就可以支持

http_port 121.12.172.184:80 vhost vport
#设置后台目标http服务器IP地址和端口,务必是IP地址,并给每个规则一个名称:a、b。
#我看了网上很多资料,都用了2.5的方式,在这里设置域名,然后配置/etc/hosts和域名对应
#经测试,是错的,应填入http服务器的IP地址

cache_peer 192.168.100.112 parent 80 0 no-query originserver weight=1 login=PASS name=a
cache_peer 127.0.0.1 parent 80 0 no-query originserver weight=1 login=PASS name=b
#下面的规则与上面的匹配,指http服务器上有那些域名,也就是对应虚拟主机
cache_peer_domain b www.sdline.cn manage.sdline.cn uc.sdline.cn home.sdline.cn shop.sdline.cn bbs.sdline.cn baocui.sdline.cn mrhf.sdline.cn outdoor.sdline.cn wedding.sdline.cn
cache_peer_domain a www.linuxfly.org
hierarchy_stoplist cgi-bin ?
#不缓存cgi-bin、POST等查询
acl QUERY urlpath_regex cgi-bin \?
acl POST method POST
no_cache deny QUERY
no_cache deny POST
acl apache rep_header Server ^Apache
broken_vary_encoding allow apache
#设置使用的内存大小
cache_mem 512 MB
#内存中允许存放的最大缓存文件
maximum_object_size_in_memory 128 KB
#设置缓存文件存放的路径和格式
cache_dir ufs /var/spool/squid 10000 16 256
#后台日志格式
logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh
access_log /var/log/squid/access.log combined

cache_log /var/log/squid/cache.log
cache_store_log /var/log/squid/store.log
emulate_httpd_log on
#下面这句是默认值,但实际上2.6已经没有根据hosts文件寻找虚拟主机位置了
hosts_file /etc/hosts
#要禁用重写url头,否则后台的http服务器日志看不到客户端的来源信息了
url_rewrite_host_header off
refresh_pattern ^ftp:           1440    20%     10080
refresh_pattern ^gopher:        1440    0%      1440
refresh_pattern .               0       20%     4320
#下面都是默认配置,反向代理基本上不要修改
acl all src 0.0.0.0/0.0.0.0
acl manager proto cache_object
acl localhost src 127.0.0.1/255.255.255.255 121.12.172.184/255.255.255.255
acl to_localhost dst 127.0.0.0/8
acl SSL_ports port 443
acl Safe_ports port 80          # http
acl Safe_ports port 21          # ftp
acl Safe_ports port 443         # https
acl Safe_ports port 70          # gopher
acl Safe_ports port 210         # wais
acl Safe_ports port 1025-65535  # unregistered ports
acl Safe_ports port 280         # http-mgmt
acl Safe_ports port 488         # gss-http
acl Safe_ports port 591         # filemaker
acl Safe_ports port 777         # multiling http
acl CONNECT method CONNECT
#这里增加一个限制对大连接数,减轻服务器的负载,但不要设置得太低了,否则客户端就会有很多x错误了
acl OverConnLimit maxconn 50
acl myip dst 192.168.100.0/255.255.255.0
http_access allow manager localhost
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access deny OverConnLimit
#这是对应上面的一条规则的
http_access allow localhost
#允许访问
http_access allow all
http_access deny all
http_reply_access allow all
icp_access allow all
#这是另外一个关于域名的安全设定,建议与上面的cache_peer部分对应
cache_peer_access a allow all
cache_peer_access b allow all
#设置管理员邮箱,注意垃圾邮件哦
cache_mgr manager@linuxfly.org
cache_effective_user squid
cache_effective_group squid
#设置错误页面中提示的域名显示
visible_hostname www.linuxfly.org
#日志循环数量,这个我注释了,因为使用rpm安装的squid,日志循环配置由/etc/logrotate.d/squid决定
# 可调整该配置文件中保存的日志数量,需留意的是,循环时间由执行logrotate的时间决定,也就是4点
#logfile_rotate 10
tcp_recv_bufsize 65535 bytes
forwarded_for on
#这里把错误提示改用中文显示,默认是英文的
error_directory /usr/share/squid/errors/Simplify_Chinese
coredump_dir /var/spool/squid

2.6版本的Squid不需要依赖/etc/hosts文件实现域名和IP对应关系,不需要额外配置。

配置文件下载:

三、修改http日志格式
Squid可以把访问Web服务器的客户端IP地址放入成为X-Forwarded-For的域中,然后传递给后台的Apache服务器。这样,Apache的后台日志上记录的就是客户端的源IP地址,而不是Squid代理网关的IP了。这有助于今后可以继续使用awstats等日志分析工具进行分析工作,也是与使用iptables nat转发的一个比较大的区别。
要实现这点,除了Squid配置文件中关闭url_rewrite_host_header外,还需要修改Apache的日志格式。修改/etc/httpd/conf/httpd.conf:
把原来:
引用
#LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

修改为:
引用
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

同样的,虚拟主机中也需要使用该日志格式:
引用
#CustomLog logs/www.linuxfly.org-access_log common
CustomLog logs/www.linuxfly.org-access_log combined

这样,从后台的日志中,即可看到类似的信息:
引用
# tail -n1 /var/log/httpd/www.linuxfly.org-access_log
61.247.222.55 - - [05/Jan/2009:22:56:28 +0800] "GET /category_3_1_39.htm HTTP/1.0" 404 298 "-" "Yeti/1.0 (NHN Corp.; http://help.naver.com/robots/)"
Tags: , ,
KK
2009/05/12 11:12
好文
分页: 1/1 第一页 1 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]