390 likes | 1.2k Views
Proxy 代理伺服器. Kenduest ( 小州). mailto:Kenduest@cynix.com.tw. Proxy Server 簡介. What’s Proxy Server Proxy Server 中文稱呼為代理伺服器 Proxy Server 的功能就是代理使用者的請求, 幫使用者傳回所需要的資料,然後儲存一份。 若是下次有使用者要求相同資料時,就不需要 連線出去抓取,直接由 Proxy Server 把快取區域 內的資料傳回給使用者。 Proxy Server 的快取特性,可以加快連線端抓取
E N D
Proxy代理伺服器 Kenduest (小州) mailto:Kenduest@cynix.com.tw
Proxy Server 簡介 • What’s Proxy Server • Proxy Server 中文稱呼為代理伺服器 • Proxy Server 的功能就是代理使用者的請求, 幫使用者傳回所需要的資料,然後儲存一份。 若是下次有使用者要求相同資料時,就不需要 連線出去抓取,直接由 Proxy Server 把快取區域 內的資料傳回給使用者。 • Proxy Server 的快取特性,可以加快連線端抓取 資料的速度(已經 Cache 住的資料就不需要重複 抓取)與減低網路資源浪費的情況。
Proxy Server 使用目的 • 使用 proxy server 有何好處? • 目前上網人數暴增,頻寬不夠使用。其中使用上 以 WWW 居多。 • 許多瀏覽過資料快取住,重複瀏覽不需要重新抓, • 具備 Firewall 隔離基本功能 • 具備色情防制的功能 • 目前學術網路管制直接對外抓取 Web 資料, 強制使用 Proxy 代理。在多台 Proxy 聯合運作下, 資料重複使用量相當高。
硬體周邊的選擇 • 選擇重點 • 建議使用 SCSI 硬碟,可以加快 IO 效率與減低 CPU 的負載情況。 • 單顆硬碟容量不要太大,建議 4GB ~ 8GB, 最好是 7600 轉速 or 以上。 • 分割區劃分,建議 2GB ~ 4GB • 記憶體提供上,盡可能多一點記憶體。建議要有 128MB 以上。專業多半被要有 512MB RAM。 • CPU 越快越好,多顆 CPU 在程式是使用 MultiThread 架構運作下效能會有大幅度提升。
Proxy 種類與選擇 (1) • 目前常見的 Proxy Server • Squid • Apache • CERN • Netscape Proxy Server • MSP (Microsoft Proxy Server) • 目前世界上最多人使用的是 Squid Proxy,Hinet、 Seednet 等等各大 ISP 都使用該 Proxy Server 提供 使用者 Proxy 使用。
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 等相關服務請求。
Squid 安裝與編譯 (1) • 取的 squid 套件 • Download squid : http://www.squid-cache.org/Mirrors/ftp-mirrors.html http://www.squid-cache.org/Mirrors/http-mirrors.html ftp://coda.nctu.edu.tw/WWW/Squid/squid-2/STABLE ftp://ftp.squid-cache.org
Squid 安裝與編譯 (2) • 取的 squid 套件 (Compiling and install ) tar zxvf squid-2.2.STABLE5-src.tar.gz cd 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
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 – 認證程式,使用系統密碼
Squid 安裝與編譯 (4) • RedHat、Mandrake 系統,使用 RPM 安裝 • mount /mnt/cdrom • cd /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 目錄內。
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%)
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
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 : 設定可以連線存取權限
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
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
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”
Squid 程式參數 • squid proxy 程式參數 • 顯示使用說明 • squid –h • -k 控制相關參數,後面接: • reconfigure : 改過設定,使用此參數讓設定生效 • rotate : 截斷 log 紀錄,然後重新建立 log • shutdown : 關閉 squid proxy • kill : 強制關閉 squid proxy • check : 檢查 squid.conf 是否正確 • parse : 解析 squid.conf 檔案內容
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
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
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
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.248 acl sex_ip dst 63.1.0.0/255.255.255.248 acl sex_domain dstdomain www.playbody.com http_access deny sex_ip http_access deny sex_domain http_access allow company_ip
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
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
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.248 acl bad-area2 dst 134.1.65.0/255.255.255.0 cache_peer_access proxy1.xxx.com.tw deny bad-area1 cache_peer_access proxy2.xxx.com.tw deny bad-area2
Squid 進階設定部分 (3) • squid.conf 進階參數設定: • cache_mgr • 定義管理者的 e-mail • cache_mgr squid@proxy.xxxx.com.tw • ftp_user • 定義當透過 proxy 存取 ftp 時,使用的 e-mail • ftp_user squid@proxy.xxx.com.tw • 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
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
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
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
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, 一舉數得。
pwebstats 流量統計程式 (1) • 安裝 pwebstats • 安裝需求: • perl 5.000 or above (使用 perl –v 檢查版本) • fly package download from : http://www.unimelb.edu.au/fly/fly.html ftp://www.unimelb.edu.au/pub/www/tools/unix/fly/ fly-1.6.5.tar.gz • gdchart package download from : • pwebstats ftp://www.unimelb.edu.au/pub/www/tools/unix/ pwebstats/pwebstats-1.3.8.tar.gz
pwebstats 流量統計程式 (2) • 建立 /home/httpd/pwebstats 相關目錄 • mkdir /home/httpd/utility • mkdir /home/httpd/utility/pwebstats • mkdir /home/httpd/utility/fly ( 註: RedHat 7.x,Mandrake 7.2 /home/httpd 搬移到 /var/www 目錄內) • 安裝 fly • tar zxvf fly-1.6.5.tar.gz • cd fly-1.6.5;make;cd .. • cp –R fly-1.6.5 /home/httpd/utility • cd /home/httpd/utility • ln –s fly-1.6.5 fly
pwebstats 流量統計程式 (3) • 安裝 pwebstats • tar zxvf pwebstats-1.3.8.tar.gz • cd pwebstats-1.3.8 • vi pwebstats 檔案第一行檔案,把 #!/usr/local/bin/perl 修正指向到 perl 實際路徑,比方 #!/usr/bin/perl • cd .. ; cp –R pwebstats-1.3.8 /home/httpd/utility • mkdir /home/httpd/utility/pwebstats/logs • 修改 /home/httpd/utility/pwebstats-1.3.8 目錄內 conf/squid-proxy.conf 檔案 • cd /home/httpd/utility • ln –s pwebstats-1.3.8 pwebstats
pwebstats 流量統計程式 (4) • pwebstats 設定檔案 squid-proxy.conf • server:proxy_xxxx • Server_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 統計
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
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
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.daily run-parts 會執行指定參數該目錄內所有 script 檔案。 • /etc/cron.daily 包含 logrotate 檔案,內容是: #!/bin/sh /usr/sbin/logrotate /etc/logrotate.conf
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 處理之。
參考資料 • http://www.squid-cache.org • http://squid-docs.sourceforge.net/latest/html/book1.htm • http://www.squid-cache.org/Doc/FAQ/FAQ.html • http://proxy.ntu.edu.tw • http://proxy.nctu.edu.tw • http://proxy.nsysu.edu.tw • http://www.cc.nsysu.edu.tw/~lmj/Squid.files/frame.htm • http://turtle.ee.ncku.edu.tw/~tung/proxy/