您的位置:首页 > 运维架构 > 反向代理

Squid(Version 3.1.23)

2015-09-22 16:30 681 查看
一、安装squidYum install squidsquid -v 查看版本以及编译参数 二、配置squid Vim /etc/squid/squid.conf 1、正向代理的基础配置(代理上网+缓存)
1.1 squid服务器配置(保证该主机能够连接互联网)http_port 192.168.1.121:3128 #设置squid代理服务器cache_mem 64 MB cache_dir ufs /data/cache1 4096 16 256 #缓存目录设置4GB缓存空间,16个目录,256个子目录cache_effective_user squid #设置运行squid的用户cache_effective_group squid #设置运行squid组的用户dns_nameservers 8.8.8.8 #设置互联网dns解析 cache_access_log /var/log/squid/access.logcache_log /var/log/squid/cache.logcache_store_log /var/log/squid/store.logvisible_hostname 192.168.1.121 #定义运行 Squid 的主机名称,当访问发生错误时,该选项会显示在错误提示网页中#cache_mgr squidtest888@163.comhttp_access allow all mkdir /data/cache #创建缓存目录
chown -R squid:squid /data/cache1 #更改权限
squid -z #初始化缓存目录/etc/init.d/squid start
squid -kcheck #可以检测配置文件是否有错
squid -k rec #可以重新加载配置
service squid restart #重启squid服务 1.2 客户端设置局域网代理设置

测试方式:多次打开同一个网站,查看其速度访问速度,也可通过如下的命令,查看squid缓存的信息
[root@mysql ~]# find /data/cache1/ -type f /data/cache1/00/00/0000001C/data/cache1/00/00/0000000B/data/cache1/00/00/00000029/data/cache1/00/00/00000011/data/cache1/00/00/000000122、透明代理(代理上网+缓存)考虑到正向代理要修改浏览器的设置,在实际应用中,肯定应用比较麻烦,所以接下来就通过“透明代理”完成这样的工作。原理:简单点说,就是把所以得数据发送到squid上,让squid完成数据的处理和转发,并完成数据缓存等工作。测试环境(squid服务器可通过192.168.1.121连接互联网)

2.1 完成基础网络配置 客户端的配置如下:IPADDR=172.16.1.2NETMASK=255.255.255.0GATEWAY=172.16.1.1 #注意网关指向squid服务器的地址172.16.1.1DNS1=8.8.8.8 Squid服务器配置eth3接口:IPADDR=172.16.1.1NETMASK=255.255.255.0eth2接口:IPADDR=192.168.1.121NETMASK=255.255.255.0GATEWAY=192.168.1.1DNS1=192.168.1.1打开转发功能echo "1" > /proc/sys/net/ipv4/ip_forward启动nat功能iptables -t nat -A POSTROUTING -o eth2 -j MASQUERADE 客户端可以正常上网[root@web1 sysconfig]# ping www.baidu.comPING www.a.shifen.com (61.135.169.125) 56(84) bytes of data.64 bytes from 61.135.169.125: icmp_seq=1 ttl=53 time=46.9 ms64 bytes from 61.135.169.125: icmp_seq=2 ttl=53 time=51.4 ms64 bytes from 61.135.169.125: icmp_seq=3 ttl=53 time=48.1 ms64 bytes from 61.135.169.125: icmp_seq=4 ttl=53 time=48.0 ms 2.2 squid的配置根据2.1的配置,只需修改如下内容http_port 172.16.1.1:3128 transparent 执行squid -k rec 重新启动squid:/etc/init.d/squid restart接下来的一般就比较关键,让所有访问80端口的服务都转到3218端口iptables -t nat -A PREROUTING -i eth3 -p tcp -s 172.16.1.0/24 --dport 80 -j REDIRECT --to-ports 3128 [root@mysqlcache1]# curl -I http://www.china.com.cn/cppcc/2015-09/18/content_36622331.htm





注意:Curl -I 测试的时候,如果只测试www.baidu.com或者www.qq.com域名是看不到hit效果的 3、反向代理的基础配置反向代理,既squid后面是服务器,服务器返回给用户数据需要走squid,在网站架构中,主要用来搭建网站静态项(图片、html、流媒体、js、css等)的缓存服务器。


3.1 squid的配置http_port 192.168.1.121:80 accel vhost vportcache_peer 172.16.1.2 parent 80 0 originserver name=web1cache_peer_domain web1 www.web1.comcache_mem 64 MBcache_dir ufs /data/cache1 4096 16 256cache_effective_user squidcache_effective_group squid cache_access_log /var/log/squid/access.logcache_log /var/log/squid/cache.logcache_store_log /var/log/squid/store.logvisible_hostname 192.168.1.121 #cache_mgr squidtest888@163.comhttp_access allow all 说明:cache_peer设置。表示从客户端过来的请求,如果是 www.web1.com,则Squid向 Server 172.16.1.2的端口80发送请求,如果有多台服务器可以设置多行,name表示设置cached_peer的别名
3.2 客户端配置设置DNS解析(注意,此时web服务器的IP地址为如上图所示的外网地址)192.168.1.121 www.web1.com 访问www.web1.com,其测试结果如下(通过浏览器查看):

squid服务器上可以查看命中率,如下所示查看缓存命中率使用命令: squidclient -h host -p port mgr:info
比如: /usr/local/squid/bin/squidclient -h 127.0.0.1 -p 8080 mgr:info


删除缓存:http://www.aminglinux.com/bbs/thread-5297-1-1.html 故障汇总: http://www.aminglinux.com/bbs/thread-148-1-1.html 三、访问控制列表1、禁止 192.168.1.0使用代理上网acl badclientnet src 192.168.1.0/24http_access deny badclientip## 顺序很重要,把想拒绝的放前面,然后重新加载配置文件,当 1.110 访问网页时则提示拒绝访问 2、禁止访问 IP 为 61.135.169.121 的网站acl badwebserver dst 61.135.169.121http_access deny badwebserver## 这里其实直接写成 baidu.com www.baidu.com 也是可以的 3、禁止访问域名为 www.163.com 的网站acl badwebserver dstdomain www.163.comhttp_access deny badwebserver## 用户可以访问 163.com mail.163.com 4、禁止用户访问域名中包含 163.com 的网站acl badwebserver url_regex 163.comhttp_access deny badwebserver## 这样就可以全面禁止所有包含 163.com 的网站 5、限制 IP 为 192.168.1.110 的并发最大连接数为10acl clientip src 192.168.1.110acl conn10 maxconn 10http_access deny clientip conn10## 通过两条 acl 来限制最大并发连接数,注意 有时候做完就直接不能访问网站了,是因为连接上限了,可以打开一个网页的连接数已经超过了 10 6、禁止 1.0 网段的 IP 在 9:00-18:00 上网acl clientnet src 192.168.1.0/24acl worktime time MTWHF 9:00-18:00http_access deny clientnet worktime 7、禁止用户访问域名包含abc的网站(针对url的关键字)acl badwebserver url_regex abchttp_access deny badwebserver## 用户无法访问 www.abc.com/index.html 等站点 8、禁止下载 *.mp3 *.mp4 *.exe *.zip *.rar 类型的文件(针对文件)acl badfile urlpath_regex -i .mp3 .mp4 .exe .zip .rarhttp_access deny badfile 9、防止盗链squid防盗链,通过设置Referer值。 acl has_referer referer_regex . #直接访问的acl allow_referer referer_regex -i baidu\.com acl allow_referer referer_regex -i google\.com acl allow_referer referer_regex -i yahoo\.cn acl allow_referer referer_regex -i google\.cn http_access allow !has_referer http_access deny !allow_referer deny_info http://img1.test.com/images/noposter.jpg allow_referer 解释一下,has_referer匹配Referer存在,然后利用!has_referer来匹配没有Referer,即直接访问的请求,这部分请求不予做防盗链处理,allow。allow_referer即允许使用源站资源的网站,然后利用!allow_referer来匹配不在允许列表的网站,这些不允许的Referer过来的请求就返回deny_info的内容 七、常见的安全配置(acl匹配安装顺序进行) acl manager proto cache_objectacl localhost src 127.0.0.1/32 ::1acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1acl localnet src 10.0.0.0/8 # RFC1918 possible internal networkacl localnet src 172.16.0.0/12 # RFC1918 possible internal networkacl localnet src 192.168.0.0/16 # RFC1918 possible internal networkacl SSL_ports port 443acl Safe_ports port 80 8080 # httpacl Safe_ports port 21 # ftpacl Safe_ports port 443 # httpsacl CONNECT method CONNECThttp_access allow manager localhosthttp_access deny managerhttp_access deny !Safe_portshttp_access deny CONNECT !SSL_portshttp_access allow localnethttp_access allow localhost
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  squid缓存