透明代理提供的服务功能与传统代理时一致的,可是其“透明”的实现依赖于默认路由和防火墙的重定向策略,所以更适用于局域网主机服务,而不适合Internet中。vim
网络环境以下:服务器
项目大概流程以下:网络
开始配置透明代理服务器:tcp
squid服务的安装及传统代理的实施过程请参考博文:http://www.javashuo.com/article/p-qkkbbuph-gm.html ide
一、配置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
二、设置firewalld的重定向策略:
透明代理中的squid服务实际上时构建在Linux网关主机上的,所以只需设置正确的防火墙策略,就能够将内网主机访问Internet的数据包转交给squid进行处理,这就须要用到了防火墙的IP假装与端口转发策略,其做用是实现本机端口的重定向,将访问网站协议http、https的外发数据包转交给本机的squid服务(3128端口)ui
[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
三、客户机验证:
客户机的网关需指向192.168.1.1的主机。spa