反向代理服務器有兩種傳輸模式:
1.同步模式:(如:squid)用戶發起請求,請求立即被轉到後端的服務器,於是在瀏覽器和後端服務器之間就建立了一個連接,在請求完成前這個連接是一直存在的。
2.異步模式:(如:nginx)用戶發起的請求會發送到nginx,nginx接收到所有的數據後在轉發到後端的服務器,後端服務器處理完成後把數據返回給nginx,nginx在返回給用戶。
由此可見如果用戶發起的請求的數據比較大,或者用戶端的網速比較慢,同步模式時後端服務器的連接數相對於異步模式會比較多,壓力也比較大。
squid 反向代理的工作原理的基礎上,指出反向代理技術在提高網站訪問速度,增強網站可用性、安全性方面有很好用途。作者在具體實驗環境下,利用 DNS 輪詢和 Squid 反向代理技術,實現了網站的負載均衡,從而提高了網站可用性和可靠性。
現在有許多大型的門戶網站如 SINA 都採用 squid 反向代理技術來加速網站的訪問速度,可將不同的 URL 請求分發到後臺不同的 WEB 服務器上,同時互聯網用戶只能看到反向代理服務器的地址,加強了網站的訪問安全。
反向代理的概念
反向代理服務器又稱爲 WEB 加速服務器,它位於WEB 服務器的前端,充當WEB 服務器的內容緩存器。
系統結構
系統結構
反向代理服務器是針對 WEB 服務器設置的,後臺 WEB 服務器對互聯網用戶是透明的,用戶只能看到反向代理服務器的地址,不清楚後臺 WEB 服務器是如何組織架構的。當互聯網用戶請求 WEB 服務時,DNS 將請求的域名解析爲反向代理服務器的 IP 地址,這樣 URL 請求將被髮送到反向代理服務器,由反向代理服務器負責處理用戶的請求與應答、與後臺 WEB 服務器交互。利用反向代理服務器減輕了後臺 WEB 服務器的負載,提高了訪問速度,同時避免了因用戶直接與 WEB 服務器通信帶來的安全隱患。
Squid 反向代理的實現原理
目前有許多反向代理軟件,比較有名的有 Nginx 和 Squid 。 Nginx 是由 Igor Sysoev 爲俄羅斯訪問量第二的 Rambler.ru 站點開發的,是一個高性能的 HTTP 和反向代理服務器,也是一個 IMAP/POP3/SMTP 代理服務器。
Squid 是由美國一項研究計劃,其目的爲解決網絡帶寬不足的問題,支持HTTP,HTTPS,FTP 等多種協議。
Squid反向代理服務器位於本地 WEB 服務器和 Internet 之間 , 組織架構如圖:
客戶端請求訪問 WEB 服務時,DNS 將訪問的域名解析爲 Squid 反向代理服務器的 IP 地址,這樣客戶端的 URL 請求將被髮送到反向代理服務器。如果 Squid 反向代理服務器中緩存了該請求的資源,則將該請求的資源直接返回給客戶端,否則反向代理服務器將向後臺的 WEB 服務器請求資源,然後將請求的應答返回給客戶端,同時也將該應答緩存在本地,供下一個請求者使用。 Squid 反向代理一般只緩存可緩衝的數據(比如 html 網頁和圖片等),而一些 CGI 腳本程序或者 ASP、JSP 之類的動態程序默認不緩存。它根據從 WEB 服務器返回的 HTTP 頭標記來緩衝靜態頁面。
四個最重要 HTTP 頭標記:
Last-Modified: 告訴反向代理頁面什麼時間被修改
Expires: 告訴反向代理頁面什麼時間應該從緩衝區中刪除
Cache-Control: 告訴反向代理頁面是否應該被緩衝
Pragma: 用來包含實現特定的指令,最常用的是 Pragma:no-cache
利用 Squid 反向代理加速網站實例
本實例的域名是 wenjin.cache.fqj.com.cn,通過DNS的輪詢技術,將客戶端的請求分發給其中一臺 Squid 反向代理服務器處理,如果這臺 Squid 緩存了用戶的請求資源,則將請求的資源直接返回給用戶,否則這臺 Squid 將沒有緩存的請求根據配置的規則發送給鄰居 Squid 和後臺的 WEB 服務器處理,這樣既減輕後臺 WEB 服務器的負載,又提高整個網站的性能和安全性。該系統結構圖:
配置系統環境:
一臺 DNS 服務器:操作系統Centos6.4,軟件 BIND 9.5,IP 192.168.76.222 ;
三臺 Squid 服務器:操作系統 Centos6.4,軟件 Squid 3.0,相應的 IP 如下:
Squid1:192.168.76.223
Squid2:192.168.76.224
Squid3:192.168.76.225
三臺 WEB 服務器:操作系統Centos6.4,應用軟件 apache+Mysql,相應的 IP 地址如下:
webServer1:210.82.118.195
webServer2:192.168.76.226
webServer1:192.168.76.227
配置 DNS 服務器
軟件利用 centos6.4自帶的 bind 軟件。然後針對該系統配置 bind,首先修改 bind 配置文件 /etc/namedb/named.conf,在文件中添加
複製代碼 代碼如下:zone "cache.xxx.com.cn"{
type master;
file "master/ cache.fqj.com.cn ";
};
再在 /etc/namedb/master 目錄下添加 cache.fqj.com.cn 文件,該文件的內容如下:
複製代碼 代碼如下:$TTL3600
@ IN SOA search. xxx.com.cn. root. xxx.com.cn. (
20080807; Serial
3600; Refresh
900 ; Retry
3600000 ; Expire
3600 ) ; Minimum
IN NS search.xxx.com.cn.
1 IN PTR localhost.xxx.com.cn.
wenjin IN A 192.168.76.223
wenjin IN A 192.168.76.224
wenjin IN A 192.168.76.225
這樣當用戶請求的時候,DNS 通過輪詢機制將 wenjin.cache.fqj.com.cn 的域名解析爲 192.168.76.223、192.168.76.224 和 192.168.76.225 其中之一。
配置完成後,啓動 bind 服務。
可在 /etc/rc.conf 中設置 named_enable="YES" 使得開機自啓動。
用 pf -ex | grep named 查看 bind 服務是否起來;
用 nslookup wenjin.cache.fqj.com.cn 測試 bind 服務是否正常運行。
配置 Squid1 服務器
下載squid-3.4.0.3.tar.gz軟件包
https://launchpad.net/squid/3.4/3.4.0.3/+download/squid-3.4.0.3.tar.gz
安裝依賴包:yum install -y gcc gcc-c++ make perl perl-devel
tar zxvf squid-3.4.0.3.tar.gz
./configure --prefix=/usr/local/squid
make && make install
groupadd squid
useradd -g squid -s /sbin/nologin squid
chown -R squid /usr/local/squid/var/logs/ /usr/local/squid/var/cache/
配置squid反向代理
清除默認配置,添加自定義的配置:
cache_effective_user squid
cache_effective_group squid
######### 設定 squid 的主機名 , 如無此項 squid 將無法啓動
visible_hostname squid1.nlc.gov.cn
############# 配置 squid 爲加速模式 #################
http_port 80 accel vhost vport
icp_port 3130
##### 配置 squid2、squid3 爲其鄰居,當 squid1 在其緩存中沒有找到請求資源時, 通過 ICP 查詢去其鄰居中取得緩存 .
cache_peer squid2.xxx.com.cn sibling 80 3130
cache_peer squid3.xxx.com.cn sibling 80 3130
##### squid1 的三個父節點,originserver 參數指明是源服務器,
round-robin 參數指明 squid 通過輪詢方式將請求分發到其中一臺父節點;
squid 同時會對這些父節點的健康狀態進行檢查,如果父節點 down 了,那麼 squid 會從剩餘origin 服務器中抓取數據.
cache_peer 210.82.118.195 parent 8080 0 no-query originserver round-robin name=webServer1
cache_peer 192.168.76.226 parent 8080 0 no-query originserver round-robin name=webServer2
cache_peer 192.168.76.227 parent 8080 0 no-query originserver round-robin name=webServer3
#### 將 wenjin.cache.xxx.com.cn 域的請求通過 RR 輪詢方式轉發到三個父節點中的一個
cache_peer_domain webServer1 webServer2 webServer3 wenjin.cache.xxx.com.cn
##### 下面是一些訪問控制、日誌和緩存目錄的設置
acl localnet src 192.168.76.223 192.168.76.224 192.168.76.225
http_access allow all
icp_access allow localnet
cache_log /usr/local/squid/var/logs/cache.log
access_log /usr/local/squid/var/logs/access.log squid
cache_dir ufs /usr/local/squid/var/cache/ 1000 16 256
####### 對 squid 的一些優化 ###############
maximum_object_size 10240 KB ### 能緩存的最大對象爲 10M
maximum_object_size_in_memory 512 KB ### 內存中緩存的最大對象 512K
cache_mem 256 MB ###squid 用於緩存的內存量,最後進行保存
在 /etc/hosts 文件中添加
複製代碼 代碼如下:
192.168.76.223 squid1.xxx.com.cn
192.168.76.224 squid2.xxx.com.cn
192.168.76.225 squid3.xxx.com.cn保存
維護命令
/usr/local/squid/sbin/squid -s #啓動squid
/usr/local/squid/sbin/squid –k parse #檢查squid配置文件是否正確
/usr/local/squid/sbin/squid –N -z #生成緩存目錄
配置 squid2 和 squid3 服務器
squid2 和 squid3 配置方法和配置參數和 squid1 一樣,配置完成後,分別啓動這兩個服務器上的 squid 服務。
在 squid 日誌文件 cache.log 中,出現如下日誌信息則說明三臺 squid 之間成功配置爲 sibling,且配置了三個父代理。
2013/11/17 10:08:47| Configuring Sibling squid1.xxx.com.cn/80/3130
2013/11/17 10:08:47| Configuring Sibling squid3.xxx.com.cn/80/3130
2013/11/17 10:08:47| Configuring Parent 210.82.118.195/8080/0
2013/11/17 10:08:47| Configuring Parent 192.168.76.226/8080/0
2013/11/17 10:08:47| Configuring Parent 192.168.76.227/8080/0
2013/11/17 10:08:47| Ready to serve requests.
進行測試
測試之前,保證 DNS 服務、三臺 squid 服務和三臺 web 服務都正常起來。在客戶端輸入 http://wenjin.cache.xxx.com.cn,
則正確的顯示該網頁。服務器端的響應對客戶端是透明的,客戶端不知道請求是由哪臺 WEB 服務器處理的;而且其中某 臺 Squid 服務器或 WEB 服務器發生故障,也不影響服務的正常運行。
測試命中率:
這裏-p後面根的是端口,根據自己具體的情況而定。如果是3128端口,就把上面的80換成3128就可以了
[root@test01 ~]# /usr/local/squid/bin/squidclient -p 80 mgr:info
Sending HTTP request ... done.
HTTP/1.1 200 OK
Server: squid/3.4.0.3
Mime-Version: 1.0
Date: Wed, 11 Dec 2013 09:08:36 GMT
Content-Type: text/plain
Expires: Wed, 11 Dec 2013 09:08:36 GMT
Last-Modified: Wed, 11 Dec 2013 09:08:36 GMT
X-Cache: MISS from www.eshow365.com
Via: 1.1 www.eshow365.com (squid/3.4.0.3)
Connection: close
Squid Object Cache: Version 3.4.0.3
Build Info:
Start Time:Wed, 11 Dec 2013 08:06:39 GMT
Current Time:Wed, 11 Dec 2013 09:08:36 GMT
Connection information for squid:
Number of clients accessing cache:3
Number of HTTP requests received:363
Number of ICP messages received:0
Number of ICP messages sent:0
Number of queued ICP replies:0
Number of HTCP messages received:0
Number of HTCP messages sent:0
Request failure ratio: 0.00
Average HTTP requests per minute since start:5.9 #每分鐘http request的數量
Average ICP messages per minute since start:0.0
Select loop called: 7788 times, 477.226 ms avg
Cache information for squid:
Hits as % of all requests:5min: 0.0%, 60min: 16.3% #Cache Request命中率
Hits as % of bytes sent:5min: -0.0%, 60min: 24.6% #Cache Byte命中率
Memory hits as % of hit requests:5min: 0.0%, 60min: 0.0%
Disk hits as % of hit requests:5min: 0.0%, 60min: 97.3%
Storage Swap size:3660 KB #存放cache的硬盤使用大小
Storage Swap capacity: 0.4% used, 99.6% free
Storage Mem size:2864 KB #存放cache的內存使用大小
Storage Mem capacity: 1.1% used, 98.9% free
Mean Object Size:17.18 KB
Requests given to unlinkd:0
Median Service Times (seconds) 5 min 60 min:
HTTP Requests (All): 0.00000 0.28853
Cache Misses: 0.00000 0.30459
Cache Hits: 0.00000 0.00000
Near Hits: 0.00000 0.00000
Not-Modified Replies: 0.00000 0.00000
DNS Lookups: 0.00000 0.00000
ICP Queries: 0.00000 0.00000
Resource usage for squid:
UP Time:3716.636 seconds
CPU Time:1.097 seconds
CPU Usage:0.03%
CPU Usage, 5 minute avg:0.01%
CPU Usage, 60 minute avg:0.02%
Process Data Segment Size via sbrk(): 8264 KB
Maximum Resident Size: 55472 KB
Page faults with physical i/o: 0
Memory usage for squid via mallinfo():
Total space in arena: 8396 KB
Ordinary blocks: 8254 KB 51 blks
Small blocks: 0 KB 0 blks
Holding blocks: 656 KB 2 blks
Free Small blocks: 0 KB
Free Ordinary blocks: 142 KB
Total in use: 142 KB 2%
Total free: 142 KB 2%
Total size: 9052 KB
Memory accounted for: #內存使用情況
Total accounted: 3762 KB 42%
memPool accounted: 3762 KB 42%
memPool unaccounted: 5290 KB 58%
memPoolAlloc calls: 79957
memPoolFree calls: 80460
File descriptor usage for squid:
Maximum number of file descriptors: 1024 #系統最大file descriptor數
Largest file desc currently in use: 18 #目前使用file descriptor最大值
Number of file desc currently in use: 10 #目前正在使用的file descriptor數
Files queued for open: 0
Available number of file descriptors: 1014
Reserved number of file descriptors: 100
Store Disk files open: 0
Internal Data Structures:
265 StoreEntries #Cache中存放的 object 的數量
219 StoreEntries with MemObjects #內存中 object 數量
218 Hot Object Cache Items #熱點object 的數量
213 on-disk objects # 硬盤上的object的數量
從訪問日誌中查看命中情況
[root@test01 ~]# tail -f /usr/local/squid/var/logs/access.log | grep HIT
1386753542.844 4 180.153.223.26 TCP_MEM_HIT/200 60317 GET http://www.eshow365.com/guowai/country-381.html - HIER_NONE/- text/html
常用的squidclient查詢語句如下:
squidclient -p 80 mgr:info #取得squid運行狀態信息:
squidclient -p 80 mgr:mem #取得squid內存使用情況:
squidclient -p 80 mgr:objects #取得squid已經緩存的列表,使用時要小心,可能會造成squid崩潰:
squidclient -p 80 mgr:diskd #取得squid的磁盤使用情況:
squidclient -p 80-m PURGE http://www.361way.com #強制更新某個url,即清理某連接的squid
轉載自:http://fuquanjun.blog.51cto.com/5820068/1339465