设为首页 收藏本站
查看: 1017|回复: 0

[经验分享] squid缓存服务器

[复制链接]

尚未签到

发表于 2018-12-26 11:03:22 | 显示全部楼层 |阅读模式
squid缓存服务器

缓存概念
  作为应用层的代理服务软件,squid主要提供缓存加速和应用层过滤控制功能



  • 代理服务器
      客户端向网站发送请求数据

  (为了能承受更多的并发连接客户端访问先请求代理服务器听过代理服务器提供出的数据给客户端,如果代理服务器上没有客户端的需求则代理服务器江湖发送请求给web服务器请求数据然后缓存到自己的缓存里面)

代理服务器分为以下几种


  • 传统代理(客户端发送请求数据,访问的是代理服务器有代理服务器提供数据)
  • 透明代理(代理服务器作为客户端的网关,在客户机访问web服务器时,必须通过代理服务器也就是说访问的网址时是web服务器的网址但是必须同代理服务器,代理服务器给你去请求数据)
  win7设置代理



首先学会安装代理服务器:squid

这边我们用的是squid-4.1

[root@localhost home]# tar zxvf /home/squid-4.1.tar.gz -C /opt/
[root@localhost home]# cd /opt/
[root@localhost opt]# ls
rh  squid-4.1
[root@localhost home] cd /opt/squid-4.1
[root@localhost squid-4.1] ./configure
> --prefix=/usr/local/squid \                   #指定安装路径
> --sysconfdir=/etc \                           #配置文件存放位置                 
> --enable-arp-acl \                            #通过mac地址进行管理防止arp欺骗
> --enable-linux-netfilter \                    #内核过滤           
> --enable-linux-tproxy \                       #指定支持透明模式
> --enable-async-io=100 \                       #制定性能
> --enable-err-language="Simplify_Chinese" \    #出现报错用中文方式显示
> --enable-underscore \                         #允许有下划线
> --enable-poll \                               #提高性能
> --enable-gnuregex                             #支持正则表达式
[root@localhost squid-4.1]# make && make install
建立软连接把squid自带命令让系统能够识别

[root@localhost squid-4.1] ln -s /usr/local/squid/sbin/* /usr/local/sbin/

创建用户不允许本地登录

[root@localhost squid-4.1] useradd -M -s /sbin/nologin squid
修改属主数组

[root@localhost squid-4.1] chown -R squid.squid /usr/local/squid/var/
在/etc/找到squid.conf文件修改主配置文件

[root@localhost squid-4.1] vim /etc/squid.conf
# And finally deny all other access to this proxy
http_access allow all
http_access deny all
# Squid normally listens to port 3128
http_port 3128
cache_effective_user squid        #添加   指定程序用户
cache_effective_group squid       #添加   指定账号基本组

清楚缓存(初始化)

[root@localhost ~] squid -z
[root@localhost ~] 2018/07/23 16:22:26| Created PID file (/usr/local/squid/var/run/squid.pid)
2018/07/23 16:22:27 kid1| Set Current Directory to /usr/local/squid/var/cache/squid
2018/07/23 16:22:27 kid1| Creating missing swap directories
2018/07/23 16:22:27 kid1| No cache_dir stores are configured.
2018/07/23 16:22:27| Removing PID file (/usr/local/squid/var/run/squid.pid)

下面可以启动服务了

[root@localhost squid-4.1]# squid
[root@localhost squid-4.1]# netstat -ntap #查看3128端口有没有开启
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name   
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      3031/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1001/sshd           
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1000/cupsd         
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2201/master         
tcp        0      0 192.168.32.207:22       192.168.32.1:49265      ESTABLISHED 14414/sshd: root@pt
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      1001/sshd           
tcp6       0      0 ::1:631                 :::*                    LISTEN      1000/cupsd         
tcp6       0      0 :::3128                 :::*                    LISTEN      30544/(squid-1)     
tcp6       0      0 ::1:25                  :::*                    LISTEN      2201/master     
上面服务器手工编译安装好了下面开始稍微进行一下优化

制作service启动脚本

[root@localhost ~] cd /etc/init.d/
[root@localhost init.d] vim squid
#!/bin/bash
#chkconfig: 2345 90 25
PID="/usr/local/squid/var/run/squid.pid"
CONF="/etc/squid.conf"
CMD="/usr/local/squid/sbin/squid"
case "$1" in
start)
netstat -natp | grep squid &> /dev/null
if [ $? -eq 0 ]
then
echo "squid is running"
else
echo "正在启动 squid..."
$CMD
fi
;;
stop)
$CMD -k kill &> /dev/null
rm -rf $PID &> /dev/null
;;
status)
[ -f $PID ] &> /dev/null
if [ $? -eq 0 ]
then
netstat -natp | grep squid
else
echo "squid is not running"
fi
;;
restart)
$0 stop &> /dev/null
echo "正在关闭 squid..."
$0 start &> /dev/null
echo "正在启动 squid..."
;;
reload)
$CMD -k reconfigure
;;
check)
$CMD -k parse
;;
*)
echo "用法:$0{start|stop|status|reload|check|restart}"
;;
esac
[root@localhost init.d] chkconfig --add squid
[root@localhost init.d] chkconfig --level 35 squid on

用service开启服务

[root@localhost init.d]# service squid start
正在启动 squid...
[root@localhost init.d]# netstat -ntap
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name   
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      3031/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1001/sshd           
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1000/cupsd         
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2201/master         
tcp        0     52 192.168.32.207:22       192.168.32.1:49265      ESTABLISHED 14414/sshd: root@pt
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      1001/sshd           
tcp6       0      0 ::1:631                 :::*                    LISTEN      1000/cupsd         
tcp6       0      0 :::3128                 :::*                    LISTEN      5412/(squid-1)      
tcp6       0      0 ::1:25                  :::*                    LISTEN      2201/master         
[root@localhost init.d]# service squid stop

传统代理

[root@localhost init.d] vim /etc/squid.conf
#在配置文件中添加
# Squid normally listens to port 3128
http_port 3128
cache_mem 64 MB                   #指定缓存功能所使用的内存空间大小,便于保持访问较频繁的WEB对象,容量最>好为4的倍数,单位为MB,建议设为物理内存的1/4
reply_body_max_size 10 MB         #允许用户下载的最大文件大小,以字节为单位。默认设置0表示不进行限制
maximum_object_size 4096 KB       #允许保存到缓存空间的最大对象大小,以KB为单位,超过大小限制的文件将不被
缓存,而是直接转发给用户
cache_effective_user squid        #添加   指定程序用户
cache_effective_group squid       #添加   指定账号基本组
设置防火墙规则

[root@localhost init.d] iptables -F #清空防火墙
[root@localhost init.d] setenforce 0 #关闭安全性模块
[root@localhost init.d] iptables -I INPUT -p tcp --dport 3218 -j ACCEPT #允许访问3128端口访问tcp协议
启动squid服务

[root@localhost init.d]# service squid start
正在启动 squid...
[root@localhost init.d]# netstat -ntap
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name   
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      3031/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1001/sshd           
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1000/cupsd         
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2201/master         
tcp        0     52 192.168.32.207:22       192.168.32.1:49265      ESTABLISHED 14414/sshd: root@pt
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      1001/sshd           
tcp6       0      0 ::1:631                 :::*                    LISTEN      1000/cupsd         
tcp6       0      0 :::3128                 :::*                    LISTEN      5652/(squid-1)      
tcp6       0      0 ::1:25                  :::*                    LISTEN      2201/master        
检测web服务器能否正常访问到

win7IP地址为192.168.32.148访问192.168.32.152web服务器

请看下图:


用客户端访问http协议

在Windows系统中开启浏览器
Internet选项---》连接----》局域网设置----ip:squid服务器地址  端口:3128
地址栏输入web服务器地址
查看web服务器访问日志access.log  是代理服务器地址访问
#上面是在windows里面设置的代理如果不在windows里面设置在linux/etc/porfile里面也可以设置
在Linux系统中设置代理
vim /etc/profile
HTTP_PROXY=http://192.168.235.206:3128      //http代理服务器地址
HTTPS_PROXY=http://192.168.235.206:3128   //https的代理服务器地址
FTP_PROXY=http://192.168.235.206:3128     //ftp的代理服务器地址
NO_PROXY=192.168.10.,192.168.20.            //当这两个网段地址访问网页不通过代理服务器
export HTTP_PROXY HTTPS_PROXY FTP_PROXY NO_PROXY
source /etc/profile    运行配置文件
透明代理
  配置IP地址:
  squid的双网卡-

                    ens33:192.168.100.1
ens36:12.0.0.1
  web服务器:

                12.0.0.12
  client客户机

                192.168.100.50
  配置squid配置文件

#这是4.1以上版本的改动如果你用3.6的版本直接改动就行4.1以上版本直接改的话会冲突
http_port 3128 #在3128端口下面重新添加一个端口
http_port 192.168.100.1:3129 transparent
#查看端口
[root@localhost logs]# netstat -ntap | grep 3128
tcp6       0      0 :::3128                 :::*                    LISTEN      21144/(squid-1)     
[root@localhost logs]# netstat -ntap
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name   
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      3031/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1001/sshd           
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1000/cupsd         
tcp        0      0 192.168.100.1:3129      0.0.0.0:*               LISTEN      21144/(squid-1)     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2201/master         
tcp        0     52 192.168.100.1:22        192.168.100.4:52304     ESTABLISHED 20705/sshd: root@pt
tcp        0      0 192.168.32.207:22       192.168.32.1:50718      ESTABLISHED 6895/sshd: root@pts
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      1001/sshd           
tcp6       0      0 ::1:631                 :::*                    LISTEN      1000/cupsd         
tcp6       0      0 :::3128                 :::*                    LISTEN      21144/(squid-1)     
tcp6       0      0 ::1:25   
  配置防火墙

[root@localhost ~]# iptables -F
[root@localhost ~]# iptables -t nat -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# iptables -t nat -I PREROUTING -i ens33 -s 192.168.100.0/24 -p tcp --dport 80 -j REDIRECT --to 3129
[root@localhost ~]# iptables -t nat -I PREROUTING -i ens33 -s 192.168.100.0/24 -p tcp --dport 443 -j REDIRECT --to 3129
[root@localhost ~]# iptables -I INPUT -p tcp --dport 3218 -j ACCEPT
  在透明代理里缓存服务器作为网关访问web服务器
如下图:






运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-656017-1-1.html 上篇帖子: linux下squid服务器的配置 下篇帖子: 用squid再次疯狂加速你的web-chding
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表