Centos7 yum安装squid实现代理缓存

Centos7 yum安装squid实现代理缓存

 

前期环境准备:

VMware 虚拟机

系统

IP地址

网卡名称

Squid服务器

centos7

外网   桥接DHCP

192.168.1.3/24

网关 192.168.1.1

DNS 自动获取

内网 NET方式   手动配置

192.168.136.100/24

无网关无DNS

外网 ens37

内网 ens33

squid测试机

windows7

NAT方式

192.168.136.110

本地连接

 

第一种 传统模式(手动模式)

安装epel

yum install epel-release -y

yum repolist 

yum info squid

yum install squid -y

squid -v 

free -h 

df -h 

vi /etc/squid/squid.conf 

[root@squid-server ~]# vim /etc/squid/squid.conf

59 #http_access deny all                                                                  //注释掉

60 http_access allow all                                                          //允许所有地址访问

61 cache_mem 512 MB                   //内存缓冲区大小,物理内存1/4

69 cache_dir ufs /var/spool/squid 10000 16 256            //硬盘缓存区目录及大小

70 visible_hostname min1.yu.net                                       //设置主机名,也可以不要这一行

 

开启squid

systemctl start squid

ls /var/spool/squid/ 

yum install net-tools -y

netstat -antup | grep squid

防火墙放行squid服务

firewall-cmd --list-all 

firewall-cmd --permanent --add-service=squid

firewall-cmd --reload 

firewall-cmd --list-all 

查看缓存目录内容

ls /var/spool/squid/

查看缓存目录占用磁盘空间大小 

du -sh /var/spool/squid/

检查配置文件

squid -k parse

再次查看缓存目录大小(每次访问过后缓存目录会变大)

du -sh /var/spool/squid/

客户机的代理配置,在IE浏览器中,选择工具”->“Internet选项,子啊弹出的“Internet选项对话框,在连接选项中的局域网(LAN)设置选项组中单机局域网设置按钮,弹出局域网(LAN)设置对话框,设置如下:

2.png

 

第二种 反向代理

Squid亲测反向代理,可以支持内网和公网IP,但配置文件不支持写域名,所以代理一个网站前得确认该网站IP和域名访问的效果是一样的。而且代理单纯的静态网站没问题,但是代理某些动态页面就会显示不全或者干脆不显示。估计这也是为什么它现在不怎么流行的原因。Squid一次只能运行一种代理模式,启用反向代理其他代理模式自动失效。

 

2.1关闭防火墙,或者不关闭开放80端口

systemctl stop firewalld

2.2 配置好一台被代理的web服务器

再用虚拟机启用一台centos7并且安装httpd充当被反向代理的web服务器用来提供测试页

它的IP为内网IP192.168.136.88

2.3修改配置文件

vim /etc/squid/squid.conf

修改为如下

http_port 80 accel defaultsite=192.168.136.88 vhost

cache_peer 192.168.136.88 parent 80 0 no-query originserver name=myAccel

visible_hostname squid-cache.com

acl our_sites dstdomain 192.168.136.88

http_access allow our_sites

cache_peer_access myAccel allow our_sites

cache_peer_access myAccel allow all

检查配置,看看有没有报错的地方

squid -k parse

2.4重启

systemctl restart squid

此时,用win7访问192.168.136.100就会访问到192.168.136.88的内容了

 

 

第三种 透明模式

透明模式就是不用设置浏览器代理,客户机电脑感觉不到被代理了。

关闭防火墙

systemctl stop firewalld

systemctl disable firewalld

安装iptables

yum install iptables-services –y

开启DNS转发和内核网络转发,其中ens37代表外网网卡的名称

21  iptables -F

22  iptables -t nat -A POSTROUTING -p udp --dport 53 -o  ens37 -j MASQUERADE

23  echo "net.ipv4.ip_forward=1" > /etc/sysctl.conf

24  sysctl –p

修改配置文件

25  vim /etc/squid/squid.conf

在端口后面加上透明模式

http_port 3128 transparent

重启squid

26  systemctl restart squid

iptabls转发80443端口的流量到squid3128端口

27  iptables -t nat -A PREROUTING  -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 3128

28     iptables -t nat -A PREROUTING  -p tcp -m tcp --dport 443 -j REDIRECT --to-ports 3128

转发192.168.136.0内网网段到squid的外网IP上,相当于静态路由,其中ens37代表外网网卡的名称

29  iptables -t nat -A POSTROUTING -s 192.168.136.0/24 -o ens37 -j SNAT --to 192.168.1.3

保存防火墙规则

30  service iptables save

验证:

win7那台电脑的IP手动配置为和squid服务器内网一个网段,并且将网关设置为squid的内网IPDNS不用改,可以用公共DNS,比如阿里223.5.5.5,这时候win7所有上网流量都被交给假网关squid代理。如果之前浏览器设置了局域网代理的要关掉才行。实测钉钉,QQ,看视频等都没异常。

 

查看缓存命中率相关

Squid代理设置好了,尤其是反向代理,希望看看到底有没有起到作用,有没有缓存到等等

就可以通过下面的命令来看

[root@localhost squid]# squidclient -p 80 mgr:info

HTTP/1.0 200 OK

Server: squid/3.1.10

Mime-Version: 1.0

Date: Mon, 26 Nov 2012 14:41:51 GMT

Content-Type: text/plain

Expires: Mon, 26 Nov 2012 14:41:51 GMT

Last-Modified: Mon, 26 Nov 2012 14:41:51 GMT

X-Cache: MISS from cache.361way.com

X-Cache-Lookup: MISS from cache.361way.com:80

Via: 1.0 cache.361way.com (squid/3.1.10)

Connection: close

Squid Object Cache: Version 3.1.10

Start Time:     Mon, 26 Nov 2012 13:18:50 GMT

Current Time:   Mon, 26 Nov 2012 14:41:51 GMT

Connection information for squid:

        Number of clients accessing cache:      34

        Number of HTTP requests received:       774

        Number of ICP messages received:        0

        Number of ICP messages sent:    0

        Number of queued ICP replies:   0

        Number of HTCP messages received:       0

        Number of HTCP messages sent:   0

        Request failure ratio:   0.00

        Average HTTP requests per minute since start:   9.3   #每分钟http request的数量

        Average ICP messages per minute since start:    0.0

        Select loop called: 637052 times, 7.818 ms avg

Cache information for squid:

        Hits as % of all requests:      5min: 77.1%, 60min: 58.8%   #Cache Request命中率

        Hits as % of bytes sent:        5min: 28.4%, 60min: 20.4%   #Cache Byte命中率

        Memory hits as % of hit requests:       5min: 43.9%, 60min: 32.0%

        Disk hits as % of hit requests: 5min: 0.0%, 60min: 0.2%

        Storage Swap size:      328 KB  #存放cache的硬盘使用大小

        Storage Swap capacity:   0.0% used, 100.0% free

        Storage Mem size:       436 KB   #存放cache的内存使用大小

        Storage Mem capacity:    0.1% used, 99.9% free

        Mean Object Size:       6.56 KB

        Requests given to unlinkd:      2

Median Service Times (seconds)  5 min    60 min:

        HTTP Requests (All):   0.06286  0.06286

        Cache Misses:          0.32154  0.20843

        Cache Hits:            0.00000  0.00000

        Near Hits:             0.06286  0.06286

        Not-Modified Replies:  0.00000  0.00000

        DNS Lookups:           0.00000  0.00000

        ICP Queries:           0.00000  0.00000

Resource usage for squid:

        UP Time:        4980.301 seconds

        CPU Time:       1.379 seconds

        CPU Usage:      0.03%

        CPU Usage, 5 minute avg:        0.09%

        CPU Usage, 60 minute avg:       0.03%

        Process Data Segment Size via sbrk(): 7508 KB

        Maximum Resident Size: 59728 KB

        Page faults with physical i/o: 0

Memory usage for squid via mallinfo():

        Total space in arena:    7640 KB

        Ordinary blocks:         7587 KB     61 blks

        Small blocks:               0 KB      0 blks

        Holding blocks:          1228 KB      5 blks

        Free Small blocks:          0 KB

        Free Ordinary blocks:      52 KB

        Total in use:            8815 KB 99%

        Total free:                52 KB 1%

        Total size:              8868 KB

Memory accounted for:   #内存使用情况

        Total accounted:         1253 KB  14%

        memPool accounted:       1253 KB  14%

        memPool unaccounted:     7614 KB  86%

        memPoolAlloc calls:    167272

        memPoolFree calls:     169189

File descriptor usage for squid:

        Maximum number of file descriptors:   1024  #系统最大file descriptor

        Largest file desc currently in use:     39  #目前使用file descriptor最大值

        Number of file desc currently in use:   33  #目前正在使用的file descriptor

        Files queued for open:                   0

        Available number of file descriptors:  991

        Reserved number of file descriptors:   100

        Store Disk files open:                   0

Internal Data Structures:

            77 StoreEntries   #Cache中存放的 object 的数量

            77 StoreEntries with MemObjects  #内存中 object 数量

            76 Hot Object Cache Items #热点object 的数量

            50 on-disk objects # 硬盘上的object的数量

命令具体使用方法

上面的-p后面根的是端口,根据自己具体的情况而定。如果是3128端口,就把上面的80换成3128就行了。另外一些常用的squidclient查询语句如下:

squidclient -p 80 mgr:info      #取得squid运行状态信息:

squidclient -p 80 mgr:mem     #取得squid内存使用情况:

squidclient -p 80 mgr:objects #取得squid已经缓存的列表,使用时要小心,可能会造成squid崩溃:

squidclient -p 80 mgr:diskd  #取得squid的磁盘使用情况:

squidclient -p 80 -m PURGE http://www.361way.com  #强制更新某个url,即清理某连接的squid


最后编辑于:2020/05/02作者: admin

发表评论