配置Squid实现反向代理。终端用户请求Web服务时,DNS将请求的域名解析为反向代理服务器的IP地址,代理服务器首先检查本地是否有缓存,如果有则直接将响应数据包返回给客户端,否则代理服务器转发客户端请求给后端真实源服务器。利用Squid强大的代理缓存技术来提高并发访问的快速响应,通过缓存可以更快地为客户端提供服务。如果在全国范围内,使用多个反向代理,再结合DNS服务器的视图功能,可以为企业提供强大而稳定的CDN系统。
Squid一般仅缓存静态页面,比如HTML网页、图片、歌曲等,默认不缓存CGI脚本程序或者PHP、JSP。Squid可以根据HTTP头信息来决定如何缓存数据,以及缓存周期,如Expires(缓存有效期)、Cache-Control(是否进行缓冲)。
服务器名称 网络配置
squid.example.com eth0:192.168.88.101
eth1:192.168.85.100
web1.example.com eth0:192.168.85.101
web2.example.com eth0:192.168.85.102
client.example.com eth0:10.10.88.102
1.代理服务器设置
a.配置网卡信息
vi /etc/sysconfig/network-scripts/ifcfg-eth0
1 2 3 4 5 6 7 8 9 | DEVICE=eth0 HWADDR=00:0C:29:E8:EC:D9 TYPE=Ethernet UUID=aed0a4d3-0c22-476a-9a80-ec3fffeff195 ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=static IPADDR=192.168.88.101 NETMASK=255.255.255.0 |
vi /etc/sysconfig/network-scripts/ifcfg-eth1
1 2 3 4 5 6 7 | DEVICE=eth1 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=static IPADDR=192.168.85.100 NETMASK=255.255.255.0 |
重启网络服务 service network restart
b.安装配置Squid服务
yum方式安装Squid服务:yum install -y squid
修改squid配置文件,vi /etc/squid/squid.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | acl manager proto cache_object #定义缓存管理ACL acl localhost src 127.0.0.1/32 ::1 #定义源地址为本地回环地址的ACL acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1 #定义目标地址的ACL acl localnet src 10.0.0.0/8 # RFC1918 possible internal network #定义源地址为10.0.0.0/8的ACL 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 #定义安全端口为443的ACL 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 #定义连接方式为CONNECT的ACL http_access allow manager localhost #仅允许本机主机进行缓存管理 http_access deny manager #拒绝其他主机缓存管理 http_access deny !Safe_ports #拒绝所有非Safe_ports端口连接,Safe_ports端口为定义的若干端口 http_access deny CONNECT !SSL_ports http_access allow localnet http_access allow localhost http_access allow all #允许所有 visible_hostname squid.example.com #设置主机名称 cache_mem 2048 MB #设置内存缓存总容量 maximum_object_size_in_memory 4096 KB #内存可以缓存的单个文件最大容量 maximum_object_size 4096 KB #磁盘可以缓存的单个文件最大容量 cache_dir ufs /var/spool/squid 800 16 256 #定义硬盘缓冲目录/var/spool/squid,缓存最大容量为800M,目录下将创建16个一级子目录和256个二级子目录,这些目录将分类管理缓存数据 error_directory /usr/share/squid/errors/zh-cn #定义报错文件的存放位置 cache_log /var/log/squid/cache.log #缓存日志文件 cache_mgr admin@test.com #管理员邮箱 http_port 80 accel vhost #代理服务器监听的端口,accel设置squid为加速模式,vhost为反向代理 http_port 3128 #同时监听3128端口,方便用squidclient对缓存进行管理 #配置后台源服务器,originserver模拟本机服务器为源服务器,80为HTTP端口 #设置ICP端口为0(源服务器不支持ICP查询);no-query表示禁止使用ICP进行查询;round-robin可以让代理轮训多台源服务器,可以使用weigh=N为源服务器指定权重;name用来设置源服务器的唯一名称,代理转发数据至相同主机的不同端口非常有用 cache_peer 192.168.85.101 parent 80 0 no-query originserver round-robin name=server1 cache_peer 192.168.85.102 parent 80 0 no-query originserver round-robin name=server2 hierarchy_stoplist cgi-bin ? #禁止缓存CGI脚本 coredump_dir /var/spool/squid #使用refresh_pattern应用在没有过期的数据,squid顺序检查refresh_pattern并确定数据是否过期,格式如下: #refresh_pattern [-i] regexp min percent max [option] #refresh_pattern 使用正则表达式匹配数据对象(-i选项表示不区分大小写) #min为过期的最低时间(单位为分钟),及对象在缓存中至少要存放的时间 #max为最大时间 #percent为百分比,如果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 |
使用squid -kcheck 检查配置文件是否出错
关闭防火墙service iptables stop
启动squid service squid start
2.源服务器配置(以一台为例)
vi /etc/sysconfig/network-scripts/ifcfg-eth0
1 2 3 4 5 6 7 8 9 | DEVICE=eth0 HWADDR=00:0C:29:B8:A3:63 TYPE=Ethernet UUID=aee772d7-4c13-487b-ba64-316b05eaab5d ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=static IPADDR=192.168.85.101 NETMASK=255.255.255.0 |
关闭防火墙 service iptables stop
安装httpd yum install httpd
echo “101” > /var/www/html/index.html
service httpd start
3.在浏览器中输入http://192.168.88.101,看代理是否成功