squid 三种代理实验

squid 软件既可以做代理,也可以做实现缓存加速,大大降低服务器的I/O.。

1.其中squid代理分为三种,正向代理、透明代理、反向代理。

(1)squid正向代理和squid透明代理都位客户端:内 IP不直接访问公 IP上 ,而需借助squid正向或者透明代理实现上 ,这样可以缓解ip资源,常见的有正向代理用在企业的办公环境中,员工上 需要通过squid代理来上 ,这样可以节省 络带宽资源

节省上 的带宽)

 

2.squid正向代理、squid透明代理、squid反向代理:

squid 正向代理:对客户端有要求,用户要上 ,需要在浏览器里面设置代理IP:PORT

squid 透明代理:浏览器里面不需要设置代理IP:PORT;客户端的 关设置为代理IP ,通过iptables实现redirect

squid 反向向代理: 客户端不做要求(公 )

3 .squid 三种代理的搭建

yum install -y squid
squid -v  查看版本以及编译参数
> /etc/squid/squid.conf # 清空原有的配置文件

(1)正向代理

(1.1)vim /etc/squid/squid.conf  配置文件如下:

http_port 3128
acl manager proto cache_object
acl localhost src 127.0.0.1/32 ::1
acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1
acl localnet src 10.0.0.0/8     # RFC1918 possible internal network
acl localnet src 172.16.0.0/12  # RFC1918 possible internal network
acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
acl SSL_ports port 443
acl Safe_ports port 80 8080         # http
acl Safe_ports port 21          # ftp
acl Safe_ports port 443         # https
acl CONNECT method CONNECT
http_access allow manager localhost
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow localnet
http_access allow localhost
http_access allow all
cache_dir aufs /data/cache 1024 16 256
cache_mem 128 MB
hierarchy_stoplist cgi-bin
coredump_dir /var/spool/squid
refresh_pattern ^ftp:           1440    20%     10080
refresh_pattern ^gopher:        1440    0%      1440
refresh_pattern -i (/cgi-bin/| 0     0%      0
refresh_pattern .(jpg|png|gif|mp3|xml) 1440    50%     2880    ignore-reload
refresh_pattern .               0       20%     4320

# 缓存设置
cache_mem 256 MB
maximum_object_size_in_memory 2 MB
#缓存内容大小控制,当cache目录被占用到97%时,内容将被清空20%
cache_swap_low 80
cache_swap_high 97

 

(1.2)

mkdir  /data/cache  #创建缓存目录
chown -R squid:squid /data/cache  #更改权限
squid -z  #初始化缓存目录,该步骤可以省略
/etc/init.d/squid start
squid  -kcheck #可以检测配置文件是否有错
squid -k rec #可以重新加载配置
service squid restart #重启squid服务

                测试:curl -xlocalhost:3128 www.qq.com
访问图片,测试缓存: curl -xlocalhost:3128   -I ‘http://www.aminglinux.com/bbs/static/image/common/logo.png’

也可以在windows浏览器里设置代理IP:3128  然后访问 站

(2 )透明代理:实现条件

(2.1)因为之前学习过LAMP环境,所以这里打算用这个环境.用到三台机器(全部是虚拟机VMware)

pc-local: 桥接,eth0,192.168.1.251      

pc-Gateway: eth0 ,NAT ,192.168.176.253    这台机器上安装squid    启动3128 进行透明代理

                  eth1,桥接,192.168.1.250

pc-web : eth0,NAT,192.168.176.252    这个机器上安装的apche(88)服务,nginx(80)服务,其中nginx只代理动态php,其余走apache

 

设置:其中pc-local的 关设定为192.168.1.250(就是pc-Gatway的eth1地址)

(2.2)vim /etc/squid/squid.conf 

将上面的配置文件

http_port 3128  修改 http_port 192.168.1.250:3128 transparent

重启启动squid  或者squid –k  reconfigure

(2.3) 然后iptables 将在pc-local上发送的web80请求重新指到3128端口

iptables -t nat -A PREROUTING -i eth1 -s 192.168.1.250  -p tcp –dport 80 -j REDIRECT –to-ports 3128

这样实现透明代理,不需要还在客户端设置。

(2.4) 在pc-local 192.168.1.251机器上测试 curl  http://192.168.176.252/forum.php –I

(3) 反向代理:

(1)机器条件可以不变

但是有一个地方需要改变:就是local的 站不要设置成eth1地址,也可以不设置 只要和eth1同 段即可。

(2)配置文件

http_port 3128 修改为 http_port 3128 accel vhost vport

cache_peer 192.168.1.252 parent 88 0 originserver name=a    #这里因为192.168.176.252的apache开启的88,nginx(80)也做了一层代理

其中192.168.1.252 是要被代理的原始的提供web服务的地址

cache_peer_domain a bbs.chinaops.com

(3) 在pc-local 192.168.1.251机器上测试 curl  http://192.168.176.252/forum.php –I

 

实验参考博客:http://fengzhilinux.blog.51cto.com/1343279/284375

其中squid代理实现:http://jaseywang.me/2011/01/21/squid-%E5%8F%8D%E5%90%91%E4%BB

 

 

 

 

 

 

 

 

相关资源:软件工程课件软件工程是指研究软件生产的一门学科,也就是将完善的…

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2015年11月1日
下一篇 2015年11月1日

相关推荐