關於Squid的概述及傳統代理的配置能夠參考上篇博文:Centos 7安裝Squid代理服務及構建傳統代理web
透明代理提供的服務功能與傳統代理時一致的,可是其「透明」的實現依賴於默認路由和防火牆的重定向策略,所以更適用於局域網主機服務,而不適合Internet中。vim
開始配置透明代理服務器:(環境是接上一篇博文環境作的,不懂的能夠先參考上一篇博文)windows
[root@centos03 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32 <!--修改httpd服務的IP地址、添加網關--> TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static DEFROUTE=yes NAME=ens32 DEVICE=ens32 ONBOOT=yes IPADDR=192.168.200.30 NATEMASK=255.255.255.0 GATEWAY=192.168.200.10 [root@centos03 ~]# systemctl restart network <!--重啓網卡服務-->
[root@centos02 ~]# cp /etc/sysconfig/network-scripts/ifcfg-ens32 /etc/sysconfig/network-scripts/ifcfg-ens34 <!--複製一塊新的網卡配置文件--> [root@centos02 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens34 <!--修改新的網卡配置文件--> TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static DEFROUTE=yes NAME=ens34 <!--修更名字爲ens34--> DEVICE=ens34 <!--修更名字爲ens34--> ONBOOT=yes IPADDR=192.168.200.10 <!--配置IP地址--> NATEMASK=255.255.255.0 [root@centos02 ~]# systemctl restart network <!--從新啓動網卡服務--> [root@centos02 ~]# ping 192.168.200.30 <!--ping命令測試和httpd服務器是否通訊--> PING 192.168.200.30 (192.168.200.30) 56(84) bytes of data. 64 bytes from 192.168.200.30: icmp_seq=1 ttl=64 time=0.350 ms 64 bytes from 192.168.200.30: icmp_seq=2 ttl=64 time=0.564 ms
[root@centos01 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32 <!--修改網卡配置文件--> GATEWAY=192.168.100.20 <!--添加網關--> [root@centos01 ~]# systemctl restart network<!--從新啓動網卡服務 --> [root@centos02 ~]# vim /etc/sysctl.conf <!--開啓路由功能--> net.ipv4.ip_forward = 1 [root@centos02 ~]# sysctl -p <!--驗證路由是否開啓成功--> net.ipv4.ip_forward = 1 [root@centos01 ~]# ping 192.168.200.30 <!--測試是否全網互通--> PING 192.168.200.30 (192.168.200.30) 56(84) bytes of data. 64 bytes from 192.168.200.30: icmp_seq=1 ttl=63 time=0.463 ms 64 bytes from 192.168.200.30: icmp_seq=2 ttl=63 time=0.484 ms
[root@centos02 ~]# iptables -F <!--清除系統自動防火牆--> [root@centos02 ~]# iptables -t nat -F <!--清除系統自動防火牆--> [root@centos02 ~]# iptables -t nat -L <!--查看防火牆規則--> Chain PREROUTING (policy ACCEPT) target prot opt source destination Chain INPUT (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination Chain POSTROUTING (policy ACCEPT) target prot opt source destination [root@centos02 ~]# iptables -t nat -I PREROUTING -i ens32 -s 192.168.100.0/24 -p tcp --dport 80 -j REDIRECT --to 3128 <!--將192.168.100.0網絡的80端口映射到外網的3218端口--> [root@centos02 ~]# iptables -t nat -I PREROUTING -i ens32 -s 192.168.100.0/24 -p tcp --dport 8080 -j REDIRECT --to 3128 <!--將192.168.100.0網絡的8080端口映射到外網的3218端口--> [root@centos02 ~]# iptables -t nat -I PREROUTING -i ens32 -s 192.168.100.0/24 -p tcp --dport 443 -j REDIRECT --to 3128 <!--將192.168.100.0網絡的443端口映射到外網的3218端口--> [root@centos02 ~]# iptables -t nat -I PREROUTING -i ens32 -s 192.168.100.0/24 -p tcp --dport 21 -j REDIRECT --to 3128 <!--將192.168.100.0網絡的21端口映射到外網的3218端口--> [root@centos02 ~]# iptables -t nat -L <!--查看規則是否生效--> Chain PREROUTING (policy ACCEPT) target prot opt source destination REDIRECT tcp -- 192.168.100.0/24 anywhere tcp dpt:ftp redir ports 3128 REDIRECT tcp -- 192.168.100.0/24 anywhere tcp dpt:https redir ports 3128 REDIRECT tcp -- 192.168.100.0/24 anywhere tcp dpt:webcache redir ports 3128 REDIRECT tcp -- 192.168.100.0/24 anywhere tcp dpt:http redir ports 3128 Chain INPUT (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination Chain POSTROUTING (policy ACCEPT) target prot opt source destination [root@centos02 ~]# /etc/init.d/squid stop <!--關閉squid服務--> 正在關閉squid... [root@centos02 ~]# vim /etc/squid.conf <!--修改squid主配置文件支持透明代理--> 60 http_port 192.168.100.20:3128 transparent [root@centos02 ~]# /etc/init.d/squid start <!--啓動squid服務--> 正在啓動squid...
[root@centos01 ~]# unset HTTP_PROXY HTTPS_PROXY FTP_PROXY <!--取消Linux系統的代理--> [root@centos01 ~]# elinks http://192.168.200.30 <!--Linux客戶端使用elinks訪問httpd服務器-->
—————— 本文至此結束,感謝閱讀 ——————centos