概念:一个缓存服务器的守护进程,支持HTTP、FTP、SSL等代理协议
正向代理:代理内网客户端,支持传统模式、透明模式
反向代理:代理公网客户端,支持反向代理模式
算法支持 :轮询(rr)与加权轮询(wrr)
环境:
centos7
关闭防火墙,selinux
配置:
1.传统模式【加速内网用户访问速度,减少出口流量,对客户端较不友好】
(1).squid服务器
#squid服务器
[root@localhost ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.2.202 netmask 255.255.255.0 broadcast 192.168.2.255
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 202.106.2.202 netmask 255.255.255.0 broadcast 202.106.2.255
#安装
[root@localhost yum.repos.d]# yum -y install squid
[root@localhost ~]# systemctl restart squid
(2).公网(模拟)
公网ip
![]()
挂载镜像,安装开启http服务(yum)

开启服务(关闭防火墙)
![]()
(3).真机
配网关
![]()
更改代理


访问公网

2.透明模式【加速内网访问速度,减少出口量,借助防火墙对端口进行重定向操作,对客户端较为友好】
(1).外网同传统模式
(2).squid服务器
[root@localhost ~]# yum -y install iptables-services
#iptables 端口重定向
[root@localhost ~]# systemctl restart iptables
[root@localhost ~]# iptables -t nat -F
[root@localhost ~]# iptables -t nat -Z
[root@localhost ~]# iptables -t nat -X
[root@localhost ~]# iptables -t nat -A PREROUTING -i eth0 -s 192.168.2.0/24 -p tcp --dport 80 -j REDIRECT --to-ports 3128
[root@localhost ~]# service iptables save
#配置squid
[root@localhost ~]# vim /etc/squid/squid.conf
#http_port 3128
#公网接口ip:3128 代理
http_port 192.168.2.202:3128 transparent
[root@localhost ~]# systemctl restart squid
(3).内网配置及测试
[root@localhost ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.2.205 netmask 255.255.255.0 broadcast 192.168.2.255
#添加网关
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
GATEWAY=192.168.2.202
[root@localhost ~]# systemctl restart network
#这里是ping不通的,curl的通外网
[root@localhost ~]# curl 202.106.2.204
202.106
3.反向代理【处理公网用户的请求并代理到内网服务中,有效减轻后端真实服务器压力,增加服务并发能力】
(1).内网服务配置
#内网服务1
[root@localhost ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.2.205 netmask 255.255.255.0 broadcast 192.168.2.255
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# echo "205" > /var/www/html/index.html
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl stop firewalld
#配网关
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
GATEWAY=192.168.2.202
[root@localhost ~]# systemctl restart network
#内网服务2
[root@node2 ~]# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
#与1同理配置网关配合测试服务(2).squid服务器配置
#测试服务
[root@localhost ~]# curl 192.168.2.205
205
[root@localhost ~]# curl 192.168.2.203
203
[root@localhost ~]# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.2.202 netmask 255.255.255.0 broadcast 192.168.2.255
#清理之前的配置
[root@localhost ~]# iptables -t nat -F
[root@localhost ~]# iptables -t nat -Z
[root@localhost ~]# iptables -t nat -X
[root@localhost ~]# service iptables save
#配置负载,这里加权轮询
[root@localhost ~]# vim /etc/squid/squid.conf
acl localnet src 202.106.2.0/24
http_port 202.106.2.202:80 vhost
cache_peer 192.168.2.205 parent 80 0 originserver round-robin proxy-only weight=1
cache_peer 192.168.2.203 parent 80 0 originserver round-robin proxy-only weight=3
[root@localhost ~]# systemctl restart squid
(3).公网测试访问
![]()

版权声明:本文为oschina_41926682原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。