squid服务之配置透明代理

透明代理提供的服务功能与传统代理时一致的,但是其“透明”的实现依赖于默认路由和防火墙的重定向策略,因此更适用于局域网主机服务,而不适合Internet中。

网络环境如下:

squid服务之配置透明代理

项目大概流程如下:

  1. 在Linux网关上,构建squid为客户机访问Internet提供代理服务。
  2. 在局域网所有的客户机上,只需有正确的IP地址、默认网关和DNS,不需要手动指定代理服务器的地址、端口等信息(若指定了反而易出错)。关于客户机的DNS解析工作,最好还是通过正常的DNS服务器来提供,不建议抛给代理服务器来处理。

开始配置透明代理服务器:

squid服务的安装及传统代理的实施过程请参考博文:https://blog.51cto.com/14154700/2406060

1、配置squid支持透明代理:

[root@localhost /]# vim /etc/squid.conf
                  ..................
http_port 192.168.1.1:3128 transparent        #只在其中一个IP地址上提供服务,并支持透明模式
cache_effective_user squid
cache_effective_group squid
                 ......................

[root@localhost ~]# systemctl restart squid
[root@localhost ~]# vim /etc/sysctl.conf
          ....................
net.ipv4.ip_forward = 1             #开启路由转发功能,实现本机中不同网段的地址转发
[root@localhost ~]# sysctl -p
net.ipv4.ip_forward = 1

2、设置firewalld的重定向策略:
透明代理中的squid服务实际上时构建在Linux网关主机上的,因此只需设置正确的防火墙策略,就可以将内网主机访问Internet的数据包转交给squid进行处理,这就需要用到了防火墙的IP伪装与端口转发策略,其作用是实现本机端口的重定向,将访问网站协议http、https的外发数据包转交给本机的squid服务(3128端口)

[root@localhost ~]# systemctl start firewalld
[root@localhost ~]# firewall-cmd --zone=external --add-interface=ens33
The interface is under control of NetworkManager, setting zone to 'external'.
success
[root@localhost ~]# firewall-cmd --zone=internal --add-interface=ens37
The interface is under control of NetworkManager, setting zone to 'internal'.
success
[root@localhost ~]# firewall-cmd --zone=external --add-service=http
success
[root@localhost ~]# firewall-cmd --zone=external --add-service=https
success
[root@localhost ~]# firewall-cmd --zone=external --add-port=3128/tcp
success
[root@localhost ~]# firewall-cmd --direct --add-rule ipv4 nat PREROUTING 0 -i ens33 -p tcp --dport 80 -j REDIRECT --to-ports 3128
success
[root@localhost ~]# firewall-cmd --direct --add-rule ipv4 nat PREROUTING 0 -i ens33 -p tcp --dport 443 -j REDIRECT --to-ports 3128
success
[root@localhost ~]# firewall-cmd --runtime-to-permanent
success

3、客户机验证:
客户机的网关需指向192.168.1.1的主机。
squid服务之配置透明代理

squid服务之配置透明代理

猜你喜欢

转载自blog.51cto.com/14154700/2406121