阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Linux下配置Squid代理服务器

153次阅读
没有评论

共计 7219 个字符,预计需要花费 19 分钟才能阅读完成。

Linux 下配置 Squid 代理服务器

1、什么是 squid

Squid cache(简称为 Squid)是一个流行的自由软件(GNU 通用公共许可证)的代理服务器和 Web 缓存服务器。Squid 有广泛的用途,从作为网页服务器的前置 cache 服务器缓存相关请求来提高 Web 服务器的速度,到为一组人共享网络资源而缓存万维网,域名系统和其他网络搜索,到通过过滤流量帮助网络安全,到局域网通过代理上网。Squid 主要设计用于在 Unix 一类系统运行。
Squid 的发展历史相当悠久,功能也相当完善。除了 HTTP 外,对于 FTP 与 HTTPS 的支援也相当好,在 3.0 测试版中也支援了 IPv6。
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  查看版本以及编译参数(Squid Cache: Version 3.1.10)
> /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
acl Safe_ports port 21
acl Safe_ports port 443
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
######################################################### 到此结束

配置解释:
acl Safe_ports port 80 8080        # http 的端口
acl Safe_ports port 21          # ftp 的端口
acl Safe_ports port 443        # https 的端口
cache_dir aufs /data/cache 1024 16 256    #缓存空间 1024M 大小 16 个一级目录,256 个子目录
cache_mem 128 MB    #缓存可以使用的内存大小;放在内存中访问数据速度快;

mkdir  /data/cache    #创建缓存目录
chown -R squid:squid /data/cache    #更改缓存目录权限
squid -z    #初始化缓存目录,squid 新版本 3.1 可以省略
/etc/init.d/squid start    #启动 squid 服务
squid  -k check    #可以检测配置文件是否有错;可以简写为 -kche
squid -k rec    #可以重新加载配置,reconfig 的简写;
service squid restart    #重启 squid 服务;重启经常性的很慢,可以先 killall squid,在启动服务;

检测配置文件,报错:Could not determine this machines public hostname. Please configure one or set ‘visible_hostname’. 没有定义公共主机名,需要配置 visible_hostname 可视化主机名;(squid 出问题,会在浏览器显示 squid 的主机名)
在配置文件中加入:visible_hostname yonglinux 就不会报错;
1234 [root@yonglinux ~]# squid -k check
2015/05/25 03:09:18| WARNING: Could not determine this machines public hostname. Please configure one or set ‘visible_hostname’.
2015/05/25 03:09:18| WARNING: Could not determine this machines public hostname. Please configure one or set ‘visible_hostname’.
squid: ERROR: No running copy

在另一台 linux 进行测试:curl -x192.168.22.30:3128 www.qq.com   
指定代理服务器 192.168.22.30 的 3128 端口访问网站,前提保证代理服务器能访问网站;
设定代理服务器的作用是让局域网的用户访问网站速度快,另一方面可以控制用户访问哪些网站;上班期间禁止员工看视频,购物;

访问图片,测试缓存,缓存的时间,X-Cache 为 HIT 击中,说明 squid 缓存起作用;第一次为 MISS;
[root@localhost ~]# curl -x192.168.22.30:3128 ‘http://www.linuxidc.com/images/home/images/logo.jpg’ -I
HTTP/1.0 200 OK
Server: Tengine
Date: Sun, 24 May 2015 13:42:43 GMT
Content-Type: image/jpeg
Content-Length: 5309
Last-Modified: Wed, 22 Jan 2014 07:55:12 GMT
Expires: Sun, 31 May 2015 13:42:43 GMT
Cache-Control: max-age=604800
Load-Balancing: web39
Accept-Ranges: bytes
Age: 29661
X-Cache: HIT from yonglinux
X-Cache-Lookup: HIT from yonglinux:3128
Via: 1.0 yonglinux (squid/3.1.10)
Connection: keep-alive

设置 squid 代理服务器只代理某几个域名 
设置域名白名单,允许 baidu sohu 可以访问,其他都拒绝;
vim /etc/squid/squid.conf    下面的内容加入到 squid 配置文件 acl 下面;
acl http proto HTTP
acl good_domain dstdomain .baidu.com .sohu.com
http_access allow http good_domain
http_access deny http !good_domain

使用 curl 测试白名单,baidu、sohu 返回状态码为 200 OK,qq 不在白名单则返回 403;

[root@localhost ~]# curl -x192.168.22.30:3128 www.sohu.com -I
HTTP/1.0 200 OK
Content-Type: text/html
Date: Sun, 24 May 2015 13:57:32 GMT
Server: SWS
Vary: Accept-Encoding
Cache-Control: no-transform, max-age=120
Expires: Sun, 24 May 2015 13:59:32 GMT
Last-Modified: Sun, 24 May 2015 13:57:21 GMT
X-RS: 11172604.20347654.12509576
FSS-Cache: HIT from 9861864.17726194.11198816
X-Cache: MISS from yonglinux
X-Cache-Lookup: MISS from yonglinux:3128
Via: 1.0 yonglinux (squid/3.1.10)
Connection: keep-alive
 
[root@localhost ~]# curl -x192.168.22.30:3128 www.qq.com -I
HTTP/1.0 403 Forbidden
Server: squid/3.1.10
Mime-Version: 1.0
Date: Sun, 24 May 2015 22:04:30 GMT
Content-Type: text/html
Content-Length: 3254
X-Squid-Error: ERR_ACCESS_DENIED 0
Vary: Accept-Language
Content-Language: en
X-Cache: MISS from yonglinux
X-Cache-Lookup: NONE from yonglinux:3128
Via: 1.0 yonglinux (squid/3.1.10)
Connection: keep-alive

限制某些域名不能通过代理访问
设置域名黑名单,不允许访问 taobao.com jd.com;其他的都允许;
vim /etc/squid/squid.conf  下面的内容加入到 squid 配置文件 acl 下面
acl http proto HTTP
acl bad_domain dstdomain .taobao.com .jd.com
http_access deny http bad_domain

使用 curl 测试黑名单,taobao、jd 返回状态码为 403,linuxidc 不在黑名单返回 200 OK;
[root@localhost ~]# curl -x192.168.22.30:3128 www.taobao.com -I
HTTP/1.0 403 Forbidden
Server: squid/3.1.10
Mime-Version: 1.0
Date: Sun, 24 May 2015 21:35:22 GMT
Content-Type: text/html
Content-Length: 3266
X-Squid-Error: ERR_ACCESS_DENIED 0
Vary: Accept-Language
Content-Language: en
X-Cache: MISS from yonglinux
X-Cache-Lookup: NONE from yonglinux:3128
Via: 1.0 yonglinux (squid/3.1.10)
Connection: keep-alive
 
[root@localhost ~]# curl -x192.168.22.30:3128 www.jd.com -I
HTTP/1.0 403 Forbidden
Server: squid/3.1.10
Mime-Version: 1.0
Date: Sun, 24 May 2015 21:35:32 GMT
Content-Type: text/html
Content-Length: 3254
X-Squid-Error: ERR_ACCESS_DENIED 0
Vary: Accept-Language
Content-Language: en
X-Cache: MISS from yonglinux
X-Cache-Lookup: NONE from yonglinux:3128
Via: 1.0 yonglinux (squid/3.1.10)
Connection: keep-alive

[root@localhost ~]# curl -x192.168.22.30:3128 www.linuxidc.com -I
HTTP/1.0 200 OK
Server: Tengine
Date: Sun, 24 May 2015 13:31:21 GMT
Content-Type: text/html
Vary: Accept-Encoding
Load-Balancing: web39
X-Cache: MISS from yonglinux
X-Cache-Lookup: MISS from yonglinux:3128
Via: 1.0 yonglinux (squid/3.1.10)
Connection: keep-alive

使用 IE 浏览器测试,需要设置代理服务器,菜单栏——工具——Internet 选项——连接——局域网设置,勾选代理服务器——高级,填写 squid 代理服务器地址和端口号;

Linux 下配置 Squid 代理服务器

访问 jd.com taobao.com,提示错误,访问被拒绝,由之前定义的可视化主机名发出的;访问其他网站正常;

Linux 下配置 Squid 代理服务器

3、搭建 squid 反向代理
vim /etc/squid/squid.conf  #如下变更
之前增加的域名白 / 黑名单相关配置去掉;
http_port 3128 改为 http_port 80 accel vhost vport
增加如下内容:
cache_peer 14.17.42.40 parent 80 0 originserver name=a
cache_peer 180.97.33.107 parent 80 0 originserver name=b
cache_peer_domain a www.qq.com
cache_peer_domain b www.baidu.com

监听的 3128 端口改为 80 端口;IE 浏览器代理服务器的端口也要更改为 80;
14.17.42.40 为 ping www.qq.com 的 ip 地址;
如果是 squid 要代理一台 web 上的所有域名,那么就写成这样: cache_peer 192.168.10.111 80 0 originserver  #只需要这一行,cache_peer_domain  可以省略;
/etc/init.d/squid restart

IE 浏览器测试反向代理,访问 baidu.com、qq.com 可以访问,其他网站提示:目前无法将您的请求进行转送操作

Linux 下配置 Squid 代理服务器

使用 curl 测试
[root@localhost ~]# curl -x192.168.22.30:80 www.qq.com -I
HTTP/1.0 200 OK
Server: squid/3.4.1
Date: Sun, 24 May 2015 14:22:47 GMT
Content-Type: text/html; charset=GB2312
Vary: Accept-Encoding
Vary: Accept-Encoding
Expires: Sun, 24 May 2015 14:23:47 GMT
Cache-Control: max-age=60
Vary: Accept-Encoding
Vary: Accept-Encoding
X-Cache: 
HIT from shenzhen.qq.com
X-Cache: 
MISS from yonglinux
X-Cache-Lookup: MISS from yonglinux:80
Via: 1.0 yonglinux (squid/3.1.10)
Connection: keep-alive

访问 qq.com 返回 HIT from shenzhen.qq.com,说明 qq.com 本身也做了反向代理;

squid 还有很多配置,后续会陆续更新;

配置 Squid 代理 http 和 rsync http://www.linuxidc.com/Linux/2013-05/84642.htm

Squid:实现高速的 Web 访问 http://www.linuxidc.com/Linux/2013-04/83512.htm

CentOS 6.2 编译安装 Squid 配置反向代理服务器 http://www.linuxidc.com/Linux/2012-11/74529.htm

简单配置 Squid 代理和反向代理 http://www.linuxidc.com/Linux/2014-04/99465.htm

CentOS 6.4 下 DNS+Squid+Nginx+MySQL 搭建高可用 Web 服务器 http://www.linuxidc.com/Linux/2014-04/99984.htm

Squid 的详细介绍:请点这里
Squid 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-05/118085.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计7219字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中