前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >01 . Squid原理配置和使用

01 . Squid原理配置和使用

作者头像
iginkgo18
修改2021-06-21 15:08:15
5.2K0
修改2021-06-21 15:08:15
举报
文章被收录于专栏:devops_k8sdevops_k8s
Squid简介

Squid是一个支持HTTP,HTTPS,FTP等服务的Web缓存代理软件,它可以通过缓存页面来提高服务器的相应速度并降低带宽占用。并且,Squid还具有强大的访问控制功能。Squid可以运行在各种操作系统平台上。 Squid会将访问页面的结果缓存在硬盘和内存上。所以Squid对硬盘和内存的空间大小具有较高的要求。内存和硬盘越大,缓存的命中率就越高。但是真实服务器数据是实时更新的,因此,我们就需要不定期的清空缓存数据以保证用户访问结果的准确性。 Squid 将数据元缓存在内存和硬盘中,同时也缓存 DNS 查询的结果。Squid 支持 SSL,支持访问控制。由于使用了 ICP(轻量 Internet 缓存协议),Squid 能够实现层叠的代理阵列,从而最大限度的节约带宽。 Squid Cache(简称 Squid)是一个流行的代理服务器和 Web 缓存服务器软件。Squid 可以做正向代理,也可以做反向代理。官方网站地址为:http://www.squid-cache.org/

Web代理功能
# 1. 将区域网中的主机保护起来
# 2. 对网站的内容缓存,加快客户端的访问速度,减少网络流量
# 3. 静态页面的站点越来越少,可缓存的内容也变少,因为数据库中的信息是不能被缓存.
常用代理服务器类型

代理服务器有多种类型,一般意义上常分为标准正向代理服务器,透明代理服务器,反向代理服务器.

正向代理服务器主要应用于内部网络希望访问外部网络时缓存页面数据, 由于公网IP地址稀缺,企业内部成百上千台计算机不可能同时大欧连接到Internet上,目前的解决方案就是通过一个统一的网络接口连接Internet。Squid就可以提供这样的接口。所有的客户端通过设置代理服务器连接值Squid,通过代理上网。此模型下,Squid主要负责提供缓存加速服务和访问控制的功能。正向代理是对用户的一种操作。

透明代理与传统的正向代理相似,区别在于传统的正向代理需要每个客户端都进行代理服务器的设置。而透明代理通过网关进行部署。即,所有的设置都是由管理员在网关服务器以及代理服务器进行的。因此,透明代理对于用户是透明的,不需要用户进行任何设置。

反向代理结合智能DNS可以实现基本的CDN框架。通过DNS的分离解析功能,可以为不同地区的DNS请求解析不同的结果返回给客户端。用户访问时,感觉不到是在访问代理服务器。由于使用到了缓存技术,当大的并发量到达时,Squid也可以更快的给用户返回响应结果。反向代理结构如下图所示。

Squid代理服务器原理

Squid原理:客户端访问Squid代理服务器,由代理服务器代表客户访问后端真实服务器,真实服务器将响应的数据返回给Squid代理服务器。最后,Squid代理服务器将响应结果返回给客户端,同时将结果缓存在硬盘上及内存中,当客户端再次访问相同的网页时,代理服务器直接将响应结果返回给客户端。

正向代理

当 Squid 做正向代理时,Squid 后面是客户端,客户端想访问外部网络资源,必须经过 Squid。正向代理按其客户端配置方式不同,又可以分为标准(普通)代理模式和透明代理模式。 标准(普通)代理模式,是代理内部网络用户访问 internet 上服务器的连接请求,客户端必须指定代理服务器,并将本来要直接发送到 internet 上服务器的连接请求发送给代理服务器处理。用户在上网时必须在浏览器等软件中填写代理服务器的 IP 地址与端口号信息,否则默认不使用代理。 透明代理模式,是相对于代理服务器而言,客户端不需要做任何和代理服务器相关的设置和操作,对用户而言,感觉不到代理服务器的存在,所以称之为透明代理。即把代理服务器部署在核心的上网出口,当用户上网浏览页面时,会交给代理服务器向外请求,如果结合iptables,可以实现代理+网关+内容过滤+流量安全控制等完整的上网解决方案。

部署Squid服务
yum -y install squid
systemctl restart squid
配置squid

可以使用squid默认的配置文件,因为默认的配置文件其实就已经设置了正向代理,也可以修改默认的配置文件,如下:

[root@squid ~]# cat /etc/squid/squid.conf
# 默认对外端口为3128
http_port 3128

cache_effective_user squid
cache_effective_group squid

# 内网控制,按需修改
acl manager proto cache_object
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 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


# 允许本机访问
http_access allow manager localhost
http_access deny manager

http_access deny !Safe_ports

# 拒绝443以外的端口访问
http_access deny CONNECT !SSL_ports

# 允许内网
http_access allow localnet
http_access allow localhost

http_access allow all

# 设置缓存文件位置、cache目录容量(单位M)、一级缓存目录数量、二级缓存目录数量
cache_dir ufs /var/spool/squid 128 16 256
cache_mem 128 MB
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 .       0       20%     4320


# 重启squid服务
systemctl restart squid
验证代理

可以使用浏览器设置代理的方式,也可以直接使用 curl 命令来测试,其中 -x 选项即 --proxy,添加代理服务器地址和端口,如下:

[root@squid ~]# curl  -xlocalhost:3128 www.baidu.com -I
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Content-Length: 277
Content-Type: text/html
Date: Fri, 29 May 2020 12:40:45 GMT
ETag: "575e1f60-115"
Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
X-Cache: MISS from squid
X-Cache-Lookup: MISS from squid:3128
Via: 1.1 squid (squid/3.5.20)
Connection: keep-alive
ACL访问控制

只允许 IP 地址为 39.108.140.0 的客户端使用服务器上的 squid 服务程序提供的代理服务,禁止其余所有的主机代理请求 编辑 /etc/squid/squid.conf,添加如下设置:

acl client src 39.108.140.0
http_access allow client
http_access deny all

[root@blog ~]# curl -x49.233.69.195:3128 -I www.baidu.com
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Content-Length: 277
Content-Type: text/html
Date: Fri, 29 May 2020 12:35:27 GMT
ETag: "575e1f60-115"
Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
X-Cache: MISS from squid
X-Cache-Lookup: MISS from squid:3128
Via: 1.1 squid (squid/3.5.20)
Connection: keep-alive

    
# 我们试一下其他IP服务器
[root@jd ~]# curl -x49.233.69.195:3128 www.baidu.com -I
HTTP/1.1 403 Forbidden
Server: squid/3.5.20
Mime-Version: 1.0
Date: Fri, 29 May 2020 12:45:44 GMT
Content-Type: text/html;charset=utf-8
Content-Length: 3513
X-Squid-Error: ERR_ACCESS_DENIED 0
Vary: Accept-Language
Content-Language: en
X-Cache: MISS from squid
X-Cache-Lookup: NONE from squid:3128
Via: 1.1 squid (squid/3.5.20)
Connection: keep-alive
禁止所有客户端访问网址包含baidu关键词网站

编辑 /etc/squid/squid.conf,添加如下设置

acl deny_keyword url_regex -i baidu
http_access deny deny_keyword

systemctl restart squid

curl -x49.233.69.195:3128 -I www.baidu.com
HTTP/1.1 403 Forbidden
Server: squid/3.5.20
Mime-Version: 1.0
Date: Fri, 29 May 2020 12:50:39 GMT
Content-Type: text/html;charset=utf-8
Content-Length: 3511
X-Squid-Error: ERR_ACCESS_DENIED 0
Vary: Accept-Language
Content-Language: en
X-Cache: MISS from squid
X-Cache-Lookup: NONE from squid:3128
Via: 1.1 squid (squid/3.5.20)
Connection: keep-alive


curl -x49.233.69.195:3128 -I https://www.cnblogs.com/you-men/
HTTP/1.1 200 Connection established

HTTP/1.1 200 OK
Date: Fri, 29 May 2020 12:50:06 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Vary: Accept-Encoding
禁止下载带有某些后缀的文件,比如.rar和.avi
acl badfile urlpath_regex -i \.rar$ \.avi$
http_access deny badfile

systemctl restart squid
# 此处就不测试了
反向代理

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

网站页面是由静态资源和动态资源一起组成的,其中静态资源包括网站架构 CSS/js 文件、大量的图片、视频等数据,这些数据相对于动态资源来说更加稳定,一般不会经常发生改变。但是随着建站技术的更新换代,外加人们不断提升的审美能力,这些静态资源占据的网站空间也越来越多。如果能够把这些静态资源从网站页面中抽离出去,然后在全国各地部署静态资源的缓存节点,这样不仅可以提升用户访问网站的速度,而且网站源服务器也会因为这些缓存节点的存在而降低负载。 反向代理是 Squid 服务程序的一种重要模式,其原理是把一部分原本向网站源服务器发起的用户请求交给 Squid 服务器缓存节点来处理。但是这种技术的弊端也很明显,如果有心怀不轨的人将自己的域名和服务器反向代理到某个知名的网站上面,从理论上来讲,当用户访问到这个域名时,也会看到与那个知名网站一样的内容(有些诈骗网站就是这样骗取用户信任的)。因此,当前许多网站都默认禁止了反向代理功能。开启了 CDN(内容分发网络)服务的网站也可以避免这种窃取行为。如果访问开启了防护功能的网站,一般会提示 500 错误。 将端口号 "http_port 3128" 修改为 "http_port 80 accel vhost vport",此时正向解析会被暂停,它不能与反向代理服务同时使用,然后再增加你要代理的后端真实服务器的信息。 编辑 /etc/squid/squid.conf,添加如下设置:

配置squid
# 监听端口
# 修改端口80 accel vhost vport 为反向代理
http_port 80  accel vhost vport
# cache_peer 代理IP 端口 0 originserver  name=a "a"代表一个域名
cache_peer 39.108.140.0 parent 80 0 originserver name=a
cache_peer 116.196.83.113  parent 80 0 originserver name=b
# 设置a的域名为 www.qq.com
#cache_peer_domain a www.qq.com
#cache_peer_domain b www.baidu.com
# acl权限控制
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目录容量(单位M)、一级缓存目录数量、二级缓存目录数量
cache_dir ufs /var/spool/squid 128 16 256
cache_mem 128 MB
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 .       0       20%     4320

systemctl restart squid
# 我们访问squid,可以看到代理我另一台博客服务器去了
Squid常用命令选项
# 常用的选项就是上面提到的 squid -k check,squid -k reconfigure 和 squid -k parse,这些命令均可以简写成:

# squid -kche(或 squid -k che),表示检查 squid.conf 配置文件是否有语法或配置错误。

# squid -krec(或 squid -k rec),表示当 squid.conf 进行过配置更改后,可以使用该命令进行 squid 配置重载,而不用重启 squid 服务,相当于热加载。

# squid -kpar(或 squid -k par),表示如果 squid.conf 有语法或配置错误,这里会返回提示你,如果没有返回,尝试启动 squid。
Squid+HAProxy+Keepalived

使用HAProxy的目的

haproxy是负载均衡器,它可以将网络请求按一定算法均匀分布到各个代理服务器(这里指正向代理)上,防止压力都集中在某一个节点上,提高整体的服务的负载能力。 假设有103台主机需要出外网,其中只有2台能直接访问外网,那么我们接着假设这两台主机的IP分别是192.168.29.210, 192.168.29.211,且已经在上面安装好squid代理服务。 还有一台主机我们将其选做负载均衡所在的机器,假设其IP为192.168.29.212。 按上述做法,在其余100台主机上可以设置代理(IE “Internet选项->连接->局域网设置->代理服务器” )为192.168.29.212,haproxy负载均衡器会将网络请求按一定算法均匀打到192.168.29.210, 192.168.29.211两台代理服务器上。

安装配置Squid
yum -y install squid
cat /etc/squid/squid.conf
cat /etc/squid/squid.conf
acl localnet src 10.0.0.0/8	
acl localnet src 172.16.0.0/12
acl localnet src 192.168.0.0/16
acl localnet src fc00::/7     
acl localnet src fe80::/10   
acl SSL_ports port 443
acl Safe_ports port 80	
acl Safe_ports port 21
acl Safe_ports port 443
acl Safe_ports port 70
acl Safe_ports port 210
acl Safe_ports port 1025-65535
acl Safe_ports port 280
acl Safe_ports port 488
acl Safe_ports port 591
acl Safe_ports port 777
acl CONNECT method CONNECT
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow localhost manager
http_access deny manager
http_access allow localnet
http_access allow localhost
http_access deny all
http_port 80 
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 .		0	20%	4320

systemctl restart squid
curl -x192.168.0.103:80 www.baidu.com -I
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Content-Length: 277
Content-Type: text/html
Date: Sat, 30 May 2020 06:24:34 GMT
ETag: "575e1f59-115"
Last-Modified: Mon, 13 Jun 2016 02:50:01 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
X-Cache: MISS from squid-1
X-Cache-Lookup: MISS from squid-1:80
Via: 1.1 squid-1 (squid/3.5.20)
Connection: keep-alive
安装配置HAProxy
yum -y install haproxy
cat /etc/haproxy/haproxy.cfg 
global
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
    stats socket /var/lib/haproxy/stats
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000
frontend http-in 
    bind *:8000
    default_backend servers
backend servers
    balance leastconn 
    server server1 192.168.171.134:80
    server server2 192.168.171.141:80

systemctl restart haproxy


curl -x192.168.171.141:8000 -I www.baidu.com
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Content-Length: 277
Content-Type: text/html
Date: Sat, 30 May 2020 07:53:13 GMT
ETag: "575e1f5d-115"
Last-Modified: Mon, 13 Jun 2016 02:50:05 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
X-Cache: MISS from squid-1
X-Cache-Lookup: MISS from squid-1:80
Via: 1.1 squid-1 (squid/3.5.20)
安装配置Keepalived
yum -y install keepalived

cat /etc/keepalived/keepalived.conf 
global_defs {
     router_id node103
}

vrrp_script chk_squid_port {
     script "/etc/keepalived/scripts/check_squid.sh"
     interval 2
     weight 2
}

vrrp_instance chk_squid {
     state BACKUP 
     interface ens32
     virtual_router_id 20
     priority 99
     advert_int 1
     authentication {
        auth_type PASS
        auth_pass squid123
     }

     track_script {
        chk_squid_port
     }

     virtual_ipaddress {
        192.168.171.250
     }
}

mkdir -p /etc/keepalived/scripts/
mkdir -p /etc/keepalived/log/

cat /etc/keepalived/scripts/check_squid.sh 
#!/bin/bash

START_SQUID="systemctl start squid.service"
STOP_KEEPALIVED="systemctl stop keepalived.service"
LOGFILE="/etc/keepalived/log/keepalived-squid-state.log"

HAPS=`ps -C squid --no-header |wc -l`

if [ $HAPS -eq 0 ];then
   echo $(date "+%Y-%m-%d %H:%M:%S") >> $LOGFILE
   echo "start squid failed, killall keepalived" >> $LOGFILE
   $STOP_KEEPALIVED

fi
chmod a+x /etc/keepalived/scripts/check_squid.sh
systemctl restart keepalived

curl -x192.168.171.250:80 -I www.baidu.com
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Content-Length: 277
Content-Type: text/html
Date: Sat, 30 May 2020 07:48:47 GMT
ETag: "575e1f5d-115"
Last-Modified: Mon, 13 Jun 2016 02:50:05 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
X-Cache: MISS from squid-1
X-Cache-Lookup: MISS from squid-1:80
Via: 1.1 squid-1 (squid/3.5.20)
Connection: keep-alive

# 我们模拟其中一台squid宕机, 虚拟IP会漂移到另一台机器,依然能提供服务支持    
systemctl stopo squid    

还有一种是如果正向代理服务器多, 可以用haproxy反代一下, 将正向代理的请求分发到不同的squid, 在haproxy的基础上做高可用

cat /etc/keepalived/keepalived.conf 
global_defs {
     router_id node104
}

vrrp_script chk_http_port {
     script "/etc/keepalived/scripts/check_haproxy.sh"
     interval 2
     weight 2
}

vrrp_script chk_squid_port {
     script "/etc/keepalived/scripts/check_squid.sh"
     interval 2
     weight 2
}

vrrp_instance chk_squid {
     state MASTER
     interface ens32
     virtual_router_id 20
     priority 100
     advert_int 1
     authentication {
        auth_type PASS
        auth_pass squid123
     }

     track_script {
        chk_squid_port
     }

     virtual_ipaddress {
        192.168.171.250
     }
}


cat /etc/keepalived/scripts/check_haproxy.sh 
#!/bin/bash

START_HAPROXY="systemctl start haproxy"
STOP_KEEPALIVED="systemctl stop keepalived.service"
LOGFILE="/etc/keepalived/log/keepalived-haproxy-state.log"

HAPS=`ps -C haproxy --no-header |wc -l`

#如果没有haproxy进程存在,就启动haproxy,停止keepalived,并写入日志
#if [ $HAPS -eq 0 ];then
#   echo $(date "+%Y-%m-%d %H:%M:%S") >> $LOGFILE
#   echo $START_HAPROXY >> $LOGFILE
#   $START_HAPROXY >> $LOGFILE 2>&1
#   sleep 3
#fi

if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then
   echo $(date "+%Y-%m-%d %H:%M:%S") >> $LOGFILE
   echo "start haproxy failed, killall keepalived" >> $LOGFILE
   $STOP_KEEPALIVED
fi
chmod a+x /etc/keepalived/scripts/check_haproxy.sh
systemctl restart keepalived

curl -x192.168.171.250:8000 -I www.baidu.com
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Content-Length: 277
Content-Type: text/html
Date: Sat, 30 May 2020 08:08:33 GMT
ETag: "575e1f5c-115"
Last-Modified: Mon, 13 Jun 2016 02:50:04 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
X-Cache: MISS from squid-1
X-Cache-Lookup: MISS from squid-1:80
Via: 1.1 squid-1 (squid/3.5.20)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-05-31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Squid简介
    • Web代理功能
    • 常用代理服务器类型
    • Squid代理服务器原理
    • 正向代理
      • 部署Squid服务
        • 配置squid
          • 验证代理
            • ACL访问控制
              • 禁止所有客户端访问网址包含baidu关键词网站
                • 禁止下载带有某些后缀的文件,比如.rar和.avi
                • 反向代理
                  • 配置squid
                  • Squid常用命令选项
                  • Squid+HAProxy+Keepalived
                    • 安装配置Squid
                      • 安装配置HAProxy
                        • 安装配置Keepalived
                        相关产品与服务
                        负载均衡
                        负载均衡(Cloud Load Balancer,CLB)提供安全快捷的流量分发服务,访问流量经由 CLB 可以自动分配到云中的多台后端服务器上,扩展系统的服务能力并消除单点故障。负载均衡支持亿级连接和千万级并发,可轻松应对大流量访问,满足业务需求。
                        领券
                        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档