squid緩存服務器
阿新 • • 發佈:2018-08-14
127.0.0.1 intern 文件中 江湖 body use 啟動 傳統 init 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服務器
如下圖:
squid緩存服務器