代理服務器英文全稱是Proxy Server,其功能就是代理網絡用戶去取得網絡信息。
Squid是一個緩存Internet 數據的軟件,其接收用戶的下載申請,並自動處理所下載的數據。當一個用戶想要下載一個主頁時,可以向Squid 發出一個申請,要Squid 代替其進行下載,然后Squid 連接所申請網站並請求該主頁,接着把該主頁傳給用戶同時保留一個備份,當別的用戶申請同樣的頁面時,Squid 把保存的備份立即傳給用戶,使用戶覺得速度相當快。Squid 可以代理HTTP、FTP、GOPHER、SSL和WAIS等協議並且Squid 可以自動地進行處理,可以根據自己的需要設置Squid,使之過濾掉不想要的東西。
一、工作流程
當代理服務器中有客戶端需要的數據時:
1)客戶端向代理服務器發送數據請求;
2)代理服務器檢查自己的數據緩存;
3)代理服務器在緩存中找到了用戶想要的數據,取出數據;
4)代理服務器將從緩存中取得的數據返回給客戶端。
當代理服務器中沒有客戶端需要的數據時:
1)客戶端向代理服務器發送數據請求;
2)代理服務器檢查自己的數據緩存;
3)代理服務器在緩存中沒有找到用戶想要的數據;
4)代理服務器向Internet 上的遠端服務器發送數據請求;
5)遠端服務器響應,返回相應的數據;
6)代理服務器取得遠端服務器的數據,返回給客戶端,並保留一份到自己的數據緩存中。
Squid代理服務器工作在TCP/IP的應用層:
二、squid分類
按照代理類型的不同,可以將Squid代理分為正向代理和反向代理。
正向代理:根據實現方式的不同,又可以分為普通代理和透明代理。
1)普通代理:需要客戶機在瀏覽器中指定代理服務器的地址、端口;
2)透明代理:適用於企業的網關主機(共享接入Internet)中,客戶機不需要指定代理服務器地址、端口等信息,代理服務器需要設置防火牆策略將客戶機的Web訪問數據轉交給代理服務程序處理;
反向代理:是指以代理服務器來接受internet上的連接請求,然后將請求轉發給內部網絡上的服務器,並將從服務器上得到的結果返回給internet上請求連接的客戶端,此時代理服務器對外就表現為一個服務器。
三、squid代理緩存環境部署過程:
1)關閉selinux和iptables
[root@server~]# vim /etc/sysconfig/selinux
.......
SELINUX=disabled
[root@server~]# /etc/init.d/iptables stop
2)檢查squid軟件是否安裝
[root@server~]# rpm -qa|grep squid
3)如果未安裝,則使用yum 方式安裝
[root@server~]# yum -y install squid
4) 設置開機自啟動,在3、5級別上自動運行squid服務
[root@server~]# chkconfig --level 35 squid on
5)squid服務器的配置文件說明
squid 的主配置文件是 /etc/squid/squid.conf,所有squid的設定都是在這個文件里配置,這里squid配置如下:
[root@server~]# vim /etc/squid/squid.conf
http_port 3128 #設置監聽的IP與端口號
cache_mem 64 MB #額外使用內存量,可根據你的系統內存在設定,一般為實際內存的1/3.比如這里內存是200M,這里設置1/3就是64MB
maximum_object_size 4 MB #設置squid磁盤緩存最大文件,超過4M的文件不保存到硬盤
minimum_object_size 0 KB #設置squid磁盤緩存最小文件
maximum_object_size_in_memory 4096 KB #設置squid內存緩存最大文件,超過4M的文件不保存到內存
cache_dir ufs /var/spool/squid 100 16 256 #定義squid的cache存放路徑 、cache目錄容量(單位M)、一級緩存目錄數量、二級緩存目錄數量
logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh #log文件日志格式
access_log /var/log/squid/access.log combined #log文件存放路徑和日志格式
cache_log /var/log/squid/cache.log #設置緩存日志
logfile_rotate 60 #log輪循60天
cache_swap_high 95 #cache目錄使用量大於95%時,開始清理舊的cache
cache_swap_low 90 #cache目錄清理到90%時停止
acl localnet src 192.168.1.0/24 #定義本地網段
http_access allow localnet #允許本地網段使用
http_access deny all #拒絕所有
visible_hostname squid.david.dev #主機名
cache_mgr wangshibo@huanqiu.com #管理員郵箱
四、普通代理服務
即標准的、傳統的代理服務,需要客戶機在瀏覽器中指定代理服務器的地址、端口。
實驗拓撲圖如下:
1)配置Squid 代理服務器IP地址
將eth1的IP地址修改為200.168.10.1
[root@server~]# ifconfig eth1 200.168.10.1
2) 編輯squid 主配置文件/etc/squid/squid.conf
[root@server~]# vim /etc/squid/squid.conf
http_port 3128
cache_mem 64 MB
maximum_object_size 4 MB
cache_dir ufs /var/spool/squid 100 16 256
access_log /var/log/squid/access.log
acl localnet src 192.168.1.0/24
http_access allow localnet
http_access deny all
visible_hostname squid.david.dev
cache_mgr wangshibo@huanqiu.com
3) 初始化
[root@server~]# squid –z
4) 啟動Squid
[root@server~]# /etc/init.d/squid start
5) 配置Web 服務器
安裝Apache
[root@server~]# rpm -qa|grep httpd
[root@server~]# yum -y install httpd
啟動Apache並加入開機啟動
[root@server~]# /etc/init.d/httpd start
[root@server~]# chkconfig httpd on
創建index.html
[root@server~]# echo "<h1>Squid-Web1/200.168.10.2</h1>" > /var/www/html/index.html
修改Web服務器IP地址
將web服務器的IP地址修改為200.168.10.2
[root@server~]# ifconfig eth0 200.168.10.2
6) 配置客戶端IP地址
7) 配置瀏覽器代理
打開瀏覽器(以IE為例,其他類似),菜單欄 -> 工具 -> Internet 選項 -> 連接 -> 局域網設置 -> 代理服務器,按照以下格式設置。
8) 測試
五、透明代理服務
適用於企業的網關主機,客戶機不需要指定代理服務器地址、端口等信息,通過iptables將客戶機的Web訪問數據轉交給代理服務程序處理。
實驗拓撲圖如下:
1)修改squid 主配置文件/etc/squid/squid.conf
[root@server~]# vim /etc/squid/squid.conf
http_port 3128 transparent
cache_mem 64 MB
maximum_object_size 4 MB
cache_dir ufs /var/spool/squid 100 16 256
access_log /var/log/squid/access.log
acl localnet src 192.168.1.0/24
http_access allow localnet
http_access deny all
visible_hostname squid.david.dev
cache_mgr wangshibo@huanqiu.com
在http_port 3128 后添加transparent 關鍵字。
2) 重啟squid服務
[root@server~]# /etc/init.d/squid reload
3) 添加iptables規則,把內部的http請求重定向到3128端口
啟動iptables 服務
[root@server~]# /etc/init.d/iptables start
清除現有iptables filter 表規則
[root@server~]# iptables -F
保存iptables 設置
[root@server~]# /etc/init.d/iptables save
查看nat 表設置
[root@server~]# iptables -t nat -L -n
在nat表中新增一條規則
[root@server~]# iptables -t nat -I PREROUTING -i eth0 -s 192.168.1.0/24 -p tcp --dport 80 -j REDIRECT --to-port 3128
保存iptables
[root@server~]# /etc/init.d/iptables save
設置iptables 開機啟動
[root@server~]# chkconfig iptables on
4) 修改客戶端IP地址
將默認網關設置為squid 服務器的內網ip地址。
5) 在瀏覽器中,取消代理設置
6) 測試
透明代理測試成功。
六、反向代理服務
為Internet用戶訪問企業Web站點提供緩存加速。
實驗拓撲圖如下:
1) 關閉防火牆
# /etc/init.d/iptables stop
2) 修改Web Server 主頁
Web1:
[root@server~]#echo "<h1>Squid-Web1/192.168.1.18</h1>" > /var/www/html/index.html
Web2:
[root@server~]# echo "<h1>Squid-Web1/192.168.1.19</h1>" > /var/www/html/index.html
3) 配置squid
[root@server~]# vim /etc/squid/squid.conf
http_port 80 accel vhost
http_access allow all
cache_peer 192.168.1.18 parent 80 0 originserver round-robin weight=1
cache_peer 192.168.1.19 parent 80 0 originserver round-robin weight=1
visible_hostname squid.david.dev
cache_mgr mchina_tang@qq.com
4) 啟動Squid服務(在此啟動會報錯,是因為上面設置了80端口,和http端口沖突。關閉http即可成功啟動這里的squid)
[root@server~]# /etc/init.d/squid reload
5) 測試
squid 采用了round-robin,所以客戶端的訪問將輪詢兩台web服務器,采用 "Ctrl + F5" 來深度刷新測試。
Web1:
Web2:
6)查看squid 的訪問日志。
七、實際應用
下面實驗將模擬通過不同的域名訪問不同的機器,簡單實現企業應用中的負載均衡。客戶端在瀏覽器地址欄中輸入www.squid.dev,將訪問192.168.1.18這台機器,訪問bbs.squid.dev,將訪問192.168.1.19這台機器。
實驗拓撲圖如下:
1) 修改Web Server 主頁
Web1:
[root@server~]# echo "<h1>www.squid.dev/192.168.1.18</h1>" > /var/www/html/index.html
Web2:
[root@server~]# echo "<h1>bbs.squid.dev/192.168.1.19</h1>" > /var/www/html/index.html
2) 配置Squid
[root@server~]# vim /etc/squid/squid.conf
http_port 80 accel vhost
http_access allow all
cache_peer 192.168.1.18 parent 80 0 originserver name=www
cache_peer 192.168.1.19 parent 80 0 originserver name=bbs
cache_peer_domain www www.squid.dev
cache_peer_domain bbs bbs.squid.dev
visible_hostname squid.david.dev
cache_mgr wangshibo@huanqiu.com
3) 配置客戶端
這里可以使用DNS服務來解析,這里我們為了方便,就在hosts 文件里直接指定。
4) 測試網絡情況
5) 測試www.squid.dev
6) 測試bbs.squid.dev
7) 查看squid 訪問日志
8) 查看兩台服務器的apache 訪問日志
[root@server~]# tailf /var/log/httpd/access.log
測試成功。