天天看點

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伺服器

請看下圖:

用用戶端通路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           
#這是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           

繼續閱讀