Squid是比較知名的代理軟件,它不僅可以跑在linux上還可以跑在windows以及Unix上,它的技術(shù)已經(jīng)非常成熟。目前使用Squid的用戶也是十分廣泛的。Squid與Linux下其它的代理軟件如Apache、Socks、TIS FWTK和delegate相比,下載安裝簡(jiǎn)單,配置簡(jiǎn)單靈活,支持緩存和多種協(xié)議。 Squid的緩存功能相當(dāng)好用,不僅可以減少帶寬的占用,同樣也大大降低了后臺(tái)的WEB服務(wù)器的磁盤I/O的壓力。Squid接收用戶的下載申請(qǐng),并自動(dòng)處理所下載的數(shù)據(jù)。也就是說(shuō),當(dāng)一個(gè)用戶象要下載一個(gè)主頁(yè)時(shí),它向Squid發(fā)出一個(gè)申請(qǐng),要Squid替它下載,然后Squid 連接所申請(qǐng)網(wǎng)站并請(qǐng)求該主頁(yè),接著把該主頁(yè)傳給用戶同時(shí)保留一個(gè)備份,當(dāng)別的用戶申請(qǐng)同樣的頁(yè)面時(shí),Squid把保存的備份立即傳給用戶,使用戶覺(jué)得速度相當(dāng)快。 Squid將數(shù)據(jù)元緩存在內(nèi)存中,同時(shí)也緩存DNS查尋的結(jié)果,除此之外,它還支持非模塊化的DNS查詢,對(duì)失敗的請(qǐng)求進(jìn)行消極緩存。Squid支持SSL,支持訪問(wèn)控制。由于使用了ICP,Squid能夠?qū)崿F(xiàn)重疊的代理陣列,從而最大限度的節(jié)約帶寬。 Squid對(duì)硬件的要求是內(nèi)存一定要大,不應(yīng)小于128M,硬盤轉(zhuǎn)速越快越好,最好使用服務(wù)器專用SCSI 硬盤,處理器要求不高,400MH以上既可。
wget http://www.squid-cache.org/Versions/v2/2.6/squid-2.6.STABLE20.tar.gz
tar zxvf squid-2.6.STABLE20.tar.gz
cd squid-2.6.STABLE20
ulimit -HSn 65535
useradd squid
編譯參數(shù)
./configure --prefix=/usr/local/squid \
--disable-dependency-tracking \
--enable-dlmalloc \
--enable-gnuregex \
--disable-carp \
--enable-async-io=240 \
--with-pthreads \
--enable-storeio=ufs,aufs,diskd,null \
--disable-wccp \
--disable-wccpv2 \
--enable-kill-parent-hack \
--enable-cachemgr-hostname=localhost \
--enable-default-err-language=Simplify_Chinese \
--with-build-environment=POSIX_V6_ILP32_OFFBIG \
--with-maxfd=65535 \
--with-aio \
--disable-poll \
--enable-epoll \
--enable-linux-netfilter \
--enable-large-cache-files \
--disable-ident-lookups \
--enable-default-hostsfile=/etc/hosts \
--with-dl \
--with-large-files \
--enable-removal-policies=heap,lru \
--enable-delay-pools \
--enable-snmp \
--disable-internal-dns
make && make install
關(guān)于squid的版本,有必要提一下,目前squid最新版本已經(jīng)到了3.1了,但是筆者認(rèn)為2.6版本比較好用,如果你有興趣可以研究一下3.1。
編輯配置文件 /usr/local/squid/etc/squid.conf
把原來(lái)配置文件刪除,替換成:
http_port 80 transparent
cache_replacement_policy lru #如果有多個(gè)(下面兩行)緩存目錄,則需要寫這個(gè)參數(shù)
cache_dir aufs /cache1 8192 16 256 #緩存目錄1 /cache1 大小為8G
cache_dir aufs /cache2 4096 16 256 #緩存目錄2 /cache2 大小為4G
上面兩行定義了緩存目錄,這個(gè)緩存目錄可以只有一個(gè),也可以定義很多個(gè)。
cache_mem 2048 MB #分配多少內(nèi)存給squid,建議留至少512M給系統(tǒng),如果你是虛擬機(jī)內(nèi)存很小,只作為試驗(yàn)用的話,那就分一半內(nèi)存給squid
maximum_object_size 2048 KB #緩存的文件最大不能超過(guò)2M
maximum_object_size_in_memory 512 KB #緩存在內(nèi)存中的文件最大不超過(guò)512k
visible_hostname cache.example.com #顯示給用戶的主機(jī)名
client_persistent_connections off #client端關(guān)閉長(zhǎng)連接
server_persistent_connections on #server端打開(kāi)長(zhǎng)連接
memory_pools on
memory_pools_limit 1024 MB
forwarded_for on
log_icp_queries off
cache_mgr cache@example.com #定義管理員的mail為cache@example.com
via on
httpd_suppress_version_string off
cache_effective_user squid #定義以squid用戶的身份運(yùn)行squid
cache_effective_group squid
error_directory /usr/local/squid/share/errors/Simplify_Chinese
icon_directory /usr/local/squid/share/icons
mime_table /usr/local/squid/etc/mime.conf
ie_refresh off
tcp_recv_bufsize 32 KB
acl all src 0.0.0.0/0.0.0.0
acl localhost src 127.0.0.0/8
acl Mgr_ip src 127.0.0.0/8
acl allow_ip dst 127.0.0.0/8 192.168.0.0/16 #定義允許代理的web的IP或者IP段
acl PURGE method PURGE
acl Safe_ports port 80 8080
acl CONNECT method CONNECT
acl manager proto cache_object
acl HTTP proto HTTP
http_access allow allow_ip
http_access allow manager Mgr_ip
http_access deny manager
http_access deny PURGE
http_access deny !Safe_ports
http_access deny all
icp_access deny all
ipcache_size 1024
ipcache_low 90
ipcache_high 95
memory_replacement_policy lru
hosts_file /etc/hosts
request_header_max_size 128 KB
hierarchy_stoplist cgi-bin ? \.php \.html
acl QUERY urlpath_regex cgi-bin \? \.php \.html
cache deny QUERY
quick_abort_min -1 KB
quick_abort_max 32 KB
quick_abort_pct 95
# error page
#error_map http://www.92csz.com/404.html 403
#deny_info http://www.92csz.com/error.html cctv_Domain
# timeout
peer_connect_timeout 20 seconds
connect_timeout 20 seconds
read_timeout 60 seconds
request_timeout 20 seconds
pconn_timeout 20 seconds
shutdown_lifetime 5 seconds
strip_query_terms off
icp_port 0
# logfile
emulate_httpd_log on
logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh
#access_log /log/squid-log/access.log combined
cache_store_log /dev/null
cache_log /var/log/squid/cache.log
logfile_rotate 12
# MISCELLANEOUS
store_objects_per_bucket 15
client_db off
修改完配置文件后保存,然后初始化squid
mkdir /cache1 /cache2 /var/log/squid
chown -R squid:squid /cache1 /cache2 /var/log/squid
/usr/local/squid/sbin/squid -z
用來(lái)生成cache目錄,如果你的配置文件配置出錯(cuò),往往會(huì)在初始化的時(shí)候報(bào)錯(cuò),錯(cuò)誤信息會(huì)直接顯示在屏幕上。初始化成功后,就可以啟動(dòng)squid了,啟動(dòng)命令為:
nohup /usr/local/squid/bin/RunCache &
啟動(dòng)后,可以去看看cache.log 在這個(gè)日志中,你可以看到很多關(guān)于squid的信息,當(dāng)然也包括一些錯(cuò)誤日志。
如果想開(kāi)機(jī)啟動(dòng)則需要在/etc/rc.d/rc.local中最后加入一行
/usr/local/bin/RunCache &
到這里算是配置完成了,但是還有一個(gè)問(wèn)題,就是如何定義被代理的web以及域名?單單看配置文件并沒(méi)有說(shuō)代理的web是哪一個(gè)。確實(shí),這個(gè)配置文件其實(shí)可以代理多臺(tái)web,只要你在/etc/hosts中定義要代理的域名以及IP即可,hosts格式在前面已經(jīng)介紹過(guò)。筆者要提醒你的是,如果是一臺(tái)web上的多個(gè)域名,請(qǐng)不要寫一行,雖然hosts是允許的,但是如果寫成一個(gè)IP對(duì)應(yīng)多個(gè)域名,squid代理時(shí)就會(huì)出錯(cuò)。所以有幾個(gè)域名就要寫幾行。
更改/etc/hosts后要重啟squid才能生效:
/usr/local/squid/sbin/squid -krec
在重啟前可以先檢測(cè)一下,是否有錯(cuò),命令為:
/usr/local/squid/sbin/squid –kcheck
如果沒(méi)有錯(cuò),則不會(huì)顯示任何信息,否則會(huì)顯示一些信息出來(lái)。
更多建議: