1、缓存服务器的原理
2、squid正向代理
3、squid透明代理
4、squid反向代理
1、缓存服务器的原理(ftp/http)
2、squid正向代理
3、squid透明代理
squid
iptables redirect
route/SNAT
(1)、在代理服务器上开启路由
[root@VM_16_17_centos ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
(2)、在代理服务器再开启SNAT
[root@VM_16_17_centos ~]# iptables -t nat -A POSTROUTING -o eth1 -s 192.168.0.0/24 -j SNAT --to 192.168.100.253
[root@VM_16_17_centos ~]#service iptables save
(3)、配置客户端的网关指向代理服务器内网IP地址
略
(4)、安装squid服务器配置正向代理
[root@VM_16_17_centos ~]# yum -y install squid
[root@VM_16_17_centos ~]# vim /etc/squid/squid.conf
1575 cache_mem 100 MB --设置缓存中占用物理内存的大小
1782 cache_dir ufs /var/spool/squid 100 16 256 --磁盘缓存的路径及大小,一级目录的个数和二级目录的个数
3002 visible_hostname nis.uplooking.com --设置squid的主机名
590 acl lannet src 192.168.0.0/24 --定义一个源地址变量
637 http_access allow localhost
638 http_access allow lannet --允许局域网的主机可以使用我们的squid代理
639 http_access deny all
[root@VM_16_17_centos ~]# service squid start
[root@VM_16_17_centos ~]# netstat -tnlp | grep squid
tcp 0 0 0.0.0.0:3128 0.0.0.0:* LISTEN 27238/(squid)
(5)、配置客户端的浏览使用squid代理上网
略
(6)、在服务器上配置透明代理
[root@VM_16_17_centos ~]# vim /etc/squid/squid.conf --配置squid工作在透明代理模式
921 http_port 3128 transparent
[root@VM_16_17_centos ~]# service squid restart
(7)、联动iptables实现真正的代理(通过端口重定向)
[root@VM_16_17_centos ~]# iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to 3128
(8)、测试
1)关闭路由,网页能被访问,但ping不通外网的主机
2)关闭squid,网页不能访问,其它的外网功能都能正常被访问
(9)、在代理服务器上实现DNAT,发布内网web服务器
[root@VM_16_17_centos ~]#iptables -t nat -A PREROUTING -i eth1 -d 192.168.100.253 -p tcp --dport 80 -j DNAT --to 192.168.0.254:80
(10)、测试
[root@VM_16_17_centos ~]#elinks -dump 1 http://192.168.100.253
[root@VM_16_17_centos ~]# tail -1 /opt/nginx/logs/access.log
192.168.100.110 - - [11/Sep/2012:16:56:10 +0800] "GET / HTTP/1.1" 200 141 "-" "ELinks/0.11.1 (textmode; Linux; -)" "-"
4、squid反向代理:
=======================================================================
实验环境 内网web服务器 squid VM2
eth0 vmnet1 eth0 eth1
172.24.100.1 172.24.100.254 192.168.0.1 192.168.0.x
- –注意:
172.24.100.254为squid的一个内网IP
192.168.0.1为squid的外网IP(也就是说在这个架构里squid在最前端)
192.168.0.x 为外网客户端
第一步:
在内网web服务器上安装httpd,并做一个主页用于测试
[root@VM_16_17_centos ~]#yum install httpd* -y
[root@VM_16_17_centos ~]#echo "内网web" > /var/www/html/index.html
[root@VM_16_17_centos ~]#/etc/init.d/httpd restart
第二步:
在squid服务器上安装,并进行配置
[root@VM_16_17_centos ~]#yum install squid -y
用一个新的配置文件;如果是在上面的透明代理基础上再做反向代理,先注释掉前面所有的配置
[root@VM_16_17_centos ~]#vim /etc/squid/squid.conf
62 http_port 80 accel vhost vport
--accel 反向代理加速模式
--vhost 支持域名
--vport 支持IP
cache_peer 172.24.100.1 parent 80 0 no-query originserver name=web
--172.24.100.1 内网web服务器的IP
--parent 上下关系,非平级关系
--80 代理内部web服务器的80端口
--0 没有使用icp,表示就一台squid服务器
--no-query 不去查询邻居,与上面的0结合使用
--originserver 表示源实际服务器
--name=web 定义一个名字,让后面的参数引用
cache_peer_domain web web.cluster.com --对web.cluster.com的请求会给web这台服务器(也就是上面定义的172.24.100.1);如果有多台web的话,可以多台web绑定同一个域名,还可以实现RR轮循调度
cache_peer_domain web 192.168.0.1 --光写上面一条,客户端只能通过web.cluster.com访问,再加上这条,客户端就可以使用来192.168.0.1访问
59 http_access allow all
68 cache_dir ufs /var/spool/squid 256 16 256
保存后,先不要重启squid;因为我这是在透明代理的基础上再做的,所以要清除iptables规则
[root@VM_16_17_centos ~]#iptables -t nat -F
重启服务,注意不要与apache的80端口冲突
–测试:
所以这里客户端的DNS解析web.cluster.com应该得到192.168.0.1的结果
所以这里不做DNS的话,就直接在/etc/hosts里写上
192.168.0.1 web.cluster.com
再使用http://web.cluster.com 就可以访问到内部的web服务器了
可以使用curl -I http://web.cluster.com去查有没有命中缓存
=======================================================================