squid 三种代理实验

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

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

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

节省上网的带宽)

(2)squid反向代理:外网IP(公网IP)要访问内网IP服务器资源的时候,不能直接访问。(因为三层网络当中,所有来源IP和目的IP必须的公网IP,否则路由器的话,私有IP携带的数据将被丢去)。这时通过squid做反向代理,来实现简介访问私有IP发布的网站。把私有web服务器资源给外网(称为服务器发布)。反向代理用来搭建网站静态项(图片、html、流媒体、js、css等)的缓存服务器,它用于网站架构中。

 

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

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/the-study-of-linux/p/5011744.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值