squid代理配置

in web with 0 comment

1.yum install -y squid //安装squid

正向代理设置:

2.编辑配置文件,加入以下配置信息

#vi /etc/squid/squid.conf

http_port 3128
acl manager proto cache_object
acl localhost src 127.0.0.1/32 ::1
acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1
acl localnet src 10.0.0.0/8 # RFC1918 possible internal network
acl localnet src 172.16.0.0/12 # RFC1918 possible internal network
acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
acl SSL_ports port 443
acl Safe_ports port 80 8080 # http
acl Safe_ports port 21 # ftp
acl Safe_ports port 443 # https
acl CONNECT method CONNECT
http_access allow manager localhost
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow localnet
http_access allow localhost
http_access allow all
cache_dir aufs /data/cache 1024 16 256
cache_mem 128 MB
hierarchy_stoplist cgi-bin ?
coredump_dir /var/spool/squid
refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern -i (/cgi-bin/|\?) 0 0% 0
refresh_pattern \.(jpg|png|gif|mp3|xml) 1440 50% 2880 ignore-reload
refresh_pattern . 0 20% 4320

3.#mkdir /data/cache #创建缓存目录
#chown -R squid:squid /data/cache #更改权限
#squid -z #初始化缓存目录,该步骤可以省略
#/etc/init.d/squid start
#squid -kcheck #可以检测配置文件是否有错
#squid -k rec #可以重新加载配置
#service squid restart #重启squid服务

4.测试

curl -xlocalhost:3128 www.qq.com -I //出现一大段信息代码则成功,-I 选项

curl -xlocalhost:3128 www.baidu.com -I

squid代理

squid代理

5.添加黑白名单域名
编辑配置文件/etc/squid/squid.conf,在acl CONNECT method CONNECT的下面添加域名信息即可。
①黑名单域名设置
acl http proto HTTP
acl bad_domain dstdomain .sina.com .souhu.com
http_access allow http !bad_domain
http_access deny http bad_domain
②白名单域名设置
acl http proto HTTP
acl good_domain dstdomain .lishiming.net .aminglinux.com
http_access allow http good_domain
http_access deny http !good_domain
重启之后,设置生效。
反向代理设置:
编辑配置文件:
只需将正向代理中的 http_port 3128 改成 http_port 80 accle vhost vport
再增加要代理的后端真实服务器信息:
cache_peer 123.125.119.147 parent 80 0 originserver name=a
cache_peer 61.135.169.125 parent 80 0 originserver name=b
cache_peer_domain a www.qq.com
cache_peer_domain b www.baidu.com
上面是用qq.com和baidu.com来做例子。其中cache_peer为配置后端的服务器ip以及端口,name后边为要配置的域名,这里和后面的cache_peer_domain相对应。实际的应用中,ip大多为内外ip,而域名也许会有多个,如果是squid要代理一台web上的所有域名,那么就写成这样:
cache_peer 192.168.10.111 80 0 originserver

后面连cache_peer_domain 也省了。

重启squid服务测试:

#/etc/squid/squid.conf restart

#curl -xlocalhost:80 www.baidu.com

会出现一大段信息,表示测试成功,能够访问此网站。

Responses