1

使用Squid部署代理服务

 1 year ago
source link: https://blog.51cto.com/lyshark/5992171
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Squid是Linux系统中最为流行的一款高性能代理服务软件,通常用作Web网站的前置缓存服务,能够代替用户向网站服务器请求页面数据并进行缓存.简单来说,Squid服务程序会按照收到的用户请求向网站源服务器请求页面,图片等所需的数据,并将服务器返回的数据存储在运行Squid服务程序的服务器上.当有用户再请求相同的数据时,则可以直接将存储服务器本地的数据交付给用户,这样不仅减少了用户的等待时间,还缓解了网站服务器的负载压力.

配置透明代理

透明二字指的是让用户在没有感知的情况下使用代理服务,这样的好处是一方面不需要用户手动配置代理服务器的信息,进而降低了代理服务的使用门槛,另一方面也可以更隐秘地监督员工的上网行为.
在透明代理模式中,用户无须在浏览器或其他软件中配置代理服务器地址、端口号等信息,而是由DHCP服务器将网络配置信息分配给客户端主机.这样只要用户打开浏览器便会自动使用代理服务了.

以下实验,将配置一个Squid透明代理服务,我们使用10.10.10.20模拟外网,使用win10模拟内网主机.

[主机类型]           [IP地址]             [网卡编号]           [网卡模式]         [作用]

Windows 10           192.168.1.8          eth0                桥接模式           模拟内网

Squid                192.168.1.10         eth0                桥接模式           内网网关
                     10.10.10.10          eth1                仅主机模式         模拟外网网口

Apache               10.10.10.20          eth0                仅主机模式         模拟web服务器

配置Squid网关

1.通过Yum仓库安装Squid代理服务

[root@localhost ~]# yum install -y squid
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager.
Package 7:squid-3.5.20-12.el7.x86_64 already installed and latest version
Nothing to do

2.编辑Squid的住配置文件,在合适的位置写入一下参数,开启透明代理服务

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

 55 # And finally deny all other access to this proxy
 56 http_access deny all
 57 
 58 # Squid normally listens to port 3128
 59 http_port 192.168.1.10:3128 transparent           #IP地址为网关(Squid)内网IP
 60 visible_hostname www.lyshark.com                   #自定义主机名,随意
 61 
 62 
 63 # Uncomment and adjust the following to add a disk cache directory.
 64 #cache_dir ufs /var/spool/squid 100 16 256

3.开启Linux的路由转发功能,并使用sysctl强制刷新内核参数

[root@localhost ~]# echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
[root@localhost ~]# sysctl -p
[root@localhost ~]# echo "1" > /proc/sys/net/ipv4/ip_forward

4.添加一条SNAT防火墙规则,将所有192.168.1.0/24(内网)的请求全部转发到eth1口的3128端口上.

iptables -t nat -A PREROUTING -i eth0(内网网卡) \
-s 192.168.1.0/24 -p tcp --dport 80 \
-j REDIRECT --to-ports 3128

5.启动Squid服务,并设置为开机自启动

[root@localhost ~]# systemctl restart squid
[root@localhost ~]# systemctl enable squid

配置内网客户机

route add default gw 192.168.1.10			#添加一条路由记录(指向网关机eth1)

外网Web配置

1.安装并启动Apache,并启动此处用来模拟外网

yum install -y httpd

systemctl restart httpd

配置反向代理

反向代理服务位于本地web服务器和Internet之间,处理所有对web服务器的请求,组织web服务器和internet的直接通信,这种方式通过降低向web服务器的请求数降低了web服务器的负载.

以下实验,将配置一个Squid反向代理,由于Squid具有静态页面缓存功能,常用作反向代理,减小后端Web主机的压力

[主机类型]           [IP地址]             [网卡编号]           [网卡模式]         [作用]

Windows 10           192.168.1.8          eth0                桥接模式           模拟外网

Squid                192.168.1.10         eth0                桥接模式           外网网口
                     10.10.10.10          eth1                仅主机模式         内网网口

Apache               10.10.10.20          eth0                仅主机模式         模拟web_1
Apache               10.10.10.30          eth0                仅主机模式         模拟web_2

配置两台Web

1.配置两台内网服务器Apache并启动,设置开机自启动

[root@localhost ~]# yum install -y httpd
[root@localhost ~]# echo "web *" >/var/www/html/index.html
[root@localhost ~]# systemctl restart httpd

2.两台Apache添加网关,指向网关IP的eth1(10.10.10.10)口,指定网关就是,告诉数据包从哪里可以出去.

[root@localhost ~]# route add default gw 10.10.10.10

配置Squid代理

1.通过Yum仓库安装Squid代理服务

[root@localhost ~]# yum install -y squid
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager.
Package 7:squid-3.5.20-12.el7.x86_64 already installed and latest version
Nothing to do

2.开启Linux的路由转发功能,并使用sysctl强制刷新内核参数

[root@localhost ~]# echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
[root@localhost ~]# sysctl -p
[root@localhost ~]# echo "1" > /proc/sys/net/ipv4/ip_forward

3.编辑Squid主配置文件,在相应的区域中加入以下语句

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

 58 # Squid normally listens to port 3128
 59 
 60 http_access allow all                                                   #允许所有
 61 
 62 http_port 192.168.1.10:80 vhost                                         #声明外网口地址
 63 
 64 cache_peer 10.10.10.20 parent 80 0 originserver round-robin weight=1    #内网的服务器节点1
 65 cache_peer 10.10.10.30 parent 80 0 originserver round-robin weight=1    #内网的服务器节点2
 66 
 67 # Uncomment and adjust the following to add a disk cache directory.
 68 #cache_dir ufs /var/spool/squid 100 16 256
 69 
 70 # Leave coredumps in the first cache dir

4.启动Squid服务,并设置为开机自启动

[root@localhost ~]# systemctl restart squid
[root@localhost ~]# systemctl enable squid

常用ACL控制参数

#--------------------------------------------------------
# Squid配置文件常用参数

http_port 3128
http_port 192.168.1.1:80                           #80端口只监听内网接口上的请求

cache_mem 512MB                                    #指定非陪多少存储
cache_dir ufs /var/spool/squid 4096 16 256         #指定硬盘缓冲区大小
cache_effective_user squid                         #指定属主squid
cache_effective_group squid                        #指定数组squid
dns_nameservers 8.8.8.8                            #设置有效DNS服务器地址
visible_hostname www.lyshark.com                   #设置主机名,必须设置
cache_access_log /var/log/squid/access.log         #指定访问记录的日志文件
cache_log /var/log/squid/cache.log                 #设置缓存日志文件
cache_store_log /var/log/squid/store.log           #设置网页缓存日志文件
cache_mgr [email protected]                         #管理员邮件
http_access [ allow|deny ]                         #访问控制列表名称

#--------------------------------------------------------
#拒绝所有客户端请求,例子中的all是用户自定义的

acl all src 0.0.0.0/0.0.0.0
http_access deny all

#--------------------------------------------------------
#禁止192.168.1.0/24 网段的客户机上网

acl client src 192.168.1.0/255.255.255.0
http_access deny client

#--------------------------------------------------------
#禁止访问域名www.baidu.com的网站

acl baidu dstdomain www.baidu.com
http_access deny baidu

#--------------------------------------------------------
#禁止192.168.1.0/24网络的用户在周一到周五的9:00 -13:00 上网

acl client src 192.168.1.0/255.255.255.0
acl badtime time MTWHF 9:00-13:00
http_access deny client badtime	

#--------------------------------------------------------
#禁止用户下载 *.mp3 *.exe *.zip *.rar 类型的文件

acl badfile urlpath_reregex -i \.mp3$ \.exe$ \.zip$ \.rar$
http_access deny badfile

#--------------------------------------------------------
#屏蔽www.baidu.com站点

acl badsite dstdomain -i www.baidu.com
http_access deny badsite

#--------------------------------------------------------
#屏蔽包含SEX的URL路径

acl sex url_regex -i SEX
http_access deny sex

#--------------------------------------------------------
#禁止访问22,23,25,53,110,119这些危险端口

acl deny_port port 22 23 25 53 110 119
http_access deny deny_port

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK