1. 什么是squid
squid可以做代理也可以做缓存
squid缓存不仅可以节省宝贵的带宽资源,也可以大大降低服务器的I/O.  
squid不仅可以做正向代理,又可以做反向代理。 
正向代理,squid后面是客户端,客户端上网要通过Squid去上;反向代理,squid后面是服务器,服务器返回给用户数据需要走squid. 
正向代理用在企业的办公环境中,员工上网需要通过squid代理来上网,这样可以节省网络带宽资源。而反向代理用来搭建网站静态项(图片、html、流媒体、js、css等)的缓存服务器,它用于网站架构中。 

2. 搭建squid正向代理
官方网站为 http://www.squid-cache.org/  
yum install -y squid
squid -v  查看版本以及编译参数
> /etc/squid/squid.conf
vim /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
### 到此结束

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服务

测试:curl -xlocalhost:3128 www.qq.com
访问图片,测试缓存: curl -xlocalhost:3128   -I 'http://www.aminglinux.com/bbs/static/p_w_picpath/common/logo.png'
限制某些域名不能通过代理访问,或者说只想代理某几个域名  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 
测 curl -xlocalhost:3128 www.google.com.hk -I  #403
测 curl -xlocalhost:3128 www.aminglinux.com/bbs/forum.php  -I #200
vim /etc/squid/squid.conf  #设置域名黑名单

3. 搭建squid反向代理
vim /etc/squid/squid.conf  #如下变更
http_port 3128 改为 http_port 80 accel 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 
之前增加的域名白/黑名单相关配置去掉
如果是squid要代理一台web上的所有域名,那么就写成这样: cache_peer 192.168.10.111 80 0 originserver  #只需要这一行,cache_peer_domain  都可以省掉
/etc/init.d/squid restart 
测试 www.baidu.com www.qq.com  www.lishiming.net

在做反向代理的时候遇到了一点问题启动squid之后失败:

正在启动 squid:....................                       [失败]

检查配置文件报错:

[root@localhost squid]# squid -k check

squid: ERROR: No running copy

查看cache.log日志报端口占用的错误

2015/03/28 00:45:44| commBind: Cannot bind socket FD 16 to [::]:80: (98) Address already in use

FATAL: Cannot open HTTP Port

这是因为启用了http服务造成的,把http服务管掉后,服务正常了。


squid知识扩展:

一、squid防盗链的配置如下:

acl pics urlpath_regex -i \.(gif|jpg|jpeg|bmp|png)$ /* 匹配是否是图片 */
acl domain_refer referer_regex -i domain /* 匹配允许的域名关键字 */
acl notnull_refer referer_regex . /* 匹配非空referer */
http_access allow pics !notnull_refer /* 允许空referer访问图片 */
http_access deny pics !domain_refer /* 拒绝其他域名调用图片 */
deny_info http://www.domain.com/logo.gif domain_refer /* 有盗链的,显示我们的 logo*/


二、提高squid缓存的命中率

安装运行squid后用命令

squidclient -t 1 -h localhost -p 80 mgr:info 查看命中率情况

Request Hit Ratios:     5min: 99.6%, 60min: 98.7%    Cache Request命中率

 Byte Hit Ratios:        5min: 100.0%, 60min: 100.0%    Cache Byte命中率

 

如果命中率低 则

1  apache中的模块 mod_expires是否打开

2  调整squid中的参数

# cache_mem 8 MB
cache_mem 64 MB

# maximum_object_size 4096 KB
maximum_object_size 16384 KB

# maximum_object_size_in_memory 8 KB
maximum_object_size_in_memory 256 KB

# ipcache_size 1024
ipcache_size 2048

#Default:
cache_dir ufs /usr/local/squid/cache 2048 32 512
3  如果apache中使用了deflate压缩
   设置   cache_vary on
4 如果用nginx 可以用第三方模块mod_urlhash 提高命中率

三、清除指定squid缓存文件的脚本代码分享

http://www.jb51.net/article/29654.htm


四、squid日志详解

http://www.php-oa.com/2008/01/17/squid-log-access-store.html