Linux下squid代理緩存服務環境部署

 

代理服務器英文全稱是Proxy Server,其功能就是代理網絡用戶去取得網絡信息。html

Squid是一個緩存Internet 數據的軟件,其接收用戶的下載申請,並自動處理所下載的數據。當一個用戶想要下載一個主頁時,能夠向Squid 發出一個申請,要Squid 代替其進行下載,而後Squid 鏈接所申請網站並請求該主頁,接着把該主頁傳給用戶同時保留一個備份,當別的用戶申請一樣的頁面時,Squid 把保存的備份當即傳給用戶,使用戶以爲速度至關快。Squid 能夠代理HTTP、FTP、GOPHER、SSL和WAIS等協議而且Squid 能夠自動地進行處理,能夠根據本身的須要設置Squid,使之過濾掉不想要的東西。linux

1、工做流程web

當代理服務器中有客戶端須要的數據時:
1)客戶端向代理服務器發送數據請求;
2)代理服務器檢查本身的數據緩存;
3)代理服務器在緩存中找到了用戶想要的數據,取出數據;
4)代理服務器將從緩存中取得的數據返回給客戶端。apache

當代理服務器中沒有客戶端須要的數據時:
1)客戶端向代理服務器發送數據請求;
2)代理服務器檢查本身的數據緩存;
3)代理服務器在緩存中沒有找到用戶想要的數據;
4)代理服務器向Internet 上的遠端服務器發送數據請求;
5)遠端服務器響應,返回相應的數據;
6)代理服務器取得遠端服務器的數據,返回給客戶端,並保留一份到本身的數據緩存中。vim

 

Squid代理服務器工做在TCP/IP的應用層:瀏覽器

2、squid分類
按照代理類型的不一樣,能夠將Squid代理分爲正向代理和反向代理。緩存

正向代理根據實現方式的不一樣,又能夠分爲普通代理和透明代理。
1)普通代理:須要客戶機在瀏覽器中指定代理服務器的地址、端口;
2)透明代理:適用於企業的網關主機(共享接入Internet)中,客戶機不須要指定代理服務器地址、端口等信息,代理服務器須要設置防火牆策略將客戶機的Web訪問數據轉交給代理服務程序處理;服務器

反向代理:是指以代理服務器來接受internet上的鏈接請求,而後將請求轉發給內部網絡上的服務器,並將從服務器上獲得的結果返回給internet上請求鏈接的客戶端,此時代理服務器對外就表現爲一個服務器。網絡

3、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) 設置開機自啓動,在三、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 %&gt;a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}&gt;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  #管理員郵箱

4、普通代理服務
即標準的、傳統的代理服務,須要客戶機在瀏覽器中指定代理服務器的地址、端口。
實驗拓撲圖以下:

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) 測試

9

 

5、透明代理服務
適用於企業的網關主機,客戶機不須要指定代理服務器地址、端口等信息,經過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) 在瀏覽器中,取消代理設置

16

6) 測試

20

透明代理測試成功。

 

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:

28

Web2:

27

6)查看squid 的訪問日誌。

26

 

7、實際應用
下面實驗將模擬經過不一樣的域名訪問不一樣的機器,簡單實現企業應用中的負載均衡。客戶端在瀏覽器地址欄中輸入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 文件裏直接指定。

32

4) 測試網絡狀況 

33

5) 測試www.squid.dev

34

6) 測試bbs.squid.dev

35

7) 查看squid 訪問日誌

36

8) 查看兩臺服務器的apache 訪問日誌

[root@server~]# tailf /var/log/httpd/access.log

40

測試成功。

相關文章
相關標籤/搜索