代理服务 SQUID 测试

第一部分:SQUID基础

clip_image001

clip_image001[7]

clip_image001[9]

clip_image001[11]

Squid代理服务的基本配置:

http_port 3128                    #设置监听的IP与端口号
cache_mem 64 MB                    #额外提供给squid使用的内存,推荐大小为物理内存的1/3-1/2或更多
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 %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>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 qin1.qin.com            #主机名
cache_mgr qin@qin.com                #管理员邮箱

clip_image001[15]

常用的ACL列表类型:

src              源地址
dst              目标地址
port             端口
srcdomain        源域
dstdomain        目标域
time             访问时间
maxconn          最大并发连接
url_regex        目标URL地址
urlpath_regex    整个目标URL路径
acl LAN1 src 192.168.1.0/24
acl PCRang  src  192.168.1.10-192.168.1.20/24
acl PC1 src 192.168.1.66/32
acl Blk_Domain dstdomain .qq.com .kaixin001.com
acl Work_Hours time MTWHF 08:30-17:30
acl Max20_Conn maxconn 20
acl Blk_URL url_regex -i ^rtsp:// ^mms://
acl ksuurl url_regex ^http://www.ksu.edu.tw/cht/.*
acl Blk_Words urlpath_regex -i sex adult
acl RealFile urlpath_regex -i \.rmvb$ \.rm$
acl OverConnLimit maxconn 6

clip_image001[17]

第二部分:准备测试环境

物理机为Win10系统,网关192.168.1.1,自动获取IP地址上网。
三台虚拟机设置如下:

image
1、RHEL2 作为外网服务器,桥接到物理机无线网卡上,设置固定IP为 192.168.1.200

1
#hostnamectl set-hostname rhel2.rusky.com
#nmcli connection add type ethernet con-name enp0s3 ifname enp0s3 autoconnect yes
#nmcli connection modify enp0s3 ipv4.addresses 192.168.1.200/24 ipv4.gateway 192.168.1.1 ipv4.dns 211.139.29.170 ipv4.method manual connection.autoconnect yes

2、RHEL1 作为内网的一台SQUID代理服务器,部属squid服务,同时添加两块网卡,一块插到内网“交换机 VirtualBox Host-Only Network #6”,另外一块连接到内网(桥接到物理机无线网卡上),
如下图所示:
虚拟网卡 VirtualBox Host-Only Network #6 作为内网的交换机(IP为192.168.100.100)。

001

002

003

#hostnamectl set-hostname rhel1.rusky.com

# nmcli connection down enp0s3  --设置内网IP
# nmcli connection modify enp0s3 ipv4.addresses 192.168.100.1/24 ipv4.gateway 192.168.100.100 ipv4.dns 192.168.100.1 ipv4.method manual connection.autoconnect yes

# nmcli connection down enp0s8   --设置外网IP
# nmcli connection modify enp0s8 ipv4.addresses 192.168.1.201/24 ipv4.gateway 192.168.1.1 ipv4.dns 211.139.29.170 ipv4.method manual connection.autoconnect yes

3、RHEL3作为内网的另外一台客户端服务器,插到内网“交换机 VirtualBox Host-Only Network #6”,用于测试。

4

主机名:rhel3.rusky.com IP:192.168.100.3

=================================

第三部分:在外网服务器RHEL2上架设测试网站,在RHEL1上配置SQUID代理,然后从RHEL3通过代理来访问RHEL2上架设的测试网站。之后完成一些简单的测试。

1、在RHEL2上架设测试网站

#yum install httpd -y
#firewall-cmd --permanent --add-service=http
#firewall-cmd --reload
#echo lxjtest > /var/www/html/index.html
#systemctl restart httpd

2、在RHEL1上配置squid服务器

#yum install -y squid
#firewall-cmd --permanent --add-port=3128/tcp
#firewall-cmd --reload
#systemctl restart squid

最简单的代理服务器,安装完成后,啥也不用修改,默认就可以使用了。内网其它客户机把RHEL1当做跳板来访问外网RHEL2上架设的网站。

3、在客户机RHEL3上访问测试

直接Ping RHEL2的IP 192.168.1.200 是不通的。
浏览器未设置代理服务器之前也无法访问RHEL2 192.168.1.200 上的网站,设置代理服务器为192.168.100.1:3128之后就可以访问了。(这就是正向代理)

image

SQUID初始化:

#vi /etc/squid/squid.conf
cache_dir ufs /var/spool/squid 100 16 256    #取消注释,再执行squid -z才会建立,如果不启用,则只使用内存,生产环境中一定要取消注释该行。
#squid -k parse  #检测配置文件
#systemctl stop squid
#squid -z  #重建缓存,在/var/spool/squid下生成很多缓存目录。

[root@rhel1 squid]# pwd
/var/spool/squid
[root@rhel1 squid]# ls
00  01  02  03  04  05  06  07  08  09  0A  0B  0C  0D  0E  0F  swap.state
[root@rhel1 squid]# ls 00/
00  0A  14  1E  28  32  3C  46  50  5A  64  6E  78  82  8C  96  A0  AA  B4  BE  C8  D2  DC  E6  F0  FA
01  0B  15  1F  29  33  3D  47  51  5B  65  6F  79  83  8D  97  A1  AB  B5  BF  C9  D3  DD  E7  F1  FB
02  0C  16  20  2A  34  3E  48  52  5C  66  70  7A  84  8E  98  A2  AC  B6  C0  CA  D4  DE  E8  F2  FC
03  0D  17  21  2B  35  3F  49  53  5D  67  71  7B  85  8F  99  A3  AD  B7  C1  CB  D5  DF  E9  F3  FD
04  0E  18  22  2C  36  40  4A  54  5E  68  72  7C  86  90  9A  A4  AE  B8  C2  CC  D6  E0  EA  F4  FE
05  0F  19  23  2D  37  41  4B  55  5F  69  73  7D  87  91  9B  A5  AF  B9  C3  CD  D7  E1  EB  F5  FF
06  10  1A  24  2E  38  42  4C  56  60  6A  74  7E  88  92  9C  A6  B0  BA  C4  CE  D8  E2  EC  F6
07  11  1B  25  2F  39  43  4D  57  61  6B  75  7F  89  93  9D  A7  B1  BB  C5  CF  D9  E3  ED  F7
08  12  1C  26  30  3A  44  4E  58  62  6C  76  80  8A  94  9E  A8  B2  BC  C6  D0  DA  E4  EE  F8
09  13  1D  27  31  3B  45  4F  59  63  6D  77  81  8B  95  9F  A9  B3  BD  C7  D1  DB  E5  EF  F9

#systemctl start squid
#tail -f /var/log/squid/access.log 查看访问日志
#tail -f /var/log/squid/cache.log  查看cache日志

测试一:通过SQUID服务来阻止某一网段或固定IP的客户端访问测试网站。

在RHEL1上修改SQUID配置文件,增加两行:

# vi /etc/squid/squid.conf

# Example rule allowing access from your local networks.
# Adapt to list your (internal) IP networks from where browsing
# should be allowed
acl lxjtest1 src 192.168.100.3   #也可以设置成一个网段
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 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
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

#
# Recommended minimum Access Permission configuration:
#
# Deny requests to certain unsafe ports
http_access deny lxjtest1
http_access deny !Safe_ports

# Deny CONNECT to other than secure SSL ports

添加完上面的两行后,重启SQUID服务。

在RHEL3客户机上访问测试不通,提示被拒绝。如果把上面配置文件第二行修改为allow,则访问正常。这个设置规则可以参照基础部分的列表类型来进行设置。

image

但是在其它客户端上通过代理访问测试网站正常。

测试二:通过一个列表文件来限制访问

创建一个列表文件:

# vi /etc/squid/ipblock.list  --名称随便写,把需要限制的IP地址加进来
192.168.100.3
192.168.200/24

#vi /etc/squid/squid.conf --修改主配置文件,添加里面两行内容

# Example rule allowing access from your local networks.
# Adapt to list your (internal) IP networks from where browsing
# should be allowed
acl ipblocktest  src "/etc/squid/ipblock.list"
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 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
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

#
# Recommended minimum Access Permission configuration:
#
# Deny requests to certain unsafe portsa
http_access deny ipblocktest
http_access deny !Safe_ports

重启squid服务

# systemctl restart squid.service

通过客户端访问测试,可以看到我们配置的限制列表文件已生效了。

生产环境中经常通过限制域名的方式来限制用户对外网的访问,比如,新建一个/etc/squid/domainblock.list文件,添加如下内容:
.taobao.com
.qq.com
.jd.com
.toutiao.com
等等来限制员工对外网的访问。主配置文件添加下面的两行:

acl domainblock dstdomain "/etc/squid/domainblock.list"

http_access deny domainblock

从基础内容部分的列表类型可以看出,我们还可以限制用户在指定的时间范围内对外网的访问。SQUID确实功能挺强大。

测试三:ACL列表的另外一种写法

# vi /etc/squid/squid.conf

acl allowclienttest src 192.168.100.3 192.168.100.123   --可以添加多个IP,空格分隔
http_access allow allowclienttest
http_access deny !allowclienttest     --加!符号表示拒绝非allowclienttest指定的IP客户端访问。

测试四:通过时间范围控制对外网的访问

只允许指定客户端在指定时间范围内访问,否则全部拒绝。

# vi /etc/squid/squid.conf

acl allowclienttest src 192.168.100.111 192.168.100.10 192.168.100.123
acl allowtimetest time mtwhf 9:00-17:00  --指定周一至周五(周六a,周天s),9点至17点的时间范围
http_access allow allowclienttest allowtimetest
http_access deny all     --拒绝所有

实验五、SQUID安全机制:通过htpasswd工具生成认证

其实就是通过apache httpd自带的htpasswd工具创建一个存有用户名和密码的认证文件。之后,配置squid启动密码认证机制 auth_param basic program,用自己的basic_ncsa_auth命令去调认证文件。即当你在访问代理服务时,需要输入管理员给你指定的用户名和密码才能访问。如果不这么做,则任何用户只要知道squid服务器的IP和端口就可以通过代理服务器上网,这样也不安全。

在RHEL1上

安装httpd-tool工具
# yum install httpd-tools.x86_64

查看认证程序命令位置

[root@rhel1 ~]# rpm -ql squid | grep ncsa
/usr/lib64/squid/basic_ncsa_auth
/usr/share/man/man8/basic_ncsa_auth.8.gz

创建隐藏的用户密码文件userlist,并添加用户testusersquid1

[root@rhel1 ~]# htpasswd -c /etc/squid/.userlist testusersquid1  #-c 创建
New password:
Re-type new password:
Adding password for user testusersquid1

添加testusersquid2用户

[root@rhel1 ~]# htpasswd  /etc/squid/.userlist testusersquid2 #添加用户testusersquid2
New password:
Re-type new password:
Adding password for user testusersquid2

(不用加-c参数,否则会把前边加的用户覆盖了)。如果加参数m,则使用MD5加密,有时候客户端不支持,所以一般不加m参数。

查看认证文件:

[root@rhel1 ~]# cat /etc/squid/.userlist
testusersquid1:$apr1$CRSWnTTt$XnSYo.q3JHgdn5WTRUzwZ/
testusersquid2:$apr1$dG69l9Q/$al8O2ziswVuAIM7gw/otO.

修改主配置文件,加入下面的内容:

#vim /etc/squid/squid.conf
auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid/.userlist   #启动密码认证机制,用basic_ncsa_auth命令去调用userlist文件。
auth_param basic children 100
auth_param basic realm Welcome to  lxjtest.rusky.com  web server
auth_param basic credentialsttl 2 hours
acl auth_user proxy_auth REQUIRED
http_access allow auth_user

#当用户在访问代理访问时,需要输入userlist文件中的配置的用户名和密码才能访问。

image

测试六:透明代理

前面使用的代理方式上网都很麻烦,客户端浏览器必须自己配置代理服务器的IP地址和端口号才能上网。而透明代理则通过在服务器上配置策略,强制用户走代理服务器上网,所以客户端浏览器不需要做任何设置。实现原理:配置防火墙端口转发策略来限制用户上网时自动转发到代理服务器的3128端口。

修改主配置文件:

#vim /etc/squid/squid.conf
#http_port 3128        #这一行是普通的正向代理,把这行注释掉,如果不注释会和下面冲突
http_port 192.168.100.1:3128 transparent    #透明代理加上这行。任何用户访问192.168.100.1的3128端口,则自动走透明代理服务。

添加防火墙策略:

如果采用iptables做防火墙,需要打开路由转发

#iptables -t nat -A PREROUTING -i eth0 -s 192.168.100.0/24 -p tcp --dport 80 -j REDIRECT --to-ports 3128
#iptables -t nat -A PREROUTING -i eth0 -s 192.168.100.0/24 -p tcp --dport 443 -j REDIRECT --to-ports 3128

如果采用firewalld做防火墙

#firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.100.0/24" forward-port port="80" protocol="tcp" to-port="3128" to-addr="192.168.100.1""

#firewall-cmd –reload 重新加载防火墙策略
#内网用户192.168.100.0/24网段的客户机如果上外网,则自动转向代理服务器。

建议采用图形化的方法添加富规则
#firewall-config
#family选择ipv4
#element选择forward-port 规则选择来源tcp/80转发到另一个ip为192.168.100.1的3128端口
#source选择192.168.100.0/24

image

image

测试七:反向代理

反射代理原理:假设 www.cnblogs.com(IP为192.168.1.201) 网站把试验中的RHEL1作为反向代理服务器,两块网卡,一块连内网,另外一块连外网(供用户通过外网访问),上面安装SQUID服务,未部属Apache httpd。然后他们公司内网的4台服务器(192.168.100.2~4)共享同一存储,每台服务器上都部属Apache httpd 服务。当有用户访问www.cnblogs.com(192.168.1.201)网站时,RHEL1上的反向代理服务会从内网的4服务器中的其中一台上把网页反向发送给用户,内网的4个网站由于共享存储,所以访问到任何一台服务器上时,网页内容都是一致的。反向代理服务器RHEL1还起到一个“负载均衡”的作用。

image

在RHEL1上安装squid,并修改主配置文件如下:

#vim /etc/squid/squid.conf
http_access allow all       #取消之前的规则,开启全部允许规则
#http_port 3128             #注释掉正向代理,防止冲突
http_port  192.168.1.201:80 vhost    #当有客户访问192.168.1.201 http 服务时,系统反向代理解析为下面的计算机

cache_peer 192.168.100.2 parent 80 0 originserver weight=5 max-conn=30
cache_peer 192.168.100.3 parent 80 0 originserver weight=5 max-conn=30
cache_peer 192.168.100.4 parent 80 0 originserver weight=5 max-conn=30
cache_peer 192.168.100.5 parent 80 0 originserver weight=5 max-conn=30

如果有多台服务器,就添加多条记录。weight 是权重,比如其中一台服务器的性能比较好,可以把这台服务器的 weight 值设置高一些。max-conn是最大连接数,当连接数达到最大值时,自动“负载”到另外一台服务器。如果其中某一台服务器上的httpd服务停止了,反向代理服务器也会自动到访问其它内网服务器。

# firewall-cmd --add-port=80/tcp –permanent   --还要添加防火墙策略,允许80端口。虽然RHEL1服务器并未部属httpd服务,但是它要反向代理内网的httpd服务,所以得开放80端口。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值