做爲應用層的代理服務軟件,squid主要提供緩存加速和應用層過濾控制的功能。css
實驗環境表:html
1.編譯安裝Squidlinux
配置Squid的編譯選項時,將安裝目錄設置爲/usr/local/squid,其餘具體選項根據實際須要來肯定,配置前可參考./configure --help給出的說明。web
tar zxvf squid-3.4.6.tar.gz -C /opt/ cd /opt/squid-3.4.6 ./configure --prefix=/usr/local/squid \ #安裝目錄 --sysconfdir=/etc \ #單獨將配置文件修改到其餘目錄 --enable-arp-acl \ #直接經過客戶端MAC進行管理,防止客戶端使用IP欺騙 --enable-linux-netfilter \ #使用內核過濾 --enable-linux-tproxy \ #支持透明模式 --enable-async-io=100 \ #異步I/O,提高存儲性能。 --enable-err-language="Simplify_Chinese" \ #錯誤信息的顯示語言 --enable-underscore \ #容許URL中有下劃線 --enable-poll \ #使用poll()模式,提高性能。 --enable-gnuregex #使用GNU正則表達式 make && make install
安裝完成後,建立連接文件、建立用戶和組。正則表達式
ln -s /usr/local/squid/sbin/* /usr/local/sbin/ useradd -M -s /sbin/nologin squid chown -R squid.squid /usr/local/squid/var/
2.squid的配置文件vim
squid服務的配置文件位於/etc/squid.conf,充分了解配置行的做用將有助於管理員根據實際狀況靈活地配置代理服務。瀏覽器
vim /etc/squid.conf http_port 3128 #用來指定代理服務監聽的地址和端口(默認端口號爲3128) cache_effective_user squid #添加 指定程序用戶 cache_effective_group squid #添加 指定帳號基本組
3.squid運行控制緩存
(1)檢查配置文件的語法是否正確bash
squid -k parse
(2)啓動、中止squid服務器
第一次啓動squid服務時,會自動初始化緩存目錄。在沒有可用的squid服務器腳本的狀況下,也能夠直接調用squid程序來啓動服務,這時須要先進行初始化。
squid -z //初始化緩存目錄 squid //啓動服務
(3)使用squid服務腳本
爲了使squid服務的啓動、中止、重載等操做更加方便,能夠編寫squid服務腳本,並使用chkconfig和service工具來進行管理。
cd /etc/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 chmod +x squid chkconfig --add squid #添加爲系統服務 chkconfig --level 35 squid on
1.squid服務器的配置
配置squid實現傳統代理服務時,須要注意添加http_access allowall訪問策略,以便容許任意客戶機使用代理服務。
1)修改squid.conf配置文件
[root@localhost init.d]# vim /etc/squid.conf http_access allow all http_access deny all 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爲單位,超過大小限制的文件將不被緩存,而是直接轉發給用戶
2)在防火牆上添加容許策略:
[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 [root@localhost init.d]# service squid reload #從新加載
2.客戶機的代理配置
在Windows系統中開啓瀏覽器
Internet選項---》鏈接----》局域網設置----ip:squid服務器地址 端口:3128
地址欄輸入web服務器地址。
3.代理服務器的驗證
在客戶機中經過瀏覽器訪問目標Web服務器網站http://192.168.126.178/,而後觀察squid代理服務器,Web服務器的訪問日誌,以驗證代理服務是否發揮做用。
1)查看squid訪問日誌的新增記錄
在squid代理服務器中,經過跟蹤squid服務的訪問日誌文件,應該可以發現客戶機192.168.126.177訪問網站服務器192.168.126.178的記錄。
[root@localhost init.d]# tail /usr/local/squid/var/logs/access.log 1532758490.972 5 192.168.126.177 TCP_MISS/404 512 GET http://192.168.126.178/noindex/css/fonts/Light/OpenSans-Light.eot? - HIER_DIRECT/192.168.126.178 text/html 1532758490.974 5 192.168.126.177 TCP_MISS/404 524 GET http://192.168.126.178/noindex/css/fonts/LightItalic/OpenSans-LightItalic.eot? - HIER_DIRECT/192.168.126.178 text/html
2)查看Web訪問日誌的新增記錄
在被訪問的Web服務器中,經過跟蹤httpd服務器的訪問日誌文件,應該可以發現來在代理服務器192.168.126.138的訪問記錄。說明當前客戶機使用代理後,Web服務器並不知道客戶機的真實地址,其實是由代理服務器在替它訪問。
[root@localhost ~]# cd /etc/httpd/logs/ [root@localhost logs]# ls access_log error_log [root@localhost logs]# cat access_log 192.168.126.138 - - [28/Jul/2018:14:14:50 +0800] "GET / HTTP/1.1" 403 4897 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)" //
1.配置Squid支持透明代理
1)在squid代理服務器上添加雙網卡,分別是內網地址192.168.100.1和外網地址12.0.0.1。Web服務器地址改成12.0.0.12外網地址,客戶端地址改成192.168.100.100內網地址。
[root@localhost network-scripts]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 #內網地址 inet 192.168.100.1 netmask 255.255.255.0 broadcast 192.168.100.255 ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 #外網地址 inet 12.0.0.1 netmask 255.255.255.0 broadcast 12.0.0.255
2)修改squid的配置文件,在http_port配置上加上transparent(透明)選項,就能夠支持透明代理了。
[root@localhost network-scripts]# vim /etc/squid.conf http_port 192.168.100.1:3128 transparent #只在其中一個IP地址上提供服務 [root@localhost network-scripts]# service squid reload
2.設置iptables的重定向策略
防火牆作重定向操做,將訪問本機80、443端口的請求重定向到3128端口
[root@localhost network-scripts]# iptables -F [root@localhost network-scripts]# iptables -F -t nat #清除防火牆規則 [root@localhost network-scripts]# iptables -t nat -I PREROUTING -i ens33 -s 192.168.100.0/24 -p tcp --dport 80 -j REDIRECT --to 3128 [root@localhost network-scripts]# iptables -t nat -I PREROUTING -i ens33 -s 192.168.100.0/24 -p tcp --dport 443 -j REDIRECT --to 3128 [root@localhost network-scripts]# iptables -I INPUT -p tcp --dport 3218 -j ACCEPT
3.驗證透明代理
在客戶機192.168.100.100中經過瀏覽器訪問目標網站http://12.0.0.12/,而後觀察squid代理服務器、Web服務器的訪問日誌,以驗證透明代理是否發揮做用。
squid代理服務器的訪問日誌: [root@localhost network-scripts]# tail /usr/local/squid/var/logs/access.log 1532761302.140 3 192.168.100.100 TCP_MISS/404 512 GET http://12.0.0.12/noindex/css/fonts/Light/OpenSans-Light.eot? - ORIGINAL_DST/12.0.0.12 text/html 1532761302.143 2 192.168.100.100 TCP_MISS/404 524 GET http://12.0.0.12/noindex/css/fonts/LightItalic/OpenSans-LightItalic.eot? - ORIGINAL_DST/12.0.0.12 text/html 1532761302.146 2 192.168.100.100 TCP_MISS/404 516 GET http://12.0.0.12/noindex/css/fonts/Regular/OpenSans-Regular.eot? - ORIGINAL_DST/12.0.0.12 text/html Web服務器的訪問日誌: [root@localhost ~]# cd /var/log/httpd/ [root@localhost httpd]# vim access_log 12.0.0.1 - - [28/Jul/2018:15:06:43 +0800] "GET /noindex/css/fonts/ExtraBold/OpenSans-ExtraBold.eot? HTTP/1.1" 404 248 "http://12.0.0.12/" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)" 12.0.0.1 - - [28/Jul/2018:15:06:43 +0800] "GET /noindex/css/fonts/ExtraBoldItalic/OpenSans-ExtraBoldItalic.eot? HTTP/1.1" 404 260 "http://12.0.0.12/" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)"
代理成功