squid完全攻略(一)squid优化后详细安装步骤
Squid工作原理
Squid是Linux下一個緩存Internet數(shù)據(jù)的代理服務(wù)器軟件,它接收用戶的下載申請并自動處理所下載的數(shù)據(jù)。即當(dāng)一個用戶下載www.idcshare的頁面,他請求squid為他取得這個頁面,suqid會連接到申請www.idcshare的網(wǎng)站并請求該主頁,接著把主頁傳給用戶并自己保留一份.當(dāng)下一次有其他用戶請求同樣頁面時,squid會從磁盤中讀到保存那份,這樣數(shù)據(jù)就會迅速傳輸?shù)接脩舻臋C器上.
Squid支持以下協(xié)議
1) 客戶端協(xié)議:HTTP .FTP.Gopher.WAIS.SSL
2) 緩存及管理協(xié)議:ICP(Internet Cache協(xié)議). HTCP(超文本緩沖協(xié)議).SNNP. Cache Digests
運行流程圖:
1) Client端向Server端發(fā)送一個資料需求封包;
2) Server端接收之后,先比對這個封包的『來源』與預(yù)計要前往的『目標(biāo)』網(wǎng)站是否為可接受?如果來源與目標(biāo)都是合法的,或者說,來源與目標(biāo)網(wǎng)站我們的Proxy都能幫忙取得資料時,那么Server端會預(yù)計開始替Client取得資料。
3) Server首先會到自己的硬碟里面,也就是所謂的cache 查看一下有沒有Client端所需要的資料,如果有的話,那就將資料直接送到Client端而不經(jīng)過向Internet要求資料的程序;
4) 將數(shù)據(jù)傳回給客戶端使用
5)在經(jīng)過1, 2, 3查尋知道cache沒有資料,或者資料過期之后, Proxy會向Internet上面的目標(biāo)網(wǎng)站要求資料;
6)在將資料取回之后, proxy會先將取得的資料『儲存一份到cache當(dāng)中』;
7)最后才將資料傳回給Client端使用。
代理服務(wù)器優(yōu)點與類型
優(yōu)點:
1) 通過緩存增加訪問速度
2) 提供用私有ip訪問internet的方法
3) 提高網(wǎng)絡(luò)的安全性
4) 方便對用戶的管理
類型:
1) 標(biāo)準(zhǔn)代理服務(wù)器
2) 透明代理服務(wù)器
3) 反向代理服務(wù)器
安裝前期準(zhǔn)備工作
1)源代碼編譯優(yōu)化
一般情況下,優(yōu)化參數(shù)都是通過export命令設(shè)定CFLAGS和CXXFLAGS,然后configure的時候會自動讀入,make的時候會自動使用所選的優(yōu)化參數(shù)。通過添加優(yōu)化參數(shù),讓程序針對某個CPU型號和某個系統(tǒng)進行最大優(yōu)化,減少文件大小、CPU使用率、內(nèi)存使用率。優(yōu)化后的程序,將不再具有跨系統(tǒng)跨平臺的能力。
首先查看CPU信息,然后根據(jù)CPU信息到http://gentoo-wiki.com/Safe_Cflags去獲取CPU優(yōu)化參數(shù)
技巧:直接是打不開http://gentoo-wiki.com/Safe_Cflagshttp://www.proxyie.cn/輸入http://gentoo-wiki.com,打開后點左側(cè)欄的搜索,輸入cpu。,要×××,打開
如下圖:
[root@server soft]# cat /proc/cpuinfo |more
processor?????? : 0
vendor_id?????? : GenuineIntel
cpu family????? : 6
model?????????? : 23
model name????? : Intel(R) Xeon(R) CPU?????????? E5420 @ 2.50GHz
stepping??????? : 10
cpu MHz???????? : 2493.978
cache size????? : 6144 KB
physical id???? : 0
siblings??????? : 4
core id???????? : 0
cpu cores?????? : 4
apicid????????? : 0
fdiv_bug??????? : no
hlt_bug???????? : no
f00f_bug??????? : no
coma_bug??????? : no
fpu???????????? : yes
fpu_exception?? : yes
cpuid level???? : 13
wp????????????? : yes
flags?????????? : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush d
ts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc pni monitor ds_cpl vmx est tm2 cx16 xtpr
lahf_lm
bogomips??????? : 4987.95
由于服務(wù)器model name 是Intel(R) Xeon(R) CPU,我選擇如下圖:
配置:
[root@server soft]#export CHOST="i686-pc-linux-gnu"
[root@server soft]#export CFLAGS="-march=Prescott -O2 -pipe -fomit-frame-pointer"
[root@server soft]#export CXXFLAGS="${CFLAGS}"
[root@server soft]#env
查看設(shè)置環(huán)境變量是否生效
紅色標(biāo)記,說明生效了.
3)修改描述符參數(shù)值
文件描述符用來限制一個程序能同時打開的文件數(shù)目,默認(rèn)是1024。也就是說,如果不修改文件描述符,那么一個程序只能同時打開1024個文件,而Squid會同時打開成千上萬個文件,所以遠(yuǎn)遠(yuǎn)是不夠的,為了保證最高的效率和響應(yīng)速度,要更改文件描述符的大小.
兩種情況:a.用戶每次訪問squid,squid會按照需要打開文件,然后讀取文件內(nèi)容再返回給用戶。
b.squid預(yù)先打開訪問頻率高的文件,用戶訪問squid,squid直接把內(nèi)容返回給用戶。相對來說,后面的方法可以對用戶的請求進行更加快的反應(yīng)。
首先修改以下兩個文件。
/usr/include/bits/typesizes.h
/usr/include/linux/posix_types.h
用vi打開上面的文件查找
#define __FD_SETSIZE 1024
把1024改為65536,然后保存,這是Linux能接受的最大數(shù)值。
剛才編輯的這兩個文件是C/C++程序里面的頭文件,編譯squid的時候會被自動引用。除了這兩個文件以外,我們還需要對當(dāng)前環(huán)境進行設(shè)置。也就是你用ssh登錄到系統(tǒng)時的一些設(shè)定。每個登錄進程都可以進行單獨的設(shè)置,當(dāng)關(guān)閉目前登錄回話,環(huán)境變量的設(shè)置就失效了.
ulimit用來設(shè)置當(dāng)前環(huán)境的一些資源限制。這個命令是對環(huán)境進行設(shè)置,所以退出當(dāng)前登錄進程后命令將會失效。
我們輸入下面的命令
[root@server soft]#ulimit -Hs 65536 [root@server soft]#ulimit -n 65536 也可以永久修改這些限制,開機自動生效,配置如下: vi /etc/security/limits.conf,添加以下內(nèi)容 *??????????????? soft?? nofile????????? 65536 *??????????????? hard?? nofile????????? 65536
H參數(shù)是硬性限制,s是堆棧上限,n是文件描述符上限。
注釋:
ulimit 參數(shù)介紹:
-H 設(shè)置硬件資源限制.
-S 設(shè)置軟件資源限制.
-a 顯示當(dāng)前所有的資源限制.
-c size:設(shè)置core文件的最大值.單位:blocks
-d size:設(shè)置數(shù)據(jù)段的最大值.單位:kbytes
-f size:設(shè)置創(chuàng)建文件的最大值.單位:blocks
-l size:設(shè)置在內(nèi)存中鎖定進程的最大值.單位:kbytes
-m size:設(shè)置可以使用的常駐內(nèi)存的最大值.單位:kbytes
-n size:設(shè)置內(nèi)核可以同時打開的文件描述符的最大值.單位:n
-p size:設(shè)置管道緩沖區(qū)的最大值.單位:kbytes
-s size:設(shè)置堆棧的最大值.單位:kbytes
-t size:設(shè)置CPU使用時間的最大上限.單位:seconds
-v size:設(shè)置虛擬內(nèi)存的最大值.單位:kbytes
安裝與配置
安裝
[root@server soft]# wget http://www.squid-cache.org/Versions/v3/3.0/squid-3.0.STABLE24.tar.gz
[root@server soft]# tar -zxvf squid-3.0.STABLE24.tar.gz
[root@server soft]# cd squid-3.0.STABLE24
[root@serversquid-3.0.STABLE24]#./configure --prefix=/usr/local/squid\--enable-gnuregex\--enable-icmp\--enable-linux-netfilter\--enable-default-err-language="Simplify_Chinese"\--enable-kill-parent-hack\--enable-cache-digests\--enable-dlmalloc\--enable-poll\--enable-async-io=240\--enable-delay-pools\--with-filedescriptors=65536\--enable-snmp\--enable-arp-acl\--mandir=/usr/share/man/--with-large-files
注釋:
可以用命令查看
[root@viong squid-3.0.STABLE24]# ./configure --help
--enable-gnuregex
在訪問控制列表和其他配置指令里,squid使用正則表達式作為匹配機制。GNU的正則表達式庫包含在squid的源代碼包里;它可以在沒有內(nèi)建正則表達式的操作系統(tǒng)中使用。./configure腳本偵察你系統(tǒng)中的正則表達式庫,假如必要,它可以激活使用GNU正則表達式。如果因為某些理由,你想強制使用GNU正則表達式,比如在proxy未來的規(guī)劃當(dāng)中。可能利用到正則表示法的方式來抵擋一些惡意的網(wǎng)站,你可以將這個選項加到./configure命令后.
--enable-icmp
squid能利用ICMP消息來確定回環(huán)時間尺寸,非常象ping程序。你能使用該選項來激活這些功能。
--enable-linux-netfilter
Netfilter是linux 2.4系列內(nèi)核的包過濾器名字。假如你想在linux2.4或以后的版本中使用HTTP攔截功能,那么激活該選項。
--enable-default-err-language=lang
該選項設(shè)置error_directory指令的默認(rèn)值。例如,假如你想使用荷蘭語,你能這樣指定:
% ./configure --enable-default-err-language=Dutch
你也能在squid.conf里指定error_directory指令,在附錄A中有描述。假如你忽略該選項,英語是默認(rèn)錯誤語言。
--enable-kill-parent-hack
在我們關(guān)掉squid的時候,連同parent process 一起關(guān)掉
--enable-cache-digests
Cache消化是ICP的另一個替代,但有著截然不同的特性
--enable-dlmalloc[=LIB]
在一些系統(tǒng)上,內(nèi)建的內(nèi)存分配機制(malloc)在使用squid時表現(xiàn)不盡人意。使用--enable-dlmalloc選項將squid源代碼包中的dlmalloc包編譯和鏈接進來。假如你的系統(tǒng)中已安裝dlmalloc,你能使用=LIB參數(shù)指定庫的路徑。
--enable-poll
unix提供兩個相似的函數(shù)用以在I/O事件里掃描開放文件描述符:select()和poll()../configure腳本通常能非常好的計算出何時使用poll()來代替select().假如你想強制使用poll(),那么指定該選項。
--enable-async-io[=N_THREADS]
異步I/O是squid技術(shù)之一,用以提升存儲性能。aufs模塊使用大量的線程來執(zhí)行磁盤I/O操作。該代碼僅僅工作在linux和solaris系統(tǒng)中。=N_THREADS參數(shù)改變squid使用的線程數(shù)量。如果網(wǎng)站的服務(wù)器配置很高,可以嘗試將這個數(shù)字改為160以上。如果是小網(wǎng)站的話,可以考慮降低到40左右.
請注意--enable-async-io是打開其他三個./configure選項的快捷方式,它等同于:
--with-aufs-threads=N_THREADS
--with-pthreads
--enable-storeio=ufs,aufs
--enable-delay-pools
延時池是squid用于傳輸形狀或帶寬限制的技術(shù)。該池由大量的客戶端IP地址組成。當(dāng)來自這些客戶端的請求處于cache丟失狀態(tài),他們的響應(yīng)可能被人工
--with-filedescriptors=65536
支持最大文件描述符
--enable-snmp
簡單網(wǎng)絡(luò)管理協(xié)議(SNMP)是監(jiān)視網(wǎng)絡(luò)設(shè)備和服務(wù)器的流行方法。該選項導(dǎo)致編譯過程去編譯所有的SNMP相關(guān)的代碼,包括一個裁切版本的CMU SNMP庫。
--enable-arp-acl
允許基于MAC地址的存取過濾
--mandir=/usr/share/man
指定man的安裝目錄
--with-large-files
讓log支持大于2G
[root@server squid-3.0.STABLE24]# make; make install
make[3]: Leaving directory `/root/soft/squid-3.0.STABLE24/tools'
make[2]: Leaving directory `/root/soft/squid-3.0.STABLE24/tools'
make[1]: Leaving directory `/root/soft/squid-3.0.STABLE24/tools'
make[1]: Entering directory `/root/soft/squid-3.0.STABLE24'
Build Successful.
make[2]: Entering directory `/root/soft/squid-3.0.STABLE24'
make[2]: Nothing to be done for `install-exec-am'.==================說明安裝成功
make[2]: Nothing to be done for `install-data-am'.
make[2]: Leaving directory `/root/soft/squid-3.0.STABLE24'
make[1]: Leaving directory `/root/soft/squid-3.0.STABLE24'
[root@server squid-3.0.STABLE24]#????????
[root@server squid-3.0.STABLE24]# ll /usr/local/squid--enable-gnuregex/
總計 24
drwxr-xr-x 2 root root 4096 03-04 23:35 bin
drwxr-xr-x 2 root root 4096 03-04 23:35 etc
drwxr-xr-x 2 root root 4096 03-04 23:35 libexec
drwxr-xr-x 2 root root 4096 03-04 23:35 sbin
drwxr-xr-x 5 root root 4096 03-04 23:35 share
drwxr-xr-x 3 root root 4096 03-04 23:35 var
bin/ :放置主要的squid執(zhí)行scripts的目錄,重要的是RunCache那個檔案;
etc/ :幾乎所有的squid設(shè)定檔都在這里;
libexec/ :一些函式庫;
sbin/ :重要的就是那個squid的執(zhí)行檔!
share/ :一些錯誤訊息代碼表示檔案,以及一些小圖示放置的目錄;
var/ :預(yù)設(shè)是放置log file的,不過我不喜歡放在這里,這點等一下我們會修改的!
第一次運行squid時,要先設(shè)置/usr/local/squid/var權(quán)限,還有在/usr/local/squid/etc/squid.conf添加主機,visible_hostnam viong 并制定http_port 端口號.這里預(yù)設(shè)個80
[root@server squid-3.0.STABLE24]# chown -R nobody:nobody /usr/local/squid/var
[root@server squid-3.0.STABLE24]# /usr/local/squid/sbin/squid –zD
首次運行squid需要初始化cache,由于squid要驗證其DNS可用才能啟動,目前都沒配置,所以我先禁用初始化DNS測試,先讓squid啟動
Squid[root@viong soft]# netstat -ntpl |grep 80
tcp??????? 0????? 0 0.0.0.0:80????????????????? 0.0.0.0:*?????????????????? LISTEN????
[root@server squid-3.0.STABLE24]# /usr/local/squid/bin/squidclient -p 80 -h localhost mgr:info
取得squid運行狀態(tài)信息
HTTP/1.0 200 OK
Server: squid/3.0.STABLE24
Mime-Version: 1.0
Date: Fri, 05 Mar 2010 14:43:26 GMT
Content-Type: text/plain
Expires: Fri, 05 Mar 2010 14:43:26 GMT
Last-Modified: Fri, 05 Mar 2010 14:43:26 GMT
X-Cache: MISS from viong
Via: 1.0 viong (squid/3.0.STABLE24)
Proxy-Connection: close
Squid Object Cache: Version 3.0.STABLE24
Start Time:???? Fri, 05 Mar 2010 14:42:48 GMT
Current Time:?? Fri, 05 Mar 2010 14:43:26 GMT
Connection information for squid:
??????? Number of clients accessing cache:????? 1
??????? Number of HTTP requests received:?????? 0
??????? Number of ICP messages received:??????? 0
??????? Number of ICP messages sent:??? 0
??????? Number of queued ICP replies:?? 0
??????? Number of HTCP messages received:?????? 0
??????? Number of HTCP messages sent:?? 0
??????? Request failure ratio:?? 0.00
??????? Average HTTP requests per minute since start:?? 0.0
??????? Average ICP messages per minute since start:??? 0.0
??????? Select loop called: 8659 times, 4.376 ms avg
Cache information for squid:
??????? Hits as % of all requests:????? 5min: 0.0%, 60min: 0.0%
??????? Hits as % of bytes sent:??????? 5min: -0.0%, 60min: -0.0%
??????? Memory hits as % of hit requests:?????? 5min: 0.0%, 60min: 0.0%
??????? Disk hits as % of hit requests: 5min: 0.0%, 60min: 0.0%
??????? Storage Swap size:????? 0 KB
??????? Storage Swap capacity:?? 0.0% used, 100.0% free
??????? Storage Mem size:?????? 104 KB
??????? Storage Mem capacity:??? 1.3% used, 98.7% free
??????? Mean Object Size:?????? 0.00 KB
??????? Requests given to unlinkd:????? 0
Median Service Times (seconds) 5 min??? 60 min:
??????? HTTP Requests (All):?? 0.00000 0.00000
??????? Cache Misses:????????? 0.00000 0.00000
??????? Cache Hits:??????????? 0.00000 0.00000
??????? Near Hits:???????????? 0.00000 0.00000
??????? Not-Modified Replies: 0.00000 0.00000
??????? DNS Lookups:?????????? 0.00000 0.00000
??????? ICP Queries:?????????? 0.00000 0.00000
Resource usage for squid:
??????? UP Time:??????? 37.890 seconds
??????? CPU Time:?????? 0.070 seconds
??????? CPU Usage:????? 0.18%
??????? CPU Usage, 5 minute avg:??????? 0.00%
??????? CPU Usage, 60 minute avg:?????? 0.00%
??????? Process Data Segment Size via sbrk(): 2784 KB
??????? Maximum Resident Size: 0 KB
??????? Page faults with physical i/o: 0
Memory usage for squid via mallinfo():
??????? Total space in arena:??? 3052 KB
??????? Ordinary blocks:???????? 2938 KB????? 1 blks
??????? Small blocks:?????????????? 0 KB????? 0 blks
??????? Holding blocks:????????? 1520 KB????? 8 blks
??????? Free Small blocks:????????? 0 KB
??????? Free Ordinary blocks:???? 113 KB
??????? Total in use:??????????? 4458 KB 98%
??????? Total free:?????????????? 113 KB 2%
??????? Total size:????????????? 4572 KB
Memory accounted for:
??????? Total accounted:???????? 1886 KB 41%
??????? memPool accounted:?????? 1886 KB 41%
??????? memPool unaccounted:???? 2685 KB 59%
??????? memPoolAlloc calls:????? 1504
??????? memPoolFree calls:??????? 481
File descriptor usage for squid:
??????? Maximum number of file descriptors: 65536? =============文件描述符
??????? Largest file desc currently in use:???? 16
??????? Number of file desc currently in use:?? 10
??????? Files queued for open:?????????????????? 0
??????? Available number of file descriptors: 1014
??????? Reserved number of file descriptors:?? 100
??????? Store Disk files open:?????????????????? 0
Internal Data Structures:
??????????? 26 StoreEntries
??????????? 26 StoreEntries with MemObjects
??????????? 25 Hot Object Cache Items
???????????? 0 on-disk objects
注釋:
Squidclient參數(shù):
取得squid運行狀態(tài)信息: squidclient -p 80 mgr:info
取得squid內(nèi)存使用情況: squidclient -p 80 mgr:mem
取得squid已經(jīng)緩存的列表: squidclient -p 80 mgr:objects
取得squid的磁盤使用情況: squidclient -p 80 mgr:diskd
為了不每次輸ulimit -HSn 65536 ,squid指定個啟動腳本,復(fù)制一下腳本保存為squid.sh
#!/bin/sh
#
ulimit -HSn 65536
# starts and stops Squid
case "$1" in
start)
????????? /usr/local/squid/sbin/squid -s
????????? echo -n ' Squid'
????????? ;;
stop)
????????? /usr/local/squid/sbin/squid -k shutdown
????????? ;;
esac
以后啟動或者停止squid,直接運行一下命令
[root@viong soft]# sh squid.sh stop
[root@viong soft]# sh squid.sh start??
本文出自 “漫游Network-->成就Dream” 博客,請務(wù)必保留此出處http://viong.blog.51cto.com/844766/280978
本文出自 51CTO.COM技術(shù)博客
轉(zhuǎn)載于:https://blog.51cto.com/lzy821218/307355
總結(jié)
以上是生活随笔為你收集整理的squid完全攻略(一)squid优化后详细安装步骤的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: u-boot-1.3.4 for dm3
- 下一篇: 网站被黑