squid配置

1. 什么是squid

 squid可以做代理也可以做缓存

squid缓存不仅可以节省宝贵的带宽资源,也可以大大降低服务器的I/O.  

 squid不仅可以做正向代理,又可以做反向代理。

 正向代理,squid后面是客户端,客户端上网要通过Squid去上;反向代理,squid后面是服务器,服务器返回给用户数据需要走squid.

正向代理用在企业的办公环境中,员工上网需要通过squid代理来上网,这样可以节省网络带宽资源。而反向代理用来搭建网站静态项(图片、html、流媒体、js、css等)的缓存服务器,它用于网站架构中。


2. 搭建squid正向代理

[root@webserver ~]# yum install -y squid

[root@webserver ~]# squid -v   //查看版本以及编译参数
Squid Cache: Version 3.1.10

*****************************

[root@webserver ~]# > /etc/squid/squid.conf
[root@webserver ~]# 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

[root@webserver ~]# mkdir  /data/cache   //创建缓存目录
[root@webserver ~]# chown -R squid:squid /data/cache  //更改权限
[root@webserver ~]# squid -z  //初始化缓存目录,该步骤可以省略

[root@webserver ~]# /etc/init.d/squid start 
正在启动 squid:                                           [确定]

[root@webserver ~]# squid -kcheck  //检测squid配置文件是否正确
2015/05/27 01:52:29| WARNING: Could not determine this machines public hostname. Please configure one or set 'visible_hostname'.
2015/05/27 01:52:30| WARNING: Could not determine this machines public hostname. Please configure one or set 'visible_hostname'.
[root@webserver ~]# vi /etc/squid/squid.conf  //在配置文件添加visible_hostname aminglinux.com
[root@webserver ~]# squid -kcheck
[root@webserver ~]# squid -krec
[root@webserver ~]# service squid restart

停止 squid:................                               [确定]

正在启动 squid:.                                          [确定]


[root@hpf-linux mnt]# curl -x192.168.1.222:3128 www.qq.com -I
HTTP/1.0 200 OK
Server: squid/3.4.1
Date: Tue, 26 May 2015 12:33:09 GMT
Content-Type: text/html; charset=GB2312
Vary: Accept-Encoding
Vary: Accept-Encoding
Expires: Tue, 26 May 2015 12:34:09 GMT
Cache-Control: max-age=60
Vary: Accept-Encoding
Vary: Accept-Encoding
X-Cache: HIT from tianjin.qq.com
X-Cache: MISS from aminglinux.com
X-Cache-Lookup: MISS from aminglinux.com:3128
Via: 1.0 aminglinux.com (squid/3.1.10)
Connection: keep-alive


限制不能访问某些域名:

[root@webserver ~]# vi /etc/squid/squid.conf   //在squid配置文件内添加如下内容

 acl http proto HTTP

 acl bad_domain dstdomain .taobao.com .jd.com

 http_access deny http bad_domain  

[root@webserver ~]# squid -kche
[root@webserver ~]# squid -krec

[root@hpf-linux mnt]# curl -x192.168.1.222:3128 www.jd.com -I
HTTP/1.0 403 Forbidden
Server: squid/3.1.10
Mime-Version: 1.0
Date: Tue, 26 May 2015 18:08:11 GMT
Content-Type: text/html
Content-Length: 3264
X-Squid-Error: ERR_ACCESS_DENIED 0
Vary: Accept-Language
Content-Language: en
X-Cache: MISS from aminglinux.com
X-Cache-Lookup: NONE from aminglinux.com:3128
Via: 1.0 aminglinux.com (squid/3.1.10)
Connection: keep-alive

[root@hpf-linux mnt]# curl -x192.168.1.222:3128 www.jd222.com -I
HTTP/1.0 200 OK
Server: nginx
Date: Tue, 26 May 2015 12:40:52 GMT
Content-Type: text/html
Vary: Accept-Encoding
X-Powered-By: PHP/5.3.28
X-Cache: MISS from aminglinux.com
X-Cache-Lookup: MISS from aminglinux.com:3128
Via: 1.0 aminglinux.com (squid/3.1.10)
Connection: keep-alive


在浏览器上进行测试:

wKioL1VkdCqg0xolAAEr5NqDs5E719.jpg

wKiom1VkcprDZ_MsAACNwerNZwc512.jpg

wKioL1VkdCqSAB6OAADxFLCCaX0850.jpg


3. 搭建squid反向代理

[root@webserver ~]# vi /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

之前增加的域名白/黑名单相关配置去掉

[root@webserver ~]# squid -kche
[root@webserver ~]# squid -krec
[root@webserver ~]# service squid restart
停止 squid:................                               [确定]
正在启动 squid:.                                          [确定]

[root@hpf-linux mnt]# curl -x192.168.1.222:80 www.qq.com -I
HTTP/1.0 200 OK
Server: squid/3.4.1
Date: Tue, 26 May 2015 13:00:45 GMT
Content-Type: text/html; charset=GB2312
Vary: Accept-Encoding
Vary: Accept-Encoding
Expires: Tue, 26 May 2015 13:01:45 GMT
Cache-Control: max-age=60
Vary: Accept-Encoding
Vary: Accept-Encoding
X-Cache: HIT from tianjin.qq.com
X-Cache: MISS from aminglinux.com
X-Cache-Lookup: MISS from aminglinux.com:80
Via: 1.0 aminglinux.com (squid/3.1.10)
Connection: keep-alive

[root@hpf-linux mnt]# curl -x192.168.1.222:80 www.aminglinux.com -I
HTTP/1.0 503 Service Unavailable
Server: squid/3.1.10
Mime-Version: 1.0
Date: Tue, 26 May 2015 18:29:06 GMT
Content-Type: text/html
Content-Length: 3455
X-Squid-Error: ERR_CANNOT_FORWARD 0
Vary: Accept-Language
Content-Language: en
X-Cache: MISS from aminglinux.com
X-Cache-Lookup: MISS from aminglinux.com:80
Via: 1.0 aminglinux.com (squid/3.1.10)
Connection: keep-alive

wKiom1VkcrXwNcQNAAF7p9bnhc4683.jpg