安裝運行squid後用命令:
squidclient -t 1 -h localhost -p 80 mgr:info 查看命中率情況
Request Hit Ratios: 5min: 99.6%, 60min: 98.7% Cache Request命中率
Byte Hit Ratios: 5min: 100.0%, 60min: 100.0% Cache Byte命中率
如果命中率低 則
1 apache中的模塊 mod_expires是否打開
2 調整squid中的參數
# cache_mem 8 MB
cache_mem 64 MB
# maximum_object_size 4096 KB
maximum_object_size 16384 KB
# maximum_object_size_in_memory 8 KB
maximum_object_size_in_memory 256 KB
# ipcache_size 1024
ipcache_size 2048
#Default:
cache_dir ufs /usr/local/squid/cache 2048 32 512
3 如果apache中使用了deflate壓縮 設置 cache_vary on
4 如果用nginx 可以用第三方模塊mod_urlhash 提高命中率
nginx過濾不良訪問提高squid的命中率:
.1、對靜態內容加以問號的訪問
例如http://www.7jiejie.com?abc,這樣的請求會透過squid緩存,直達後端服務器,並且在squid中保存緩存,從而造成壓力和內存浪費。
在nginx的server中加入對html文件和首頁等的過濾規則以解決此問題,此規則判斷首頁和html、jpg、gif結尾的文件,如果結尾有?xxx,則拋出403錯誤,由error_page接收,並用302跳轉到正確的地址。
location ~* (.html$)|(^/$)| (.jpg$)|(.gif$){
proxy_pass http://www.7jiejie.com;
if ($is_args)
{
return 403;
error_page 403 =200 $scheme://$host$uri;
}
}
這個方式也不是非常的完美,在測試中試圖使用rewrite來達成目的,但rewrite之後會保留原來的$args即?的內容,所以不能成功。另外,如果url中有中文,則跳轉是會失敗,所以要保證url中不含有中文,包括url_encode的結果。
2、對靜態內容發送POST請求
這種請求也會透過squid,但不會在squid緩存內容。
依葫蘆畫個瓢,以下配置可解決
location ~* (.html$)|(^/$)| (.jpg$)|(.gif$){
proxy_pass http://www.7jiejie.com;
if ($request_method = POST){
return 403;
error_page 403 =200 $scheme://$host$uri;
}
}