linux笔记35-搭建squid

squid
1、什么是squid
squid可以做代理也可以做缓存
squid缓存不仅可以节省宝贵的宽带资源,也可以大大降低服务器的I/O
squid 不仅可以做正向代理,又可以做反向代理
正向代理:squid后面是客户端,客户端上网要通过squid代理来上网;
反向代理:squid后面是服务器,服务器返回给用户数据需要走squid;
正向代理在企业办公环境中,员工上网需要通过squid代理来上网,可以节省网络宽带资源,而反向代理用来搭建网站静态项(图片、html、流媒体、js、css等)的缓存服务器,他用于网站架构中。
2、搭建squid正向代理
(1)yum install -y squid
squid -v 查看版本信息和编译配置
>/etc/squid/squid.conf 清空配置,拷贝如下配置:
http_port 3128
acl manager proto cache_object
visible_hostname HostA   #squid服务器名称
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 internet network
acl localnet src 172.16.0.0/12  #RFC1918 possible internet network
acl localnet src 192.168.0.0/16  #RFC1918 possible internet 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  #本地缓存目录,1024MB,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|JPEG)  1440  50%  2880  ignore-reload
refresh_pattern .   0  20%  4320
mkdir /data/cache      
chown -R squid:squid /data/cache
squid -z   //初始化缓存目录,新版本可以省略
/etc/init.d/squid start  
squid -kcheck  //检查配置文件是否有错
squid -krec    //重新加载配置
service squid restart    //重启squid服务
在另一台机器上测试:
curl -x192.168.1.119:3128 www.baidu.com

可以看到已经可以正常访问网页:


(2)需求案例:
限制某些域名不能通过代理访问,或者只代理几个域名
vim /etc/squid/squid.conf,设置白名单
acl http proto HTTP
acl good_domain dstdomain .baidu.com 
http_access allow http good_domain
http_access deny http !good_domain

测试curl -x192.168.1.119:3128 www.qq.com -I
测试curl -x192.168.1.119:3128 www.baidu.com -I



设置黑名单:
acl http proto HTTP
acl bad_domain dstdomain .taobao.com .jd.com
http_access deny http bad_domain

测试curl -xlocalhost:3128 www.taobao.com -I
测试curl -xlocalhost:3128 www.baidu.com -I



windows下的测试:
打开浏览器属性-连接-局域网(LAN)设置-代理设置:


此时访问淘宝会提示错误:


3、搭建squid反向代理
vim /etc/squid/squid.conf #做如下更改
http_port 3128更改为http_port 80 accel vhost vport
增加如下内容:
cache_peer 180.163.26.39 parent 80 0 originserver name=a
cache_peer 115.239.211.112 parent 80 0 originserver name=b
cache_peer_domain a www.qq.com
cache_peer_domain b www.baidu.com


之前增加的域名白/黑名单相关配置去掉
如果squid要代理一台web上的所有域名,那么写成:
cache_peer 192.168.1.119 parent 80 0 originserver #只需这一行 ,cache_peer_domain都可以


未完待续
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值