proxy 代理伺服器
DESCRIPTION
Proxy 代理伺服器. Kenduest ( 小州). mailto:[email protected]. Proxy Server 簡介. What’s Proxy Server Proxy Server 中文稱呼為代理伺服器 Proxy Server 的功能就是代理使用者的請求, 幫使用者傳回所需要的資料,然後儲存一份。 若是下次有使用者要求相同資料時,就不需要 連線出去抓取,直接由 Proxy Server 把快取區域 內的資料傳回給使用者。 Proxy Server 的快取特性,可以加快連線端抓取 - PowerPoint PPT PresentationTRANSCRIPT
2
Proxy Server 簡介What’s Proxy Server
Proxy Server 中文稱呼為代理伺服器Proxy Server 的功能就是代理使用者的請求,幫使用者傳回所需要的資料,然後儲存一份。若是下次有使用者要求相同資料時,就不需要連線出去抓取,直接由 Proxy Server 把快取區域內的資料傳回給使用者。Proxy Server 的快取特性,可以加快連線端抓取資料的速度 ( 已經 Cache 住的資料就不需要重複抓取 ) 與減低網路資源浪費的情況。
3
Proxy Server 使用目的使用 proxy server 有何好處?
目前上網人數暴增,頻寬不夠使用。其中使用上以 WWW 居多。許多瀏覽過資料快取住,重複瀏覽不需要重新抓,具備 Firewall 隔離基本功能具備色情防制的功能目前學術網路管制直接對外抓取 Web 資料,強制使用 Proxy 代理。在多台 Proxy 聯合運作下,資料重複使用量相當高。
4
Child 、 Parent 、 Sibling 關係
5
硬體周邊的選擇選擇重點
建議使用 SCSI 硬碟,可以加快 IO 效率與減低CPU 的負載情況。單顆硬碟容量不要太大,建議 4GB ~ 8GB ,最好是 7600 轉速 or 以上。分割區劃分,建議 2GB ~ 4GB記憶體提供上,盡可能多一點記憶體。建議要有128MB 以上。專業多半被要有 512MB RAM 。CPU 越快越好,多顆 CPU 在程式是使用MultiThread 架構運作下效能會有大幅度提升。
6
Proxy 種類與選擇 (1)目前常見的 Proxy Server
Squid ApacheCERNNetscape Proxy ServerMSP (Microsoft Proxy Server)
目前世界上最多人使用的是 Squid Proxy , Hinet 、Seednet 等等各大 ISP 都使用該 Proxy Server 提供使用者 Proxy 使用。
7
Proxy 種類與選擇 (2)使用 Squid 來當 Proxy Cache Server
目前世界上許多 Proxy 主機多半是 Unix 系統,多半都採用 squid 當 proxy server 使用。squid 相當多人使用,功能強大,可以完成需多設定功能機制。網路上有許多 squid addons 程式工具,有利於分析與研究。squid proxy server 提供給 Web Client 存取使用,Client 端統一使用 HTTP 協定方式來溝通。其支援 HTTP 、 FTP 、 GOPHER 、 HTTPS等相關服務請求。
8
Squid 安裝與編譯 (1)取的 squid 套件
Download squid :http://www.squid-cache.org/Mirrors/ftp-mirrors.htmlhttp://www.squid-cache.org/Mirrors/http-mirrors.htmlftp://coda.nctu.edu.tw/WWW/Squid/squid-2/STABLEftp://ftp.squid-cache.org
9
Squid 安裝與編譯 (2)取的 squid 套件 (Compiling and install )tar zxvf squid-2.2.STABLE5-src.tar.gzcd squid-2.2.STABLE5./configure --help (看看有哪些編譯選項可以使用) export CFLAGS='-O2 -mcpu=pentium'./configure --prefix=/usr/local/squid --enable-dlmalloc \--enable-gnuregex --enable-async-io=160 \--enable-kill-parent-hack --enable-cache-digests \--enable-cachemgr-hostname=proxy.xxx.com.tw \--enable-err-language=Traditional_Chinese \--enable-poll --disable-ident-lookups \--enable-heap-replacement –enable-icmp
10
Squid 安裝與編譯 (3)Compiling and install ( 續 )
make;make install
安裝好目錄結構 :
/usr/local/squid – 主目錄/usr/local/etc – 組態檔案目錄/usr/local/logs – 記錄檔案存放目錄原始碼套件額外提供的程式auth_modules/NCSA – 認證程式,透過 htpasswd 建立auth_modules/SMB – 認證程式,透過 smb 取得認證 auth_modules/getpwnam – 認證程式,使用系統密碼
11
Squid 安裝與編譯 (4)RedHat 、 Mandrake 系統,使用 RPM 安裝
mount /mnt/cdromcd /mnt/cdrom/RedHat/RPMS (for RedHat)cd /mnt/cdrom/Mandrake/RPMS (for Mandrake)rpm –ihv squid*.rpm
可以使用 rpm –qlp squid-xx.xx-xx.rpm 觀察該 rpm 檔案目錄檔案清單與結構。或者是使用 rpm –ql squid 觀察安裝在系統 squid 套件內容。RedHat 、 Mandrake 提供的 squid rpm 套件,主要程式在 /usr/sbin 目錄內。設定檔案目錄在 /etc/squid 內。其他相關在 /usr/lib/squid 目錄內。
12
Squid 快速參數設定 (1)修改 squid.conf 檔案 :
http_port : 設定 squid proxy 要監聽哪個 port
• http_port 3128
cache_mem : 設定 cache memory 大小• cache_mem 200 MB
• cache_mem 預設 8MB
• cache_mem_low ( 百分比使用,預設 75%)
• cache_high_low ( 百分比使用,預設 90%)
13
Squid 快速參數設定 (2)修改 squid.conf 檔案 :
cache_dir : 設定 cache 目錄位置與大小• cache_dir /var/spool/squid/cache 10240 16 256
• /var/spool/squid/cache 是儲存 cache 資料目錄• 10240 是存放快取資料的大小,單位是 MB
• 16 與 256 分別是 Level 1 與 Level 2
• 表示建立 16 的子目錄,每個子目錄又有 256 個目錄cache_access_log : 對外連線的紀錄
• cache_access_log /var/log/squid/access.log
cache_log : Squid 執行時的資訊 , 包括錯誤訊息 .
• cache_access_log /var/log/squid/cache.log
14
Squid 快速參數設定 (3)修改 squid.conf 檔案 :
cache_store_log : 設定物件紀錄檔案目錄位置• cache_store_log /var/log/squid/store.log
pid_filename : 指定 squid pid 檔案存放目錄位置• pid_filename /var/run/squid.pid
reference_age : 指定資料保存期限• reference 6 month
acl : 設定 acl 存取清單配合http_access : 設定可以連線存取權限
15
Squid 快速參數設定 (4)修改 squid.conf 檔案 :
acl : 設定 acl 存取清單配合acl & http_access 設定使用 :
• acl myhost1 192.168.1.0/255.255.255.0
• acl myhost2 140.1.0.96/255.255.255.248
• http_access allow myhost1
• http_access allow myhost2
cache_effective_user : 設定啟動時的身份• cache_effective_user squid
cache_effective_group : 設定啟動時的群組• cache_effective_group squid
16
Squid 的啟動 (1)啟動 squid proxy
先建立 squid 這個使用者與群組 :
• groupadd squid
• useradd –r –s /dev/null –g squid squid
先建立 squid 要使用的相關目錄• mkdir /var/spool/squid
• mkdir /var/log/squid
• chown squid.squid /var/spool/squid
• chown squid.squid /var/log/squid
17
Squid 的啟動 (2)啟動 squid proxy
先建立 squid proxy cache 目錄結構• squid -z
squid 程式使用說明• squid --help
執行 squid daemon 常駐• squid 或者是 squid &
• /etc/rc.d/init.d/squid start (for RedHat 、 Mandrake)
測試 squid 是否有常駐與工作• telnet localhost 3128
• ps auxw | grep “squid”
18
Squid 程式參數 squid proxy 程式參數顯示使用說明
• squid –h
-k 控制相關參數,後面接 :
• reconfigure : 改過設定,使用此參數讓設定生效• rotate : 截斷 log 紀錄,然後重新建立 log
• shutdown : 關閉 squid proxy
• kill : 強制關閉 squid proxy
• check : 檢查 squid.conf 是否正確• parse : 解析 squid.conf 檔案內容
19
Squid 進階設定部分 (1)squid.conf 進階參數設定 :
icp_port : 提供芳鄰 proxy server 提出 ICP 請求 回應的 port
• 預設是 3130
• icp_port 3130
cache_peer : 指定 parent or sibling cache 的 squid 主機• usage : hostname type http_port icp_port [opt]
• type : parent | sibling
• cache_peer proxy.xxx.com.tw sibling 3128 3130
no-query
20
Squid 進階設定部分 (2)acl 設定 : 定義 acl 存取清單
acl aclname src ip-address/netmask ... (clients IP address)
acl aclname src addr1-addr2/netmask ... (range of addresses)
• 定義連線存取來源• 舉例 :
acl host1 src 192.168.1.0/255.255.255.0 acl localhost src 127.0.0.1
acl aclname dst ip-address/netmask ... (URL host's IP)
• 定義要連線到的目的端• 舉例 :
acl des_host dst 140.0.0.0/255.0.0.0
21
Squid 進階設定部分 (3)acl 設定 : 定義 acl 存取清單 ( 續 )
acl aclname srcdomain .foo.com ...
• reverse lookup, client IP ,反查來源 IP 的主機• 範例 : acl the_src_domain srcdomain .xxxx.idv.tw
acl aclname dstdomain .foo.com ...
• Destination server from URL ,反查目的端主機• 範例 : acl the_dest_domain dstdomain .hello.com.tw
acl aclname srcdom_regex [-i] xxx ...
• regex matching client name
acl aclname dstdom_regex [-i] xxx ...
• regex matching server
22
Squid 進階設定部分 (3)acl 設定 : 定義 acl 存取清單 ( 續 )
http_access• 定義 acl list 內存取的權限• Usage: http_access allow | deny [aclname] ... [!aclname]
使用範例 :acl company_ip src 211.20.143.105/255.255.255.248acl sex_ip dst 63.1.0.0/255.255.255.248acl sex_domain dstdomain www.playbody.comhttp_access deny sex_iphttp_access deny sex_domainhttp_access allow company_ip
23
Squid 進階設定部分 (3)squid.conf 進階參數設定 :
cache_peer : ( 續 )
• options : proxy-only : 只有 cache ,不會儲存在硬碟內 weight = n : 預設是 1 ,越高優先權越高 no-query : 不送出 ICP Query default : 沒有 ICP_HIT 回應,由 default 負責
cache_peer 使用範例 :
• cache_peer proxy.xxx.com.tw sibling 3128 3130
no-query
24
Squid 進階設定部分 (3)squid.conf 進階參數設定 :
cache_peer_domain
• 用來指定某台 proxy server 代理特定主機網域• usage: cache_peer_domain cache_host domain [...]
• usage: cache_peer_domain cache_host !domain [...]
• 使用範例 :
cache_peer_domain proxy1.xxx.com.tw .com
cache_peer_domain proxy2.xxx.com.tw .net
cache_peer_domain proxy3.xxx.com.tw .org .tw
25
Squid 進階設定部分 (3)squid.conf 進階參數設定 :
cache_peer_access
• 與 cache_peer_domain 功能相同• 本參數額外提供可以使用 acl 設定清單• usage : cache_peer_access cache_host allow | deny
• 使用範例 :
cache_peer proxy1.xxx.com.tw sibling 3128 3130 cache_peer proxy2.xxx.com.tw sibling 3128 3130 acl bad-area1 dst 220.10.3.96/255.255.255.248acl bad-area2 dst 134.1.65.0/255.255.255.0cache_peer_access proxy1.xxx.com.tw deny bad-area1 cache_peer_access proxy2.xxx.com.tw deny bad-area2
26
Squid 進階設定部分 (3)squid.conf 進階參數設定 :
cache_mgr• 定義管理者的 e-mail• cache_mgr [email protected]
ftp_user• 定義當透過 proxy 存取 ftp 時,使用的 e-mail• ftp_user [email protected]
always_direct• 將特定的請求導向給要連線的 server• usage : always_direct allow | deny [!]aclname• acl local_dstip dst 192.168.1.0/255.255.255.0• always_direct local_dstip
27
Squid 進階設定部分 (3)squid.conf 進階參數設定 :
never_direct• 與 always_direct 功能相反• usage : never_direct allow | deny [!]aclname• acl local_srcip src 211.20.23.96/255.255.255.249• never_direct local_srcip
cachemgr_passwd• squid 提供的一個 cgi 管理程式。定義管理密碼• Usage: cachemgr_passwd password action action....• 使用範例 :
cachemgr_passwd secure_password all
28
Squid 進階設定部分 (3)squid.conf 進階參數設定 :
maximum_object_size• 設定 cache 最大物件檔案大小,超過將不 Cache 住• usage : maximum_object_size size (bytes)• 預設是 4MB
shutdown_lifetime• 當 squid 接受到 shutdown 信號時,將不會繼續
接受任何新的請求。該參數是設定多久內尚未傳完資料的話,也強制斷線,連線端將會收到timeout 的回應。
• usage : client_lifetime time_units• 預設是 1440 minutes
29
Squid 進階設定部分 (3)Cache_dir 的規劃 Cache 的一些演算式如下 Bucket 數 = CacheDir * Level1_Dir * Level2_Dir 總檔案數 = Bucket * FilesPerBucket 檔案平均大小 = Cache總容量 / 總檔案數量 原則: Level1 & Level2 的 Dir File Size 最好不超過 1024 Bytes ,也就是說每層目錄下不要有太多的子目錄或檔案。 Level1, Level2, FilesPerBucket 三個數字最好差不多大,才不會造成某個數字偏大。 n 檔案平均大小無一定的決定規則,因使用者習慣而異,先估計出檔案平均大小,再計算出總檔案數量,接著再去計算各層目錄與檔案數。 n可以的話,盡量用 16 的倍數。 nBucket 數量不要超過 65536
30
Squid 進階設定部分 (3)Sibling 的技術很重要,如果對方有允許抓取 cache digest table ,最好使用 no-query 選項,這樣對方沒有的東西就不會用 ICP_QUERY 去問了。 盡量減少無法交換 cache digest table 的 sibling ,
因為這種 sibling 只能用 ICP_QUERY 。
Cache digest + no-query 的聯合應用,可以強制proxy 只抓取 sibling
已經有的東西,不但節省頻寬,且可省掉許多落空的 ICP_QUERY ,也可提高 sibling 的 Hit Rate ,
一舉數得。
31
pwebstats 流量統計程式 (1)安裝 pwebstats 安裝需求 :
perl 5.000 or above ( 使用 perl –v 檢查版本 )fly package download from : http://www.unimelb.edu.au/fly/fly.htmlftp://www.unimelb.edu.au/pub/www/tools/unix/fly/
fly-1.6.5.tar.gzgdchart packagedownload from :pwebstatsftp://www.unimelb.edu.au/pub/www/tools/unix/
pwebstats/pwebstats-1.3.8.tar.gz
32
pwebstats 流量統計程式 (2)建立 /home/httpd/pwebstats 相關目錄
mkdir /home/httpd/utilitymkdir /home/httpd/utility/pwebstatsmkdir /home/httpd/utility/fly
( 註 : RedHat 7.x , Mandrake 7.2 /home/httpd 搬移到 /var/www 目錄內 )
安裝 flytar zxvf fly-1.6.5.tar.gzcd fly-1.6.5;make;cd ..cp –R fly-1.6.5 /home/httpd/utilitycd /home/httpd/utilityln –s fly-1.6.5 fly
33
pwebstats 流量統計程式 (3)安裝 pwebstats
tar zxvf pwebstats-1.3.8.tar.gzcd pwebstats-1.3.8vi pwebstats 檔案第一行檔案,把#!/usr/local/bin/perl 修正指向到 perl 實際路徑,比方 #!/usr/bin/perlcd .. ; cp –R pwebstats-1.3.8 /home/httpd/utilitymkdir /home/httpd/utility/pwebstats/logs修改 /home/httpd/utility/pwebstats-1.3.8 目錄內conf/squid-proxy.conf 檔案cd /home/httpd/utilityln –s pwebstats-1.3.8 pwebstats
34
pwebstats 流量統計程式 (4)pwebstats 設定檔案 squid-proxy.conf
server:proxy_xxxxServer_header: xxxx proxy server 快取服務•顯示在 pwebstats 首頁最上方。
logfile:/var/log/squid/access.log.1.gz•指定要統計的 squid access log 記錄檔案
outdir:/home/httpd/utility/pwebstats/logs• 指定輸出 log 統計的目錄
templates:/home/htdocs/pwebstats/templates • pwebstats 中的 templates 目錄
interval:daily•每天做 log 統計
35
pwebstats 流量統計程式 (5)pwebstats 設定檔案 squid-proxy.conf( 續 )
verbose:true•執行過程是否有訊息顯示。
fly_prog:/home/httpd/utility/fly/fly• 安裝好的 fly 執行檔位置。
local_patt:\.xxxx\.com\.tw$|^211\.20\.142• 指定 local lan 範圍,以便於不統計該區段
使用 crontab 每天定時排程產生統計圖表 (/etc/crontab)0 5 * * * root /home/httpd/utility/pwebstats/pwebstats –c /home/httpd/utility/pwebstats/conf/squid-proxy.conf
36
pwebstats 流量統計程式 (6)關於 crontab排程部分
目前系統都有安裝 logrotate 程式,可以定期對相關 log 檔案進行 rotate 的動作。/var/log/ 目錄內有許多 .1、 .2 檔案都是 logrotate程式處理產生的。以 messages 檔案來說, logrotate 的動作就是先把 messages 檔案改名成為 messages.1 檔案,然後對syslog daemon 下 SIGHUP 信號,如此 syslogd 會把對於 messages 檔案寫入動作關閉,然後重新產生messages 檔案。依系統設定,多半可能會對於 log檔案進行壓縮, messages.1 會壓縮成為 messages.1.gz
37
pwebstats 流量統計程式 (7)關於 crontab排程部分 ( 續 )
目前跑 logrotate 程式部分,安裝好 squid rpm 套件後,在 /etc/logrotate.d/ 目錄內可以找到 squid 檔案,內容就是告訴 logrotate 怎麼進行 rotate 動作的 script 檔案。/etc/crontab 檔案內,跑每天例行性行程敘述為 :02 4 * * * root run-parts /etc/cron.daily也就是每天 4:02 跑 run-parts /etc/cron.dailyrun-parts 會執行指定參數該目錄內所有 script 檔案。/etc/cron.daily 包含 logrotate 檔案,內容是 :#!/bin/sh/usr/sbin/logrotate /etc/logrotate.conf
38
pwebstats 流量統計程式 (8)關於 crontab排程部分 ( 續 )
/etc/logrotate.conf 檔案內有下面敘述 :include /etc/logrotate.d所以 /etc/logrotate.d/* 相關檔案都會被 logrotate 執行,連同也會執行到安裝 rpm 套件提供的 squid script 檔案。該 /etc/logrotate.d/squid 預設是使用 weekly 為一期rotate 時間,請改成 daily 每天跑一次。因為系統是在 4:02 左右進行 logrotate 動作,建議跑 pwebstats 統計 squid 記錄檔案,可以在每天 5:00處理之。
39
參考資料http://www.squid-cache.orghttp://squid-docs.sourceforge.net/latest/html/book1.htmhttp://www.squid-cache.org/Doc/FAQ/FAQ.htmlhttp://proxy.ntu.edu.twhttp://proxy.nctu.edu.twhttp://proxy.nsysu.edu.twhttp://www.cc.nsysu.edu.tw/~lmj/Squid.files/frame.htmhttp://turtle.ee.ncku.edu.tw/~tung/proxy/