1、squid简介
官网http://www.squid-cache.org/
squid可做加速的代理服务器。
例如公司有一个人要访问一个新的外网,另外的人也要访问这个页面,同一个公司用的是同一个网线,如果很多人访问这个网站,会占用很大的流量带宽,对于公司来说就是资源的浪费。如果把访问的网页缓存下来放在本地,后面的人访问本地的资源,就能节省很大的带宽资源。还可降低服务器的IO。
假如你访问的是外国的网,可以在公司搭建一个squid代理服务器,可以将服务器访问的网页缓存在服务器上。
可以做正向代理和反向代理。
正向代理:客户端(内网)--->squid---->服务器
反向代理:服务器--->squid---->客户端(内网)
2、安装squid
平台:centos6.4
[root@localhost ~]# yum install -y squid
[root@localhost ~]# squid -v //查看版本以及编译参数 Squid Cache: Version 3.1.10 configure options: '--build=i386-redhat-linux-gnu'
编写配置文件
[root@localhost ~]# vim /etc/squid/squid.conf 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 localnet src fc00::/7 # RFC 4193 local private network range acl localnet src fe80::/10 # RFC 4291 link-local (directly plugged) machines 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 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 deny all http_port 3128 hierarchy_stoplist cgi-bin ? cache_dir ufs /var/spool/squid 100 16 256 //本地缓存目录 cache_mem 128 MB cache_log /var/log/squid.log //日志文件 access_log /var/log/squid/access.log squid 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 . 0 20% 4320
创建缓存目录
[root@localhost ~]# mkdir /data/cache [root@localhost ~]# chown -R squid:squid /data/cache [root@localhost ~]# squid -z //初始化缓存目录 [root@localhost ~]# ls /data/cache/ //生成目录 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
[root@localhost ~]# touch /var/log/squid.log //创建日志文件 [root@localhost ~]# chmod 777 /var/log/squid.log
启动squid
[root@localhost ~]# /etc/init.d/squid start Starting squid: . [ OK ]
[root@localhost ~]# squid -k check //检查配置文件是否有错 [root@localhost ~]# squid -k rec //重新加载配置文件 [root@localhost ~]# squid -k shutdown //关闭squid,关闭速度比较慢
3、正向代理
设置IE浏览器的代理服务器:Internet选项--连接--设置--局域网设置--选择为LAN使用代理服务器--高级--(http 192.168.0.104 3128)--去掉套接字,对所有协议使用
IE浏览器访问网站正常
[root@localhost ~]# tcpdump -nn //通过了squid访问 10:13:11.093638 IP 192.168.0.102.50317 > 192.168.0.104.22: Flags [.], ack 156352, win 4264, length 0 10:13:11.094024 IP 192.168.0.104.22 > 192.168.0.102.50317: Flags [P.], seq 156352:156784, ack 2401, win 583, length 432
在squid上配置iptables禁止3128端口,IE浏览器无法正常访问
[root@localhost ~]# iptables -I INPUT -p tcp --dport 3128 -j REJECT
squid可以控制员工上网,控制上网行为。
测试缓存
[root@localhost ~]# curl -xlocalhost:3128 http://www.lishiming.net/static/p_w_picpath/common/logo.png -I HTTP/1.0 200 OK Server: nginx/1.2.9 Date: Wed, 23 Apr 2014 02:19:05 GMT Content-Type: p_w_picpath/png Content-Length: 7222 Last-Modified: Sat, 12 Oct 2013 01:13:07 GMT Expires: Fri, 23 May 2014 02:19:05 GMT Cache-Control: max-age=2592000 Accept-Ranges: bytes X-Cache: MISS from localhost.localdomain //没有从本地匹配到 X-Cache-Lookup: MISS from localhost.localdomain:3128 Via: 1.0 localhost.localdomain (squid/3.1.10) Connection: keep-alive [root@localhost ~]# [root@localhost ~]# curl -xlocalhost:3128 http://www.lishiming.net/static/p_w_picpath/common/logo.png -I HTTP/1.0 200 OK Server: nginx/1.2.9 Date: Wed, 23 Apr 2014 02:19:05 GMT Content-Type: p_w_picpath/png Content-Length: 7222 Last-Modified: Sat, 12 Oct 2013 01:13:07 GMT Expires: Fri, 23 May 2014 02:19:05 GMT Cache-Control: max-age=2592000 Accept-Ranges: bytes Age: 8 X-Cache: HIT from localhost.localdomain //从本地缓存 X-Cache-Lookup: HIT from localhost.localdomain:3128 Via: 1.0 localhost.localdomain (squid/3.1.10) Connection: keep-alive
设置白名单和黑名单,限制上网的网站
[root@localhost ~]# vim /etc/squid/squid.conf //设置白名单,下面的网站可以访问 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_access allow localnet //写在此句前面 http_access allow localhost http_access deny all
[root@localhost ~]# vim /etc/squid/squid.conf //设置黑名单,下面的网站不访问 acl http proto HTTP acl bad_domain dstdomain .sina.com .baidu.com http_access deny http bad_domain http_access allow localnet http_access allow localhost http_access deny all
4、反向代理
[root@localhost ~]# vim /etc/squid/squid.conf http_port 80 accel vhost vport //修改port 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 //需要指定web服务器后端的IP、域名和端口,反向代理qq和baidu服务器
[root@localhost ~]# vim /etc/hosts 127.0.0.1 www.qq.com www.baidu.com www.aminglinux.com www.sina.com
测试反向代理 [root@localhost ~]# curl www.qq.com -I HTTP/1.0 200 OK //访问正常 Server: squid/3.2.1 Date: Wed, 23 Apr 2014 02:48:54 GMT Content-Type: text/html; charset=GB2312 Vary: Accept-Encoding Expires: Wed, 23 Apr 2014 02:49:54 GMT Cache-Control: max-age=60 Vary: Accept-Encoding X-Cache: HIT from beijing.qq.com X-Cache: MISS from localhost.localdomain X-Cache-Lookup: MISS from localhost.localdomain:80 Via: 1.0 localhost.localdomain (squid/3.1.10) Connection: keep-alive [root@localhost ~]# curl -x127.0.0.1:80 www.sohu.com -I HTTP/1.0 503 Service Unavailable //访问失败 Server: squid/3.1.10 Mime-Version: 1.0 Date: Wed, 23 Apr 2014 02:49:55 GMT Content-Type: text/html Content-Length: 3433 X-Squid-Error: ERR_CANNOT_FORWARD 0 Vary: Accept-Language Content-Language: en X-Cache: MISS from localhost.localdomain X-Cache-Lookup: MISS from localhost.localdomain:80 Via: 1.0 localhost.localdomain (squid/3.1.10) Connection: keep-alive