squid缓存服务器

squid缓存服务器

缓存概念

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

  • 代理服务器

    客户端向网站发送请求数据

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

代理服务器分为以下几种

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

win7设置代理
squid缓存服务器

squid缓存服务器

首先学会安装代理服务器: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服务器

请看下图:

image

用客户端访问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服务器
如下图:
squid缓存服务器
squid缓存服务器

猜你喜欢

转载自blog.51cto.com/13645280/2159491
今日推荐