1. 程式人生 > >squid代理(一)--傳統代理

squid代理(一)--傳統代理

gnu 則表達式 dev 文件中 重啟 https協議 max 記錄 透明

squid代理(一)--傳統代理

Web代理的工作機制
緩存網頁對象,減少重復請求

代理的基本類型
傳統代理:適用於Internet,客戶端上網需明確指定服務端IP以及端代理口
透明代理:適用於共享上網網關,客戶端上網不需指定服務端IP以及端代理口

使用代理的好處
提高Web訪問速度
隱藏客戶機的真實IP地址

編譯安裝squid
centos7服務器一枚
準備好squid-3.5.23.tar.gz源碼包
[root@localhost ~]# tar -zxf squid-3.5.23.tar.gz
[root@localhost ~]# cd squid-3.5.23/
[root@localhost squid-3.5.23]# ./configure --prefix=/usr/local/squid --sysconfdir=/etc --enable-linux-netfilter --enable-async-io=240 --enable-default-err-language=Simplify_Chinese --disable-poll --enable-epoll --enable-gnuregex

--prefix= ##安裝位置
--sysconfdir= ##單獨將配置文件修改到其他目錄
--enable-linux-netfilter ##使用內核過濾
--enable-async-io=值 ##異步I/O,提升存儲性能
--enable-default-err-language=Simplify_Chinese ##錯誤信息的顯示語言
--disable-poll與--enable-epoll ##關閉默認使用poll模式,開啟epoll模式提升性能
--enable-gnuregex ##使用GNU正則表達式

[root@localhost squid-3.5.23]# make && make install ##編譯安裝

安裝完後創建連接文件,創建用戶和組
[root@localhost squid-3.5.23]# ln -s /usr/local/squid/sbin/* /usr/local/sbin/ ##軟連接ln -s [源文件或目錄] [目標文件或目錄]

[root@localhost squid-3.5.23]# useradd -M -s /sbin/nologin squid ##創建squid用戶,並且不允許登陸系統
[root@localhost squid-3.5.23]# chown -R squid:squid /usr/local/squid/var/ ##指定文件的擁有者改為指定的用戶或組

squid的配置文件
[root@localhost squid-3.5.23]# vi /etc/squid.conf ##在coredump_dir /usr/local/squid/var/cache/squid上邊添加一下兩條配置
http_port 3128 ##默認自帶(用來指定代理的監聽端口)
cache_effective_user squid ##指定squid程序用戶,用來設置初始化,運行時緩存的賬號,否則啟動不成功
cache_effective_group squid
:wq
[root@localhost squid-3.5.23]# squid -k parse ##檢查配置文件是否正確

啟動、停止squid
[root@localhost ~]# squid -z ##-z選項用來初始化緩存目錄
[root@localhost ~]# squid ##啟動squid服務
[root@localhost ~]# netstat -anpt |grep squid ##監聽3128端口是否開啟
tcp6 0 0 :::3128 :::* LISTEN 82716/(squid-1)

編輯squid服務腳本
[root@localhost ~]# vi /etc/init.d/squid
#!/bin/bash
chkconfig: 2345 90 25
Squid Server Scrip

PID="/usr/local/squid/var/run/squid.pid "
CONF="/etc/squid.conf"
CMD="/usr/local/squid/sbin/squid"

case "$1" in
start)
netstat -anpt | grep squid &>/dev/null
if [ $? -eq 0 ]
then
echo "Squid正在運行"
else
$CMD
fi
;;
stop)
kill -9 cat $PID &> /dev/null
rm -rf $PID &>/dev/null
;;
status)
[ -f $PID ] &> /dev/null
if [ $? -eq 0 ]
then
echo "squid正在運行...."
else
echo "squid未啟動"
fi
;;
restart)
$0 stop
$0 start
;;
reload)
$CMD -k reconfigure
;;
check)
$CMD -k parse
;;
)
echo "請輸入正確參數{start|stop|reload|restart|check|status}"
;;
esac
:wq
[root@localhost ~]# chmod +x /etc/init.d/squid ##添加執行權限
[root@localhost ~]# chkconfig --add squid ##添加為系統服務
[root@localhost ~]# systemctl restart squid ##重啟測試腳本
[root@localhost ~]# netstat -anpt |grep squid
tcp6 0 0 :::3128 :::
LISTEN 82942/(squid-1)

傳統代理
squid服務器的配置:添加如下配置
[root@localhost ~]# vim /etc/squid.conf
http_access allow all ##表示允許任意客戶機使用代理,放在http_access deny all之前
reply_body_max_size 10 MB ##表示允許下載的最大文件大小(10MB)
:wq

重啟squid服務
[root@localhost ~]# systemctl restart squid

[root@localhost ~]# squid -k reconfigure

驗證代理
在Windows中設置代理
打開IE瀏覽器-->工具-->Intelnet選項-->連接-->局域網設置-->"勾上"代理服務器的勾-->地址中寫代理服務器的IP,
端口中寫"3128"-->確定,訪問百度即可

在Linux中設置代理,需要通過環境變量來制定代理服務器的地址、端口
[root@localhost ~]# vi /etc/profile ##配置文件中添加一下內容
HTTP_PROXY=http://192.168.10.30:3128 ##http協議
HTTPS_PROXY=http://192.168.10.30:3128 ##https協議
FTP_PROXY=http://192.168.10.30:3128 ##ftp協議
NO_PROXY=192.168.1.,192.168.2. ##對這兩個局域網網段不適用代理
export HTTP_PROXY HTTPS_PROXY FTP_PROXY NO_PROXY

[root@localhost ~]# source /etc/profile ##刷新配置文件

代理服務器驗證方法
1、chakansquid訪問日誌的新增記錄
[root@localhost ~]# tail -f /usr/local/squid/var/logs/access.log
1536938536.649 3 192.168.10.100 TCP_MISS/404 525 GET http://192.168.10.40/noindex/css/fonts/L
ightItalic/OpenSans-LightItalic.eot? - HIER_DIRECT/192.168.10.40 text/html

2、查看被訪問端的web訪問日誌的新增記錄
[root@localhost ~]# tail -f /var/log/httpd/access_log
192.168.10.30 - - [14/Sep/2018:23:22:17 +0800] "GET /noindex/css/fonts/Light/OpenSans-Light.eot? HT
TP/1.1" 404 240 "http://192.168.10.40/" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; T
rident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0;
InfoPath.2)"

squid代理(一)--傳統代理