博文大綱
1、Squid服務概述
2、安裝及運行控制html
做爲應用層的代理服務軟件,Squid主要提供緩存加速、應用層過濾控制的功能。linux
當客戶機經過代理來請求Web頁面時,指定的代理服務器會先檢查本身的緩存,若是緩存中已經有客戶機須要的頁面,則直接將緩存中的頁面內容反饋給客戶機;若是緩存中沒有客戶機要訪問的頁面,則由代理服務器向Internet發送訪問請求,得到返回的Web頁面之後,將網頁數據保存到緩存中併發送給客戶機,以下圖所示:
因爲客戶機的web訪問請求其實是squid代理服務器來代替完成的,從而能夠隱藏了用戶的真實IP地址,起到必定的保護做用。另外一方面,squid也能夠針對要訪問的目標、客戶機的地址、訪問的時間段進行過濾控制。web
傳統代理:也就是普通的代理服務,須要咱們客戶端在瀏覽器、聊天工具等一些程序中設置代理服務器的地址和端口,而後才能使用代理來訪問網絡,這種方式相比較而言比較麻煩,由於客戶機還需手動指定代理服務器,因此通常用於Internet環境。正則表達式
- 透明代理:與傳統代理實現的功能是同樣的,區別在於客戶機不須要手動指定代理服務器的地址和端口,而是經過默認路由、防火牆策略將web訪問重定向,實際上仍然交給代理服務器來處理,重定向的過程徹底是由squid服務器進行的,因此對於客戶機來講,甚至不知道本身使用了squid代理服務,所以呢,咱們稱之爲透明模式。
透明代理多用於局域網環境,如在Linux網關中啓用透明代理後,局域網主機無須進行額外設置就能享受更好的上網速度。vim
所需設備:centos
- Windows客戶端一臺;IP地址:192.168.100.102
- linux客戶端一臺;IP地址:192.168.100.10
- squid服務器一臺; IP地址:192.168.100.20
- web服務器一臺;IP地址:192.168.100.30
所需的鏡像請訪問:https://pan.baidu.com/s/1-3jN_z-JqWg2X1Bpz0SHUw
提取碼:ysxn瀏覽器
[root@centos03 ~]# yum -y install httpd <!--安裝httpd服務--> [root@centos03 ~]# cd /var/www/html/ <!--修改網站主頁的內容--> [root@centos03 html]# ls -a . .. [root@centos03 html]# echo "www.accp.com" > index.html <!--插入數據--> [root@centos03 ~]# systemctl start httpd <!--啓動httpd服務--> [root@centos03 ~]# systemctl enable httpd <!--設置開機自動啓動-->
[root@centos01 ~]# yum -y install elinks <!--安裝elinks軟件,linux操做系統光盤自動此軟件包--> [root@centos01 ~]# elinks http://192.168.100.30 <!--訪問網站服務器是否能夠訪問到-->
[root@centos02 ~]# mount /dev/cdrom /mnt/ <!--掛載linux光盤--> [root@centos02 ~]# tar zxvf /mnt/squid-3.4.6.tar.gz -C /usr/src/ <!--解壓縮squid軟件包到/usr/src/目錄--> [root@centos02 squid-3.4.6]# ./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 <!--配置squid--> [root@centos02 squid-3.4.6]# make && make install <!--編譯安裝squid-->
上述配置項的具體做用以下:緩存
- --prefix=/usr/local/squid :指定安裝目錄;
- --sysconfdir=/etc :單獨將配置文件修改到其餘目錄;
- --enable-linux-netfilter:使用內核過濾;
- --enable-async-io=值:異步I/O,提高存儲性能;
- --enable-default-err-language=Simplify_Chinese :錯誤信息顯示語言;
- --disable-poll 與--enable-epoll:關閉默認使用poll模式,開啓epoll模式提提高性能;
- --enable-gunregex:使用GUN正則表達式;
更多的配置項能夠參考「./configure --help」;
安裝完成後,建立連接文件、建立用戶和組bash
[root@centos02 ~]# ln -s /usr/local/squid/sbin/* /usr/local/sbin/ [root@centos02 ~]# useradd -M -s /sbin/nologin squid [root@centos02 ~]# chown -R squid:squid /usr/local/squid/var/
四、修改squid主配置文件服務器
[root@centos02 ~]# vim /etc/squid.conf <!--編輯squid主配置文件--> 54 http_access allow all <!--容許全部人訪問squid--> 60 http_port 3128 <!--squid監聽的端口號--> 61 cache_effective_user squid <!--管理squid的帳戶--> 62 cache_effective_group squid <!--管理squid的組--> 63 reply_body_max_size 10 MB <!--容許用戶下載最大數據10M--> 68 coredump_dir /usr/local/squid/var/cache/squid <!--設置緩存位置--> [root@centos02 ~]# squid -k parse <!--檢查配置文件語法是否錯誤-->
[root@centos02 ~]# vim /etc/init.d/squid <!--爲了方便控制服務,編寫一個squid服務腳本,並添加爲系統服務, 腳本以下--> #!/bin/bash # chkconfig: 35 90 25 # config file:/etc/squid.conf # pidfile: /usr/local/squid/var/run/squid.pid # Description: Squid - Internet object cache. 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 is running" else echo "正在啓動squid..." $CMD fi ;; stop) $CMD -k kill &> /dev/null rm -rf $PID &> /dev/null echo "正在關閉squid..." ;; status) [ -f $PID ] &> /dev/null if [ $? -eq 0 ] then netstat -anpt | 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|restart|reload|check|status}" exit 1 ;; esac [root@centos02 ~]# chmod +x /etc/init.d/squid <!--腳本添加執行權限--> [root@centos02 ~]# chkconfig --add squid <!--添加系統服務--> [root@centos02 ~]# chkconfig --level 35 squid on <!--設置開機自動啓動--> [root@centos02 ~]# /etc/init.d/squid start <!--啓動squid服務--> 正在啓動squid...
如今基於傳統代理的squid代理服務器已經配置完成了
[root@centos01 ~]# vim /etc/profile HTTP_PROXY=http://192.168.100.20:3128 <!--爲使用HTTP協議指定代理--> HTTPS_PROXY=http://192.168.100.20:3128 <!--爲使用HTTPS協議指定代理--> FTP_PROXY=http://192.168.100.20:3128 <!--爲使用FTP協議指定代理--> NO_PROXY=192.168.10.,192.168.20. <!--對兩個局域網段不使用代理--> export HTTP_PROXY HTTPS_PROXY FTP_PROXY NO_PROXY [root@centos01 ~]# source /etc/profile <!--應用代理--> [root@centos01 ~]# elinks http://192.168.100.30 <!--客戶端訪問網站服務器--> [root@centos03 ~]# tail -f /var/log/httpd/access_log <!--網站服務器追蹤squid代理服務器的訪問日誌文件--> <!--請注意,全部跟蹤都是在訪問以前先作好準備的--> 192.168.100.20 - - [18/Nov/2019:04:38:40 +0800] "GET / HTTP/1.1" 200 13 "-" "ELinks/0.12pre6 (textmode; Linux; 80x18-2)"
[root@centos03 ~]# tail -f /var/log/httpd/access_log <!--請注意,全部跟蹤都是在訪問以前先作好準備的--> 192.168.100.20 - - [18/Nov/2019:04:38:40 +0800] "GET / HTTP/1.1" 200 13 "-" "ELinks/0.12pre6 (textmode; Linux; 80x18-2)" 192.168.100.20 - - [18/Nov/2019:05:18:03 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
透明代理將在下一篇博文詳細配置,盡情期待!!!
—————— 本文至此結束,感謝閱讀 ——————