码迷,mamicode.com
首页 > 系统相关 > 详细

Linux下squid代理缓存服务环境部署

时间:2016-10-23 23:09:20      阅读:388      评论:0      收藏:0      [点我收藏+]

标签:启动会   china   routing   日志   htm   代理   agent   selinux   --   

 

代理服务器英文全称是Proxy Server,其功能就是代理网络用户去取得网络信息。

Squid是一个缓存Internet 数据的软件,其接收用户的下载申请,并自动处理所下载的数据。当一个用户想要下载一个主页时,可以向Squid 发出一个申请,要Squid 代替其进行下载,然后Squid 连接所申请网站并请求该主页,接着把该主页传给用户同时保留一个备份,当别的用户申请同样的页面时,Squid 把保存的备份立即传给用户,使用户觉得速度相当快。Squid 可以代理HTTP、FTP、GOPHER、SSL和WAIS等协议并且Squid 可以自动地进行处理,可以根据自己的需要设置Squid,使之过滤掉不想要的东西。

一、工作流程

当代理服务器中有客户端需要的数据时:
1)客户端向代理服务器发送数据请求;
2)代理服务器检查自己的数据缓存;
3)代理服务器在缓存中找到了用户想要的数据,取出数据;
4)代理服务器将从缓存中取得的数据返回给客户端。

当代理服务器中没有客户端需要的数据时:
1)客户端向代理服务器发送数据请求;
2)代理服务器检查自己的数据缓存;
3)代理服务器在缓存中没有找到用户想要的数据;
4)代理服务器向Internet 上的远端服务器发送数据请求;
5)远端服务器响应,返回相应的数据;
6)代理服务器取得远端服务器的数据,返回给客户端,并保留一份到自己的数据缓存中。

技术分享

 

Squid代理服务器工作在TCP/IP的应用层:

技术分享

二、squid分类
按照代理类型的不同,可以将Squid代理分为正向代理和反向代理。

正向代理根据实现方式的不同,又可以分为普通代理和透明代理。
1)普通代理:需要客户机在浏览器中指定代理服务器的地址、端口;
2)透明代理:适用于企业的网关主机(共享接入Internet)中,客户机不需要指定代理服务器地址、端口等信息,代理服务器需要设置防火墙策略将客户机的Web访问数据转交给代理服务程序处理;

反向代理:是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。

三、squid代理缓存环境部署过程:
1)关闭selinux和iptables
[root@server~]# vim /etc/sysconfig/selinux
.......
SELINUX=disabled
[root@server~]# /etc/init.d/iptables stop

2)检查squid软件是否安装
[root@server~]# rpm -qa|grep squid

3)如果未安装,则使用yum 方式安装
[root@server~]# yum -y install squid

4) 设置开机自启动,在3、5级别上自动运行squid服务
[root@server~]# chkconfig --level 35 squid on

5)squid服务器的配置文件说明
squid 的主配置文件是 /etc/squid/squid.conf,所有squid的设定都是在这个文件里配置,这里squid配置如下:
[root@server~]# vim /etc/squid/squid.conf
http_port 3128    #设置监听的IP与端口号
cache_mem 64 MB   #额外使用内存量,可根据你的系统内存在设定,一般为实际内存的1/3.比如这里内存是200M,这里设置1/3就是64MB
maximum_object_size 4 MB  #设置squid磁盘缓存最大文件,超过4M的文件不保存到硬盘
minimum_object_size 0 KB  #设置squid磁盘缓存最小文件
maximum_object_size_in_memory 4096 KB   #设置squid内存缓存最大文件,超过4M的文件不保存到内存
cache_dir ufs /var/spool/squid 100 16 256   #定义squid的cache存放路径 、cache目录容量(单位M)、一级缓存目录数量、二级缓存目录数量
logformat combined %&gt;a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}&gt;h" %Ss:%Sh #log文件日志格式
access_log /var/log/squid/access.log combined  #log文件存放路径和日志格式
cache_log /var/log/squid/cache.log   #设置缓存日志
logfile_rotate 60   #log轮循60天
cache_swap_high 95  #cache目录使用量大于95%时,开始清理旧的cache
cache_swap_low 90   #cache目录清理到90%时停止
acl localnet src 192.168.1.0/24  #定义本地网段
http_access allow localnet  #允许本地网段使用
http_access deny all  #拒绝所有
visible_hostname squid.david.dev  #主机名
cache_mgr wangshibo@huanqiu.com  #管理员邮箱

四、普通代理服务
即标准的、传统的代理服务,需要客户机在浏览器中指定代理服务器的地址、端口。
实验拓扑图如下:

技术分享

1)配置Squid 代理服务器IP地址
将eth1的IP地址修改为200.168.10.1
[root@server~]# ifconfig eth1 200.168.10.1

2) 编辑squid 主配置文件/etc/squid/squid.conf
[root@server~]# vim /etc/squid/squid.conf
http_port 3128
cache_mem 64 MB
maximum_object_size 4 MB
cache_dir ufs /var/spool/squid 100 16 256
access_log /var/log/squid/access.log
acl localnet src 192.168.1.0/24
http_access allow localnet
http_access deny all
visible_hostname squid.david.dev
cache_mgr wangshibo@huanqiu.com

3) 初始化
[root@server~]# squid –z

4) 启动Squid
[root@server~]# /etc/init.d/squid start

5) 配置Web 服务器
安装Apache
[root@server~]# rpm -qa|grep httpd
[root@server~]# yum -y install httpd
启动Apache并加入开机启动
[root@server~]# /etc/init.d/httpd start
[root@server~]# chkconfig httpd on
创建index.html
[root@server~]# echo "<h1>Squid-Web1/200.168.10.2</h1>" > /var/www/html/index.html
修改Web服务器IP地址
将web服务器的IP地址修改为200.168.10.2
[root@server~]# ifconfig eth0 200.168.10.2

6) 配置客户端IP地址

技术分享

 

7) 配置浏览器代理
打开浏览器(以IE为例,其他类似),菜单栏 -> 工具 -> Internet 选项 -> 连接 -> 局域网设置 -> 代理服务器,按照以下格式设置。

技术分享

8) 测试

技术分享

 

五、透明代理服务
适用于企业的网关主机,客户机不需要指定代理服务器地址、端口等信息,通过iptables将客户机的Web访问数据转交给代理服务程序处理。
实验拓扑图如下:

技术分享

1)修改squid 主配置文件/etc/squid/squid.conf
[root@server~]# vim /etc/squid/squid.conf
http_port 3128 transparent
cache_mem 64 MB
maximum_object_size 4 MB
cache_dir ufs /var/spool/squid 100 16 256
access_log /var/log/squid/access.log
acl localnet src 192.168.1.0/24
http_access allow localnet
http_access deny all
visible_hostname squid.david.dev
cache_mgr wangshibo@huanqiu.com

在http_port 3128 后添加transparent 关键字。

2) 重启squid服务
[root@server~]# /etc/init.d/squid reload

3) 添加iptables规则,把内部的http请求重定向到3128端口
启动iptables 服务
[root@server~]# /etc/init.d/iptables start
清除现有iptables filter 表规则
[root@server~]# iptables -F
保存iptables 设置
[root@server~]# /etc/init.d/iptables save
查看nat 表设置
[root@server~]# iptables -t nat -L -n
在nat表中新增一条规则
[root@server~]# iptables -t nat -I PREROUTING -i eth0 -s 192.168.1.0/24 -p tcp --dport 80 -j REDIRECT --to-port 3128
保存iptables
[root@server~]# /etc/init.d/iptables save

设置iptables 开机启动
[root@server~]# chkconfig iptables on

4) 修改客户端IP地址
将默认网关设置为squid 服务器的内网ip地址。

技术分享

5) 在浏览器中,取消代理设置

技术分享

6) 测试

技术分享

透明代理测试成功。

 

六、反向代理服务
为Internet用户访问企业Web站点提供缓存加速。
实验拓扑图如下:

技术分享

 

 

1) 关闭防火墙
# /etc/init.d/iptables stop
2) 修改Web Server 主页
Web1:
[root@server~]#echo "<h1>Squid-Web1/192.168.1.18</h1>" > /var/www/html/index.html

Web2:
[root@server~]# echo "<h1>Squid-Web1/192.168.1.19</h1>" > /var/www/html/index.html

3) 配置squid
[root@server~]# vim /etc/squid/squid.conf
http_port 80 accel vhost
http_access allow all
cache_peer 192.168.1.18 parent 80 0 originserver round-robin weight=1
cache_peer 192.168.1.19 parent 80 0 originserver round-robin weight=1
visible_hostname squid.david.dev
cache_mgr mchina_tang@qq.com

4) 启动Squid服务(在此启动会报错,是因为上面设置了80端口,和http端口冲突。关闭http即可成功启动这里的squid)
[root@server~]# /etc/init.d/squid reload

5) 测试
squid 采用了round-robin,所以客户端的访问将轮询两台web服务器,采用 "Ctrl + F5" 来深度刷新测试。
Web1:

技术分享

Web2:

技术分享

6)查看squid 的访问日志。

技术分享

 

七、实际应用
下面实验将模拟通过不同的域名访问不同的机器,简单实现企业应用中的负载均衡。客户端在浏览器地址栏中输入www.squid.dev,将访问192.168.1.18这台机器,访问bbs.squid.dev,将访问192.168.1.19这台机器。
实验拓扑图如下:

技术分享

 

1) 修改Web Server 主页
Web1:
[root@server~]# echo "<h1>www.squid.dev/192.168.1.18</h1>" > /var/www/html/index.html

Web2:
[root@server~]# echo "<h1>bbs.squid.dev/192.168.1.19</h1>" > /var/www/html/index.html

2) 配置Squid
[root@server~]# vim /etc/squid/squid.conf
http_port 80 accel vhost
http_access allow all
cache_peer 192.168.1.18 parent 80 0 originserver name=www
cache_peer 192.168.1.19 parent 80 0 originserver name=bbs
cache_peer_domain www www.squid.dev
cache_peer_domain bbs bbs.squid.dev
visible_hostname squid.david.dev
cache_mgr wangshibo@huanqiu.com

3) 配置客户端
这里可以使用DNS服务来解析,这里我们为了方便,就在hosts 文件里直接指定。

技术分享

4) 测试网络情况 

技术分享

5) 测试www.squid.dev

技术分享

6) 测试bbs.squid.dev

技术分享

7) 查看squid 访问日志

技术分享

8) 查看两台服务器的apache 访问日志

[root@server~]# tailf /var/log/httpd/access.log

技术分享

测试成功。

 

Linux下squid代理缓存服务环境部署

标签:启动会   china   routing   日志   htm   代理   agent   selinux   --   

原文地址:http://www.cnblogs.com/kevingrace/p/5991274.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!