Squid Proxy

Squid(http://www.squid-cache.org)
Squid=主要服務程式+DNS查詢程式+部份重寫請求和執行認證的程式+部份管理工具
ps:
Squid用一個單獨的、非模組化的、I/O驅動的進程來處理所有的用戶端請求,和一般的代理緩存軟體不同

支援
全功能的網頁代理快取 ex:HTTP,FTP,URL
gopher,SSL,存取控制,本身快取DNS查詢
ICP,HTCP,CARP,WCCP數種快取通訊協定

其他功能
可使用多台squid快取伺服器建構出快取架構
可用cachemgr.cgi透過網頁或利用SNMP做即時快取狀況查詢
使用ICP(輕量Internet緩存協議)使Squid能夠實現層疊的代理陣列,從而最大限度地節約帶寬

DNS查詢方式
Squid啟動後會派生出預先指定數目的dnsserver行程,每一個dnsserver行程可以執行單獨的DNS查詢,這可減少伺服器等待DNS查詢的時間。

################################################################# 

安裝部份

1
以yum安裝

yum install squid

2
設定squid.conf參數

ps:通常在/etc/squid.conf
2.1
一般設定
http_port 3128 設定proxy port為3128
icp_port 3130 查詢封包觀查的port為3130
http_access allow all 打開squid所有權限
cache_effective_user nobody 設定squid管理者
cache_effective_group nogroup 設定squid管理者群組
ps:會被寫入到目錄的擁有者與群組需改成cache_effective的user和group
cache_mgr account@hostname 設定squid管理者信箱
2.2
指定快取目錄
cache_dir < aufs|ufs> < cache dir> < mb size> < dir1> < dir2> 
 aufs功能需在安裝時有–enable-async-io才支援,可將硬碟速度發揮到極限
 cache dir為想要做為快取的位置
 mb size為cache dir的最大值
 dir1為cache dir下的次目錄數量
 dir2為dir1下的次目錄數量,dir2建議為dir1的16倍
ex:aufs /usr/local/squid/var/cache 1000 16 256
2.3
以下是認證相關參數,建議關閉
#auth_param basic children 5
#auth_param basic realm Squid proxy-caching web server
#auth_param basic credentialsttl 2 hours
2.4
設定那些來源可以使用proxy
acl our_networks src 192.168.1.0/14
http_access allow our_networks

3
快取目錄設定

3.1
cache dir目錄的擁有者與群組需改成cache_effective的user和group
3.2
執行sbin/squid -z 來建立並格式化cache_dir,出現creating swap directories表示初始化成功


4
啟動squid

執行
service squid start

su nobody -c “RunCache位置 &”

sbin/squid -N -d 1
ps:
測試是否啟動squid
bin/squidclient -g 0 -h < proxy_ip> -p 3128

ps:
若squid.conf的cache dir重設,則需
1刪除原cache dir
2建立並設定cache dir擁有者與群組
3執行squid -z


squid相關目錄
bin/ 存放squid主要執行的scripts
sbin/ 存放squid的執行檔
etc/ 存放squid設定檔
libexec/ 函式庫
man/ 說明文件
share/ 存放錯誤訊息代碼表示檔,一些小圖示等
var/ 存放log檔

squid相關檔案
etc/squid.conf 主要設定檔
squid/etc/mime.conf 設定internet上的檔案格式,使用預設即可
sbin/squid 執行檔
bin/RunCache 執行squid的script
bin/RunAccel 可讓www服務透過squid加速

squid記錄檔放置位置
cache_access_log /usr/local/squid/var/logs/access.log
cache_log /usr/local/squid/var/logs/cache.log
cache_store_log /usr/local/squid/var/logs/store.log
pid_filename /usr/local/squid/var/logs/squid.pid
ps:存放log檔目錄的擁有者與群組需改成cache_effective的user和group

###############################################################

Squid.conf設定部份

squid.conf效能設定

#與磁碟容量有關的設定
cache_swap_low 90 //若cache dir滿到cache_swap_high,就將容量減少到90%
cache_swap_high 95 //當cache dir容量到95%時減少容量,越大的proxy此值不可設太高
maximum_object_size 4096 KB //若cache dir夠大可改成32768

#與記憶體保存資料有關的設定
cache_mem 8 MB //額外提供給squid 8m的記憶體,協助作業
maximum_object_size_in_memory 8 KB

#經dns正反解及IP的結果
ipcache_size 1024 //ip快取
ipcache_low 90 //和cache_swap_low動作差不多
ipcache_high 95 //和cache_swap_high動作差不多
fqdncache_size 1024 //fqdn的快取

ps:官方建議實際記憶體最好是squid所用記憶體兩倍大
ps:預設每1g的cache dir會消耗掉10m記憶體
ps:squid使用的記憶體公式為:因cache dir所用的+cache_mem+15mb
ex:
1g的cache dir,及squid.conf中的cache_men為8m
則記憶體用量為10m+8m+15m=33m

………………………..

squid.conf中acl用法

定義acl的格式為acl < acl name> < type>
< type>可以如下:
src < ip/cidr> 來源ip
src < iprange/cidr> 來源ip範圍
srcdomain < fqdn> 來源領域名稱
dst < ip/cidr> 目標ip
dstdomain < fqdn> 目標網域名稱
ex:acl acl2 140.116.44.120-140.116.44.130/24
ex:acl acl3 srcdomain .mine.nu
ex:acl acl3 dstdomain .tw
url_regex < regex>  網域正規表示法
urlpath_regex < regex> 網址正規表示法
ex:acl url1 url_regex ^http://www.google.com.*
ex:acl url2 urlpath_regex .php$


acl的常見應用

不對某類型網頁做cache
ex:不對php網頁做cache
hierarchy_stoplist php ?
acl denyphp urlpath_regex php ?
no_cache deny denyphp

某些網頁自己抓資料
always_direct allow < acl1,acl2,…>

是否要透過上層proxy抓資料
never_direct < allow|deny> < acl1,acl2,…>
allow表示一定經由上層Proxy捉資料
deny表示一定是自己向外頭捉資料

限制使用
http_access < allow|deny> < acl>
ex:只有192.168.1.0/24可使用proxy
acl inside src 192.168.1.0/24
http_access allow inside
http_access deny all


……………………….

squid.conf中設定proxy by ldap

#auth_param設定
auth_param basic program /usr/lib/squid/squid_ldap_auth -b “< base dn>” -f “< filter rule>” < ldap server ip>
ps:各參數說明
-v 表示LDAP版本
-b 設定BaseDN
-D 設定UserDN
-w 設定slapd.conf文件中”rootpw 管理者密碼
-f 設定Uid認證
-h 設定LDAP Server的位置可使用FQDN or IP的方式
ex:auth_param basic program /usr/lib/squid/squid_ldap_auth -b “dc=systw,dc=net” -f “(&(uid=%s)(userPassword=*))” 192.168.100.100
auth_param basic children 5 #認證程式數量
auth_param basic realm squid proxy-caching web srever
auth_param basic credentialsttl 360 minutes

#squid使用ldap設定
acl ldap proxy_auth REQUIRED
http_access allow ldap

……………………….

squid.conf中時間設定
refresh_pattern < regex> < min minute> < % max minute>
< min minute>最小時間內檔案有更動就直接更新
< % max minute>目標檔案上次更新時間已超過最大時間的百分比時
ex:proxy在一天(1440)後存取ftp網頁時,cache資料會被更新
refresh_pattern ^ftp: 1440 20% 10080

connect_timeout 30 seconds 連到其他主機超過30秒後失敗
peer_connect_timeout 10 seconds 連到上層proxy主機超過10秒後失敗
request_timeout 1 minutes 連線後要求時間1分鐘
persistent_request_timeout 20 seconds 連線要求時間20秒

############################################################

應用部份


設定transparent proxy

1
設定squid.conf

2.6版前
設定proxy的squid.conf
httpd_accel_host fqdn
httpd_accel_port 80
ps:因設定httpd_accel_host後,cache的設定會自動被終止,須在加以下參數才可啟用
httpd_accel_with_proxy on
httpd_accel_uses_host_header on
2.6版後
http_port 3128 transparent

2
設定nat

iptables -t nat -A PREROUTING -i eth0 -p tcp -s 192.168.0.0/24 –dport 80 -d 127.0.0.1 -j REDIRECT –to-ports 3128

…………………………………………………………………………….

使用WCCP v2 for L2 mode

switch設定
1
啟用wccp

(config)#ip wccp web-cache
2
指定進來該介面封包做WCCP的轉送

(config)#interface < interface >
(config-if)#ip wccp web-cache redirect in
ps:
ip wccp web-cache redirect out 是指定出去的介面才做proxy
ps:
show ip wccp 可看到狀態
debug ip wccp events 可用來debug

squid設定

#設定在127.0.0.1 (Loopback 0)的 Port 3128 上跑 Transparent Mode Proxy
http_port 127.0.0.1:3128 transparent

#指定要跑WCCP Switch的IP
wccp2_router 192.168.6.30

#指定版本
wccp_version 2

#指定由Router/Switch上所轉送來的封包為L2 mode格式
wccp2_forwarding_method 2

#指定由Server上所送回Router/Switch的封包為L2 mode格式
wccp2_return_method 2

#指定選派方式
wccp2_assignment_method 2
ps:
WCCP2選派的方式1是以Hash 來選派,2是Mask來選派,通常Router支援Hash Method而Switch 支援Mask Method

此為transparent,當使用者透過wccp上網時
來源ip還是顯示使用者的,而不是squid的ip
reference:http://www.ringline.com.tw/epaper/Product980201.htm

#################################################################

報表部份

cache manager
將squid提供的cachemgr.cgi放到apache的cgi-bin目錄下
進入http://proxy server url/cgi-bin/cachemgr.cgi

常見說明

在General Runtime Information下有這些
Number of clients accessing cache: 557 //使用proxy的電腦數量
Number of HTTP requests received: 599308 //客戶端http要求數量
Number of ICP messages received: 418549 //接受到的icp query數量
Number of ICP messages sent: 418549 //發出icp query數量
HTTP requests per minute: 4883.8 //每分鐘http request的數量
ICP messages per minute: 6821.6 //每分鐘處理的icp數量
Request Hit Ratios: 5min: 34.7%, 60min: 40.3% //Cache Request命中率
Byte Hit Ratios: 5min: 37.0%, 60min: 44.1% //Cache Byte命中率
Storage Swap size: 10874153 KB //存放cache的磁碟使用量
Storage Mem size: 102396 KB //存放cache的記憶體使用量
Median Service Times (seconds) 5 min 60 min: //平均回應時間
CPU Usage: 82.33% //自程式啟動起計算,CPU使用率
CPU Usage, 5 minute avg: 22.82% //最近5分鐘CPU使用率
CPU Usage, 60 minute avg: 77.33% //最近60分鐘CPU使用率
Maximum number of file descriptors: 8192 //系統最大file descriptor數
Largest file desc currently in use: 2044 //目前使用file descriptor最大值
Number of file desc currently in use: 430 //目前正在使用的file descriptor數
719504 StoreEntries //Cache中存放的快取檔案數量
13774 StoreEntries with MemObjects //記憶體鬥的快取檔案數量
709747 on-disk objects //磁碟機中存放的快取檔案數量

在Peer Cache Statistics有這些
Status : Up //連線狀態
FETCHES : 252870 0% //由parent餵送的物件數量
Last failed connect() at: 02/May/2000:10:13:16 +0800 //上一次與parent斷線發生的時間

在Cache Utilization內有這些
sample_start_time = 957238720.384495 (Tue, 02 May 2000 03:38:40 GMT) //取樣起始時間
sample_end_time = 957239023.269460 (Tue, 02 May 2000 03:43:43 GMT) //取樣結束時間
client_http.requests = 109.031493/sec //客戶端每秒的http要求數
client_http.hits = 47.602231/sec //客戶端每秒的http命中次數
client_http.kbytes_in = 50.963243/sec //客戶端每秒送出的資料量
client_http.kbytes_out = 514.066454/sec //每秒送出至客戶端的資料量
client_http.all_median_svc_time = 1.384466 seconds //反應時間
server.all.requests = 63.053642/sec //squid幫客戶端抓取的要求數
server.all.kbytes_in = 278.699869/sec //squid對外抓取的資料量
server.all.kbytes_out = 32.121106/sec //squid對外送出需求的資料量
dns.median_svc_time = 0.007785 seconds //DNS的反應時間
aborted_requests = 10.710337/sec //客戶端中斷連線請求的次數
cpu_time = 274.290000 seconds //squid佔用CPU的時間
wall_time = 302.884965 seconds //取樣的時間總長
cpu_usage = 90.559134% //平均CPU使用率

reference:
http://forum.arjin.tw/lpic/suse/9.3/suselinux-adminguide_zh_TW/html/sec.squid.cachemgr.html

………………………………

sarg(Squid Analysis Report Generator)
(http://sarg.sourceforge.net/)
可分析squid log檔產生報表

安裝
下載tar.gz解壓縮後進入該目錄內,執行
./configure –prefix=”/usr/local/sarg”
make
make install

設定
找到sarg.conf後確任以下設定
access_log /var/logs/squid/access.log //squid的log檔位置
temporary_dir /tmp //記錄檔暫存位置
output_dir /var/www/html/sarg //報表輸出位置
overwrite_report no //報表是否覆寫
exclude_codes /usr/local/sarg/exclude_codes //exclude_codes位置

執行sarg
#/usr/local/sarg/bin/sarg

看報表
進入http://proxy server url/sarg/


If you want to generate daily reports from Squid proxy server logs,
1
create script:
#!/bin/bash
#Get current date
TODAY=$(date +%d/%m/%Y)
#Get yesterday’s date
YESTERDAY=$(date –date yesterday +%d/%m/%Y)
/usr/bin/sarg -z -d $YESTERDAY-$TODAY > /dev/null 2>&1
exit 0

2
And add it to cron jobs:
55 23 * * * /scripts/sarg_daily_report