Squid 緩存服務css
一、緩存服務器結束前端
緩存服務器(cache server),即用來存儲(介質爲內存及硬盤)用戶訪問的網頁、圖片、文件等等信息的專用服務器,這種服務器不只可使用戶能夠最快的獲得他們想要的信息,並且能夠大大減小服務端網絡傳輸的數據量,緩存服務器每每也是代理服務器,對於網站的用戶來講,緩存服務器和代理是不可見的,即在用戶看來全部的網站信息都是來自其正在訪問的網站,而實際上多是緩存服務器在提供訪問數據mysql
目前國內互聯網公司經常使用的緩存服務器有:squid、varnish、nginx、ats。linux
第一次用戶訪問網站,先到squiq,可是squid沒有數據,因此sqquid代理用戶去服務端訪問,以後數據也會保留在squid,因此下次用戶訪問的時候,能夠直接從squid中得到,而沒必要再到服務器中取得數據。nginx
官網:http://www.squid-cache.org/web
二、web緩存的相關概念算法
a、cache命中sql
cache命中是在cache server每次從它的緩存裏知足客戶端http請求發送。cache命中率,是全部客戶端HTTP請求中的命中比例,web緩存典型的cache命中率在30%到60%之間,另外一個類似的度量單位叫作字節命中率,描述了cache提供服務的數據容量。數據庫
提供cache命中率的方法:apache
一、nginx/apache expries,cache-control緩存頭
二、動靜分離,靜態化,對靜態走CDN
三、設置數據庫的cache等,如設置mysql 的cache,讓緩存靠前
四、4XX/5XX之類的錯誤頁面,死鏈沒法緩存。
五、動態不走緩存
b、cache丟失
cache丟失在cache server不能從它的緩存裏面知足客戶端HTTP請求時發生,cache丟失的緣由有不少種:
一、當cache server第一次接受到對第一個新資源的請求時,就會產生一個cache丟失,如何解決第一次命中?
先預熱或者預取
a、內部先請求訪問,能夠經過腳本實現(可是這個思想不太靠譜)。
b、後端生成數據以後,統一推到前端cache server,即預取、預熱
二、存儲空間慢,或者對象自身過時,cache server會清除這些緩存對象,以釋放空間給新對象
若是解決問題2:
a、增大內存或者磁盤。
b、增大對象緩存時間
c、參數設置,緩存參數設置打一下,最大緩存對象2M(想辦法多緩存熱門的數據)
d、分資源緩存,如1M/10M/100M不一樣大小的分開緩存。(分拆服務器,acl正則匹配拋給不一樣的pools)
三、還有多是客戶訪問的資源不可到達,原始服務器指示cache server怎樣處理用戶響應,如:他會提示數據不能被緩存,或在有限的時間內才被重複使用等等。
a、
c、cache確認
cache確認保證cache server不對訪問的用戶返回過時的數據,在重複使用緩存對象時,cache server須要常常從原始服務器確認它,假如服務器指示squid的拷貝仍舊有效,數據就發送出去,不然,squid更新它的存儲拷貝,而且轉發給客戶
當用戶更新了數據到數據庫或者存儲服務器的時候,能夠從業務角度主動調用接口清楚該對象緩存的指令。
對應緩存來說,數據的一致性是一個特別頭疼的問題,特別是memcached。
CDN刪除數據通常須要5-15分鐘才能刪除完成。
圖片放到CDN通常是不要更新了,圖片修改算更新,這種業務就要推送。
刪除圖片再上傳,這種不算修改,而是一個新的資源。總是數據,CDN會有相應的算法,在必定的時間內自動刪除。
網站改版:在CDN上推送js、css(更名推送)等程序。
三、squid服務介紹
squid是一個高性能的代理和緩存服務器,squid支持FTP/gopher和HTTP協議,和通常的代理緩存軟件不一樣,squid用一個單獨的,非模塊化的、I/O驅動的進程來處理全部的客戶端請求。(gopher是internet上一個很是有名的信息查詢系統,它將Internet上的文件組織成某種索引,很方便地將用戶從Internet的一處帶到另一處,在WWW出現以前,GopherSHI Internet上主要的信息檢索工具)
Squid將數據元緩存到內存或硬盤中,同時也緩存DNS查詢結果,Squid只是SSL,支持訪問控制,因爲使用ICP(輕量Internet緩存協議),Squid可以實現層疊的代理陣列,從而最大限度的節約帶寬
Squid Cache簡稱Squid,是一個流行的代理服務器和WEB緩存服務器軟件,Squid服務有至關多的用途。
一、用於放置在WEB服務器的前面,緩存網站WEB服務器的相關數據。這樣用戶請求緩存服務器就能夠直接返回數據給用戶了,從而提高了用戶的訪問網站體驗,從另一方面也減輕了web服務器、數據服務器、圖片文件存儲服務器等業務服務器的壓力。這種應用被稱之爲反向代理服務
二、用於放置在企業內部關鍵的出網位置或者某些共享的網絡前端,緩存內部上網用戶的數據,域名系統和其餘網絡搜索數據等,這樣用戶上網請求的數據,就能夠有緩存服務器放回給內部用戶,而不須要上網了,從而使得內部用戶上網更快,更安全,也會大大節約公司的帶寬,這種應用被稱之爲正向代理服務(分爲普通代理或者透明代理)。(如今不多用,5年前,帶寬很小的時候用得挺多)
三、經過放在網絡的關鍵位置過濾網絡流量和訪問數據,提高整個網絡安全,例如:能夠監控及限制內部企業員工的上網行爲,能夠和iptables配合做爲辦公網的網關。
四、用做局域網經過代理上網
只要說是一臺就能夠上網的機器就能夠,位置隨便,讓全部的用戶的瀏覽器設置這個服務器代理上網便可。
Squid代理服務器主要用於類UNIX系統中運行,其發展歷史相對悠久,功能也相對完善,除了對HTTP支持很好外,對於FTP和HTTPS的支持也很好,在3.0測試版中也支持了IPV6,Squid的主頁在http://www.squid-cache.org/。目前業界主流的CDN都基本是Squid進行二次開發做爲cache緩存服務器的
四、三種代理服務器的原理
普通代理(傳統代理)
傳統的代理服務器就是經過瀏覽器設置代理的方法。
用戶訪問網站,先到squid代理服務器,若是squid代理服務器有數據,那麼squid就直接從緩存中放回給用戶,若是squid緩存中沒有,那麼squid就代替用戶去訪問網站,把數據返回的給用戶的同時留一份到緩存中,一遍下次用戶(或者其餘用戶)訪問的時候,直接從緩存中返回給用戶
透明代理:
所謂的透明代理,是相對於普通代理服務而言,客戶端不須要作任何和代理服務器相關的設置,對用戶而言,感受不到代理服務器的存在,因此稱之爲透明代理,即把代理服務器部署在覈心的上網出口,當用戶上網瀏覽頁面時,會交給代理服務器向外請求,若是結合iptables能夠實現代理+網關+內容過濾+流量安全控制等完整的上網解決方案。
透明代理流程說明:
用戶A發送一個訪問請求到防火牆,由防火牆將該用戶的訪問請求轉發給Squid,squid再先檢查自身緩存中有無該用戶請求的訪問內容,若是沒有,則請求遠端目的的服務器,獲取該用戶的訪問內容,再放回給用戶的同時,在自身緩存保留一份記錄以備下次調用,當用戶B發送一個和用戶A相同的請求到防火牆時,有防火牆轉發該用戶請求到squid,squid檢查到自身緩存發現有相同的內容,直接將該內容放回給用戶B。而無需再次去訪問請求的服務器。
普通代理和透明代理的區別就是,普通代理須要在客戶端瀏覽器設置指定代理服務器,而透明代理不須要。
反向代理服務原理:
普通代理方式是代理內部網絡用戶請求internet上服務器的鏈接請求,客戶端必須指定代理服務器,並將原本要直接發送到internet上的服務器的鏈接請求發送給代理服務器處理,
反向代理(Reverse Proxy)方式是指以代理服務器來接受internet上的鏈接請求,而後將請求轉發給內部網絡上的服務器,並將從內部服務器上獲得的結果返回給internet上請求鏈接的客戶端,此時代理服務器對外表現爲一個服務器。
反向代理流程說明:
squid作反向代理服務器,一般工做在一個服務器集羣的前端,在用戶看來,squid服務器就是他所要訪問的服務器,而實際意義上,squid只是接受用戶的請求,同時將用戶請求轉發給內部真正的WEB服務器,若是squid自己有用戶要訪問的內容,則squid直接將數據返回給用戶,起到了緩存數據的做用,減小了後端服務器的壓力
三種代理的區別:
五、何時須要用squid(CDN)
a、想節省帶寬及服務器成本
b、想提升訪問速度,提高用戶體驗
c、源站扛不住了
六、haproxy和squid代理的區別
有了haproxy,後面還須要squid代理嗎?
squid緩存及代理功能,通常用squid是用來做爲緩存服務器
haproxy作動態及靜態的代理,及負載均衡
squid分拆動態和靜態進行緩存。
常見架構:
動態數據======>靜態化=====>CDN
不少CDN如今逐漸開始支持動態加速業務。
七、如何選擇squid服務的版本
squid2.5:有點老,不支持epoll,內存管理也不完善,例如可能經常會引發從新
squid2.6-2.7:建議使用,性能不錯,2.7很建議使用,覺得基本有了3.0的因此特性
squid3.0:不建議使用,由於使用C++所有重寫,如今性能不如2.6和2.7,之後觀察
八、部署squid須要的硬件環境
a、第一重要資源:內存
squid對硬件的要求最主要就是內存資源,內存資源短缺會嚴重影響性能,由於全部的對象都會盡量的被緩存到內存中,這樣才能更快提高用戶的響應及返回數據
b、第二資源:磁盤
磁盤空間也是另一個squid可以高效運行的重要因素,更多的磁盤空間意外着更多的緩存目標和更高的命中率,快速的磁盤介質也是頗有必要的,例如用ssd,sas替代sata磁盤,除了使用raid外,能夠指定多個磁盤路徑緩存
九、squid的編譯和安裝
部署前環境準備
環境 :
squid server IP:10.0.0.4 WEB server IP:10.0.0.3
1.更改yum源: wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo yum install tree -y grep keepcache /etc/yum.conf sed -i 's/keepcache=0/keepcache=1/g' /etc/yum.conf grep keepcache /etc/yum.conf 2.關閉selinux: setenforce 0 #臨時生效 sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config #永久生效 3.關閉防火牆 /etc/init.d/iptables stop chkconfig iptables off chkconfig --list iptables 4.時間同步 /usr/sbin/ntpdate pool.ntp.org echo '#time sync by gao at 2010-2-1'>>/var/spool/cron/root echo '*/10 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1'>>/var/spool/cron/root crontab -l 5.修改主機名 ======================================= hostname Squid cp /etc/hosts /etc/hosts.bak cp /etc/sysconfig/network /etc/sysconfig/network.bak sed -i '$a 10.0.0.4 WEB' /etc/hosts sed -i '$a 10.0.0.3 Squid' /etc/hosts sed -i -e '/HOSTNAME=/d' -e '1aHOSTNAME=WEB' /etc/sysconfig/network logout ======================================= hostname WEB cp /etc/hosts /etc/hosts.bak cp /etc/sysconfig/network /etc/sysconfig/network.bak sed -i '$a 10.0.0.4 WEB' /etc/hosts sed -i '$a 10.0.0.3 Squid' /etc/hosts sed -i -e '/HOSTNAME=/d' -e '1aHOSTNAME=Squid' /etc/sysconfig/network logout ======================================= 6.調整文件描述符 ulimit -Hn 20480 echo "ulimit -Hn 20480" >>/etc/rc.local tail -1 /etc/rc.local ulimit -n echo "* - nofile 20480">>/etc/security/limits.conf tail -2 /etc/security/limits.conf 7.調整臨時端口 cat /proc/sys/net/ipv4/ip_local_port_range echo "net.ipv4.ip_local_port_range = 4000 65000">>/etc/sysctl.conf sysctl -p|grep port_range
###記得必定要作時間同步
##開始編譯安裝Squid
在10.0.0.3操做
建立放置軟件的目錄 mkdir -p /home/lvnian/tools cd /home/lvnian/tools 下載並安裝squid #wget http://www.it.squid-cache.org/Versions/v3/3.0/squid-3.0.STABLE20.tar.gz #wget http://down1.chinaunix.net/distfiles/squid-3.0.STABLE20.tar.gz #http://www.it.squid-cache.org/Versions/v3/3.0 wget http://www.squid-cache.org/Versions/v3/3.0/squid-3.0.STABLE20.tar.gz tar xf squid-3.0.STABLE20.tar.gz cd squid-3.0.STABLE20 ./configure --prefix=/application/squid3.0 \ --enable-asyno-io=100 \ --with-pthreads \ --enable-storeio="aufs,diskd,ufs" \ --enable-removal-policies='heap,lru' \ --enable-icmp \ --enable-delay-pools \ --enable-useragent-log \ --enable-referer-log \ --enable-kill-parent-hack \ --enable-cachemgr-hostname=localhost \ --enable-arp-acl \ --enable-default-err-language=English \ --enable-err-language="Simplify_Chinese" \ --disable-poll \ --disable-wccp \ --disable-ident-lookups \ --disable-internal-dns \ --enable-basic-auth-helpers="NCSA" \ --enable-stacktrace \ --with-filedescriptors=64000 \ --enable-ssl \ --enable-x-accelerator-very \ --disable-snmp \ --with-aio \ --enable-linux-netfilter \ --enable-linux-tproxy ========================================================== make && make install ############################################## make 報錯解決 structs.h:738: error: ISO C++ forbids declaration of ‘SSL_CTX’ with no type structs.h:738: error: expected ‘;’ before ‘*’ token structs.h:1160: error: ISO C++ forbids declaration of ‘SSL_CTX’ with no type structs.h:1160: error: expected ‘;’ before ‘*’ token structs.h:1161: error: ISO C++ forbids declaration of ‘SSL_SESSION’ with no type structs.h:1161: error: expected ‘;’ before ‘*’ token make[1]: *** [cf_gen.o] Error 1 make[1]: Leaving directory `/home/lvnian/tools/squid-3.0.STABLE20/src' make: *** [all-recursive] Error 1 [root@Squid squid-3.0.STABLE20]# 安裝openssl* yum install openssl* -y cd /home/lvnian/tools rm -rf squid-3.0.STABLE20 tar xf squid-3.0.STABLE20.tar.gz cd squid-3.0.STABLE20 ##############################################
#製做軟鏈接
ln -s /application/squid3.0 /application/squid
到這來安裝squid完畢:
squid 目錄結構介紹:
文件名/目錄名功能描述 sbin squid主從程序的目錄,正常只能被root啓動 sbin/squid squid的主程序 bin bin目錄包含對全部用戶可用的程序 bin/RunAccel RunAccel和RunCache幾乎一致,惟一不一樣時他增長了一個命令行參數,告訴squid在哪裏偵聽HTTP請求 bin/RunCache RunCache是一個腳本,你能用它來啓動squid,假如squid死掉,該腳本自動重啓它,除非它檢測到常常的重啓 bin/squidclient squidclient是一個簡單的HTTP客戶端程序,你能用它來測試squid,他也有一些特色的功能,用以對運行的squid進程發起管理請求 libexec libexec目錄包含了輔助程序,有一些命令你不能正常啓動,然而,這程序一般被其餘程序啓動 libexec/unlinkd unlinkd是一個輔助程序,它從cache目錄裏刪除文件 libexec/cachemgr.cgi cachemgr.cgi是squid管理功能的CGI接口,爲了使用它,你須要拷貝該程序到你的web服務的cgi-bin目錄下 libexec/diskd diskd假如你知道了 --enable-storeio=diskd,你才能看到它 libexec/pinger pinger假如你知道了 --enable-icmp,你才能看到它 etc etc包含squid的主配置文件 etc/squid.conf 這是squid的主配置文件 var var目錄包含了不是很重要的和常常變化的文件,這些文件沒必要正常備份他們 var/logs var/logs目錄是squid不一樣日誌文件的默認位置,當你第一次安裝squid時,他是空的,一旦squid開始運行,你能在這裏看到名字爲access.log。cache.log和store.log這樣的文件 var/cache 假如你不在squid.conf文件裏指定,這是默認的cache緩存目錄(cache_dir)
squid.conf 語法介紹
[root@Squid etc]# pwd /application/squid/etc [root@Squid etc]# ls cachemgr.conf cachemgr.conf.default mime.conf mime.conf.default squid.conf squid.conf.default [root@Squid etc]#
上面中以default爲後綴的都是對於配置文件的備份
#看去掉註銷和空行以後的配置文件
[root@Squid etc]# cat squid.conf.default|egrep -v '^#|^$' acl manager proto cache_object acl localhost src 127.0.0.1/32 acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 acl localnet src 10.0.0.0/8 # RFC1918 possible internal network acl localnet src 172.16.0.0/12 # RFC1918 possible internal network acl localnet src 192.168.0.0/16 # RFC1918 possible internal network acl SSL_ports port 443 acl Safe_ports port 80 # http acl Safe_ports port 21 # ftp acl Safe_ports port 443 # https acl Safe_ports port 70 # gopher acl Safe_ports port 210 # wais acl Safe_ports port 1025-65535 # unregistered ports acl Safe_ports port 280 # http-mgmt acl Safe_ports port 488 # gss-http acl Safe_ports port 591 # filemaker acl Safe_ports port 777 # multiling http acl CONNECT method CONNECT http_access allow manager localhost http_access deny manager http_access deny !Safe_ports http_access deny CONNECT !SSL_ports http_access allow localnet http_access deny all icp_access allow localnet icp_access deny all htcp_access allow localnet htcp_access deny all http_port 3128 hierarchy_stoplist cgi-bin ? access_log /application/squid3.0/var/logs/access.log squid refresh_pattern ^ftp: 1440 20% 10080 refresh_pattern ^gopher: 1440 0% 1440 refresh_pattern (cgi-bin|\?) 0 0% 0 refresh_pattern . 0 20% 4320 icp_port 3130 coredump_dir /application/squid3.0/var/cache [root@Squid etc]#
#建立加用戶
useradd squid -s /sbin/nologin -M id squid
#調整配置文件
vi /etc/squid.conf ############################################# 修改下面的用戶爲squid,注意不能是root,root沒法執行 2941 # cache_effective_user nobody 2941 cache_effective_user squid 或者用sed修改 grep "cache_effective_user " squid.conf sed -i "s/# cache_effective_user nobody/cache_effective_user squid/g" squid.conf grep "cache_effective_user " squid.conf ============================================
#添加組
sed -i "/cache_effective_user squid/a cache_effective_group squid" squid.conf grep "cache_effective" squid.conf ============================================ [root@Squid etc]# grep "cache_effective" squid.conf # TAG: cache_effective_user # see also; cache_effective_group cache_effective_user squid cache_effective_group squid # TAG: cache_effective_group [root@Squid etc]# ============================================
Squid日誌文件
squid默認的日誌你是是squid安裝位置下的logs目錄,例如,假如你在./configure中沒有使用--prefix=選項,那麼默認的日誌文件路徑是/usr/local/squid/var/logs,必須確認日誌文件所存在的磁盤位置空間足夠多,在squid寫日誌若是接受到錯誤,它會退出和重啓。該行爲的主要理由應引發你的注意,squid想確認你不會丟失任何重要的日誌信息,特別你的系統被濫用或者被***時。
squid有三個主要的日誌文件:cache.log access.log store.log
cache.log日誌文件
cache.log 包含多種消息,例如Squid的配置信息,性能警告,以及嚴重錯誤,以下是cache.log的輸出樣本,主要的錯誤和異常條件最可能報告在cache.log裏。
剛開始運行squid時,須要密切關注該文件,加入squid拒絕運行,緣由也行會出如今cache.log文件的結尾處,在正常條件下,該文件不會變化很大,加入你以-s選型來運行squid,重要的cache.log消息也會被髮送到你的syslog進程,經過使用cache_log指令,你能夠修改配置文件squid.conf來改變該日誌文件路徑。
轉發cache.log消息到系統日誌
爲了讓squid發送到cache.log消息的拷貝到系統日誌,請使用-s命令選型,僅僅在debug級別0和1的消息被轉發,級別0的消息以syslog級別LOG_WARGING記錄,級別爲1的消息以syslog級別爲LOG_NOTICE記錄,全部消息使用LOCAL4的syslog設備,以下配置syslogd的一個方法,以便這些消息能保存下來
local4.warning/var/log/squid.log
在維護多個squid主機時,使用syslog來記錄cache.log特別方便,能夠配置每一個本機syslog進程,轉發這些消息到中央日誌主機,這樣就能夠在一個地方統一瀏覽全部cache日誌,例如。能夠在/etc/sysconfigd.conf 裏使用以下接口
local4.notice@192.168.1.123
access.log日誌文件
Squid把關於HTTP事務的關鍵信息存在access.log裏,該文件是基於行,也就說每行對應一個客戶端請求,squid記錄客戶端IP(或主機名)、請求URL、響應size、和其餘信息
Squid 在access.log 裏記錄全部HTTP訪問,除了那些在尚未發送數據前就斷開的鏈接,squid也記錄全部ICP事務,除非你使用log_icp_querise指令關閉了這個功能,
store.log日誌文件
store.log記錄Squid關於存儲或刪除cache目標的決定,對每一個存在cache裏的目標,每一個不可cache的目標,已經每一個被輪換策略刪除的目標,Squid都會建立相關的日誌條目,該日誌文件內容包含了內存cache,又包含了磁盤cache。
squid的日誌文件增長沒有限制,爲了保證日誌文件大小合理,應建立計劃規律的重命名和打包日誌,squid有內建的日誌回滾功能,也能夠避免單個日誌過濾。
squid的訪問控制
理解squid如何搜索ACL元素去匹配是很重要的,當ACL元素有多個值時,任何單個值都能致使匹配,換句話說,squid在檢查ACL元素值時使用OR邏輯,當squid找到第一個值匹配時,他中止搜索,這意味着把最可能匹配的值放在列表的開頭出,能減小延時。
重點強調:
a、squid在搜索ACL元素時使用或邏輯,在acl裏的任何單值均可以致使匹配。
b、而應用訪問規則剛好相反,對http_access和其餘規則設置,squid使用與邏輯。
squid默認的配置文件拒絕每個客戶請求,在任何人能使用代理以前,你必須在squid.conf文件里加入附加的訪問控制規則,最簡單的一個方式,就是定義一個針對客戶IP地址的ACL和一個訪問規則,告訴Squid運行來自這些地址的HTTP請求,squid有許多不一樣的ACL類型,src類型匹配客戶端IP,squid會針對客戶HTTP請求檢查http_access規則。
如:
acl mynet src 10.0.0.0/24 htcp_access allow mynet
這兩行須要放在正確的位置,http_access的順序很是重要,可是ACL行的順序沒必要介意,squid默認的配置文件包含了一些重要的訪問控制,最好不要改變她或者刪除它們,除非你徹底理解它們的意思。
##訪問日誌,保存默認,修改成以下
access_log /application/squid3.0/var/logs/access.log squid ##修改存儲日誌打開 ============================================ sed -i 's/# cache_store_log/cache_store_log/g' squid.conf grep "cache_store" squid.conf ============================================ [root@Squid etc]# grep "cache_store" squid.conf # TAG: cache_store_log cache_store_log /application/squid3.0/var/logs/store.log ============================================
#打開緩存log
============================================ grep cache_log squid.conf sed -i 's/# cache_log/cache_log/g' squid.conf grep cache_log squid.conf ============================================ [root@Squid etc]# grep cache_log squid.conf # TAG: cache_log cache_log /application/squid3.0/var/logs/cache.log ============================================
#####修改緩存目錄,也能夠配置多個
grep cache_dir squid.conf sed -i 's%# cache_dir ufs /application/squid3.0/var/cach%cache_dir ufs /application/squid3.0/var/cach%g' squid.conf grep cache_dir squid.conf ============================================ [root@Squid etc]# grep cache_dir squid.conf # Applies to any cache_dir lines listed below this. # TAG: cache_dir # cache_dir Type Directory-Name Fs-specific-data [options] # You can specify multiple cache_dir lines to spread the # cache_dir ufs Directory-Name Mbytes L1 L2 [options] # cache_dir aufs Directory-Name Mbytes L1 L2 [options] # cache_dir diskd Directory-Name Mbytes L1 L2 [options] [Q1=n] [Q2=n] # block-size=n defines the "block size" for COSS cache_dir's. # leads to a maximum cache_dir size of 512<<24, or 8 GB. Note # has written some objects to the cache_dir. # no-store, no new objects should be stored to this cache_dir # the cache_dir lines with the smallest max-size value first and the cache_dir ufs /application/squid3.0/var/cache 100 16 256 # 'cache_dir' directory, but you may specify an alternate # a representation of the cache_dir name where each / is replaced # with '.'. This is needed to allow adding/removing cache_dir # If have more than one 'cache_dir', and %s is not used in the name # corresponds to the order of the 'cache_dir' lines in this # configuration file. If you change the order of the 'cache_dir' # the correct 'cache_dir' entry (unless you manually rename # better to keep these index files in each 'cache_dir' directory. [root@Squid etc]# ============================================
##編輯可見主機名,若是不配置,可能沒法啓動squid
sed -n '/visible_hostname/p' squid.conf sed -i '/# TAG: visible_hostname/avisible_hostname img01.etiantian.org ' squid.conf sed -n '/visible_hostname/p' squid.conf ============================================ [root@Squid etc]# sed -n '/visible_hostname/p' squid.conf # TAG: visible_hostname visible_hostname img01.etiantian.org # 'visible_hostname' you must give each machine a different [root@Squid etc]# ============================================
##配置配置管理員聯繫信息cache_mgr配置這個,在網站出問題的時候,客戶能夠找到我
sed -n '/cache_mgr /p' squid.conf sed -i 's/# cache_mgr webmaster/cache_mgr 75685538@qq.com/g' squid.conf sed -n '/cache_mgr /p' squid.conf ============================================ [root@Squid etc]# sed -n '/cache_mgr /p' squid.conf cache_mgr 75685538@qq.com [root@Squid etc]# ============================================
對比修改哪些內容:
############################################################################## ############################################################################## [root@Squid etc]# diff squid.conf squid.conf.default 1710c1710 < cache_dir ufs /application/squid3.0/var/cache 100 16 256 --- > # cache_dir ufs /application/squid3.0/var/cache 100 16 256 1889c1889 < cache_log /application/squid3.0/var/logs/cache.log --- > # cache_log /application/squid3.0/var/logs/cache.log 1899c1899 < cache_store_log /application/squid3.0/var/logs/store.log --- > # cache_store_log /application/squid3.0/var/logs/store.log 2912c2912 < cache_mgr 75685538@qq.com --- > # cache_mgr webmaster 2941,2942c2941 < cache_effective_user squid < cache_effective_group squid --- > # cache_effective_user nobody 2971d2969 < visible_hostname img01.etiantian.org [root@Squid etc]# ##############################################################################
修改後的配置文件
[root@Squid etc]# egrep -v '^#|^$' squid.conf acl manager proto cache_object acl localhost src 127.0.0.1/32 acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 acl localnet src 10.0.0.0/8 # RFC1918 possible internal network acl localnet src 172.16.0.0/12 # RFC1918 possible internal network acl localnet src 192.168.0.0/16 # RFC1918 possible internal network acl SSL_ports port 443 acl Safe_ports port 80 # http acl Safe_ports port 21 # ftp acl Safe_ports port 443 # https acl Safe_ports port 70 # gopher acl Safe_ports port 210 # wais acl Safe_ports port 1025-65535 # unregistered ports acl Safe_ports port 280 # http-mgmt acl Safe_ports port 488 # gss-http acl Safe_ports port 591 # filemaker acl Safe_ports port 777 # multiling http acl CONNECT method CONNECT http_access allow manager localhost http_access deny manager http_access deny !Safe_ports http_access deny CONNECT !SSL_ports http_access allow localnet http_access deny all icp_access allow localnet icp_access deny all htcp_access allow localnet htcp_access deny all http_port 3128 hierarchy_stoplist cgi-bin ? cache_dir ufs /application/squid3.0/var/cache 100 16 256 access_log /application/squid3.0/var/logs/access.log squid cache_log /application/squid3.0/var/logs/cache.log cache_store_log /application/squid3.0/var/logs/store.log refresh_pattern ^ftp: 1440 20% 10080 refresh_pattern ^gopher: 1440 0% 1440 refresh_pattern (cgi-bin|\?) 0 0% 0 refresh_pattern . 0 20% 4320 cache_mgr 75685538@qq.com cache_effective_user squid cache_effective_group squid visible_hostname img01.etiantian.org icp_port 3130 coredump_dir /application/squid3.0/var/cache [root@Squid etc]# ##############################################################################
##啓動squid
[root@Squid etc]# /application/squid/sbin/squid -h Usage: squid [-cdhvzCDFNRVYX] [-s | -l facility] [-f config-file] [-[au] port] [-k signal] -a port Specify HTTP port number (default: 3128). -d level Write debugging to stderr also. -f file Use given config-file instead of /application/squid3.0/etc/squid.conf -h Print help message. -k reconfigure|rotate|shutdown|interrupt|kill|debug|check|parse Parse configuration file, then send signal to running copy (except -k parse) and exit. -s | -l facility Enable logging to syslog. -u port Specify ICP port number (default: 3130), disable with 0. -v Print version. -z Create swap directories -C Do not catch fatal signals. -D Disable initial DNS tests. -F Don't serve any requests until store is rebuilt. -N No daemon mode. -R Do not set REUSEADDR on port. -S Double-check swap during rebuild. -X Force full debugging. -Y Only return UDP_HIT or UDP_MISS_NOFETCH during fast reload.
簡單命令說明:
/application/squid/sbin/squid -z 初始化緩存空間 /application/squid/sbin/squid 啓動 /application/squid/sbin/squid -k shutdown 中止 /application/squid/sbin/squid -k reconfigure 從新載入配置文件 /application/squid/sbin/squid -k rotate 輪循日誌 /application/squid/sbin/squid -k parse檢查語法
#檢查配置文件語法
/application/squid/sbin/squid -k parse chown -R squid.squid /application/squid3.0/var/logs/ /application/squid/sbin/squid -k parse ===================================================================== [root@Squid etc]# /application/squid/sbin/squid -k parse 2015/09/08 11:23:45| Processing Configuration File: /application/squid3.0/etc/squid.conf (depth 0) 2015/09/08 11:23:45| Initializing https proxy context WARNING: Cannot write log file: /application/squid3.0/var/logs/cache.log /application/squid3.0/var/logs/cache.log: Permission denied 《=============語法問題。這個文件沒有出現 messages will be sent to 'stderr'. [root@Squid etc]# chown -R squid.squid /application/squid3.0/var/logs/ [root@Squid etc]# /application/squid/sbin/squid -k parse 2015/09/08 11:24:49| Processing Configuration File: /application/squid3.0/etc/squid.conf (depth 0) 2015/09/08 11:24:49| Initializing https proxy context [root@Squid etc]# =====================================================================
###設置環境變量
echo 'export PATH=$PATH:/application/squid/sbin:=/application/squid/bin '>>/etc/profile . /etc/profile echo $PATH
###初始化squid
grep cache_dir squid.conf ll /application/squid3.0/var/cache ll /application/squid3.0/var/ squid -z /application/squid3.0/var/ chown -R squid.squid /application/squid3.0/var squid -z /application/squid3.0/var/ tree /application/squid3.0/var/ |wc -l ===================================================================== [root@Squid etc]# grep cache_dir squid.conf # Applies to any cache_dir lines listed below this. # TAG: cache_dir # cache_dir Type Directory-Name Fs-specific-data [options] # You can specify multiple cache_dir lines to spread the # cache_dir ufs Directory-Name Mbytes L1 L2 [options] # cache_dir aufs Directory-Name Mbytes L1 L2 [options] # cache_dir diskd Directory-Name Mbytes L1 L2 [options] [Q1=n] [Q2=n] # block-size=n defines the "block size" for COSS cache_dir's. # leads to a maximum cache_dir size of 512<<24, or 8 GB. Note # has written some objects to the cache_dir. # no-store, no new objects should be stored to this cache_dir # the cache_dir lines with the smallest max-size value first and the cache_dir ufs /application/squid3.0/var/cache 100 16 256 # 'cache_dir' directory, but you may specify an alternate # a representation of the cache_dir name where each / is replaced # with '.'. This is needed to allow adding/removing cache_dir # If have more than one 'cache_dir', and %s is not used in the name # corresponds to the order of the 'cache_dir' lines in this # configuration file. If you change the order of the 'cache_dir' # the correct 'cache_dir' entry (unless you manually rename # better to keep these index files in each 'cache_dir' directory. [root@Squid etc]# ll /application/squid3.0/var/cache ls: cannot access /application/squid3.0/var/cache: No such file or directory [root@Squid etc]# [root@Squid etc]# [root@Squid etc]# ll /application/squid3.0/var/ total 4 drwxr-xr-x. 2 squid squid 4096 Nov 10 00:15 logs [root@Squid etc]# squid -z /application/squid3.0/var/ 2015/11/10 10:09:45| Creating Swap Directories FATAL: Failed to make swap directory /application/squid3.0/var/cache: (13) Permission denied [root@Squid etc]# chown -R squid.squid /application/squid3.0/var [root@Squid etc]# squid -z /application/squid3.0/var/ 2015/11/10 10:10:09| Creating Swap Directories 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/00 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/01 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/02 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/03 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/04 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/05 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/06 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/07 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/08 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/09 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/0A 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/0B 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/0C 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/0D 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/0E 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/0F [root@Squid etc]# tree /application/squid3.0/var/ |wc -l 4118 [root@Squid etc]# =====================================================================
###啓動squid
squid -N -d1 初始化cache目錄後,就能夠在終端窗口裏運行squid,將日誌記錄到標準輸出,這樣就能夠輕易的定位任何錯誤或者問題,而且確認squid是否成功啓動,使用-N參數,會持續squid在前臺運行。-d1選項在標準錯誤裏顯示1級錯誤的調試信息,假如你看到錯誤信息,你該首先修正它,一旦你見到「Ready to serve requests. 」消息,就可用一些http請來測試squid。 ===================================================================== [root@Squid etc]# squid -N -d1 2015/09/08 11:37:46| Starting Squid Cache version 3.0.STABLE20 for x86_64-unknown-linux-gnu... 2015/09/08 11:37:46| Process ID 51053 2015/09/08 11:37:46| With 20480 file descriptors available 2015/09/08 11:37:46| Performing DNS Tests... 2015/09/08 11:37:46| Successful DNS name lookup tests... 2015/09/08 11:37:46| helperOpenServers: Starting 5/5 'dnsserver' processes 2015/09/08 11:37:46| User-Agent logging is disabled. 2015/09/08 11:37:46| Referer logging is disabled. 2015/09/08 11:37:47| Unlinkd pipe opened on FD 14 2015/09/08 11:37:47| Swap maxSize 102400 + 8192 KB, estimated 8507 objects 2015/09/08 11:37:47| Target number of buckets: 425 2015/09/08 11:37:47| Using 8192 Store buckets 2015/09/08 11:37:47| Max Mem size: 8192 KB 2015/09/08 11:37:47| Max Swap size: 102400 KB 2015/09/08 11:37:47| Version 1 of swap file without LFS support detected... 2015/09/08 11:37:47| Rebuilding storage in /application/squid3.0/var/cache (CLEAN) 2015/09/08 11:37:47| Using Least Load store dir selection 2015/09/08 11:37:47| Set Current Directory to /application/squid3.0/var/cache 2015/09/08 11:37:47| Loaded Icons. 2015/09/08 11:37:47| Accepting HTTP connections at 0.0.0.0, port 3128, FD 16. 2015/09/08 11:37:47| Accepting ICP messages at 0.0.0.0, port 3130, FD 17. 2015/09/08 11:37:47| HTCP Disabled. 2015/09/08 11:37:47| Pinger socket opened on FD 19 2015/09/08 11:37:47| Ready to serve requests. <=================表示成功 2015/09/08 11:37:47| Done reading /application/squid3.0/var/cache swaplog (0 entries) 2015/09/08 11:37:47| Finished rebuilding storage from disk. 2015/09/08 11:37:47| 0 Entries scanned 2015/09/08 11:37:47| 0 Invalid entries. 2015/09/08 11:37:47| 0 With invalid flags. 2015/09/08 11:37:47| 0 Objects loaded. 2015/09/08 11:37:47| 0 Objects expired. 2015/09/08 11:37:47| 0 Objects cancelled. 2015/09/08 11:37:47| 0 Duplicate URLs purged. 2015/09/08 11:37:47| 0 Swapfile clashes avoided. 2015/09/08 11:37:47| Took 0.02 seconds ( 0.00 objects/sec). 2015/09/08 11:37:47| Beginning Validation Procedure 2015/09/08 11:37:47| Completed Validation Procedure 2015/09/08 11:37:47| Validated 25 Entries 2015/09/08 11:37:47| store_swap_size = 0 2015/09/08 11:37:48| storeLateRelease: released 0 objects =====================================================================
###不能中止另一個窗口,查看是否啓動
netstat -lntup|grep 31 ===================================================================== [root@Squid ~]# netstat -lntup|grep 31 tcp 0 0 0.0.0.0:3128 0.0.0.0:* LISTEN 6993/squid udp 0 0 0.0.0.0:3130 0.0.0.0:* 6993/squid [root@Squid ~]# =================================================================
====
#上面的是普通代理
用法:
打開IE瀏覽器>工具>鏈接>局域網設置>爲LAN使用代理服務器
把squid服務器ip和3128端口填上去便可
以後從新打開IE,瀏覽網頁。
一邊看日誌,通常經過IE瀏覽,會看到訪問日誌不停滾動
tailf /application/squid/var/logs/access.log
看服務器日誌
================= [root@Squid ~]# tailf /application/squid/var/logs/access.log 1447122191.894 23 10.0.0.1 TCP_MISS/200 7428 GET http://img.firefoxchina.cn/2015/11/8/201511090953240.jpg - DIRECT/222.186.20.109 p_w_picpath/jpeg 1447122191.895 24 10.0.0.1 TCP_MISS/200 5339 GET http://img.firefoxchina.cn/2015/11/8/201511090954000.jpg - DIRECT/58.220.2.124 p_w_picpath/jpeg 1447122191.900 29 10.0.0.1 TCP_MISS/200 20789 GET http://img.firefoxchina.cn/2015/11/4/201511100926100.jpg - DIRECT/58.220.2.10 p_w_picpath/jpeg 1447122191.901 27 10.0.0.1 TCP_MISS/200 25635 GET http://img.firefoxchina.cn/2015/11/8/201511091118200.jpg - DIRECT/58.217.195.246 p_w_picpath/jpeg 1447122191.916 41 10.0.0.1 TCP_MISS/200 6183 GET http://img.firefoxchina.cn/2015/11/8/201511091539590.jpg - DIRECT/222.186.20.123 p_w_picpath/jpeg 1447122193.337 30012 10.0.0.1 TCP_MISS/200 2878 CONNECT g.alicdn.com:443 - DIRECT/222.73.134.40 - 1447122193.493 30011 10.0.0.1 TCP_MISS/200 2878 CONNECT img.alicdn.com:443 - DIRECT/101.226.178.140 - [root@Squid ~]# tree /application/squid3.0/var/cache/|wc -l 4714 [root@Squid ~]# [root@Squid ~]# tree /application/squid3.0/var/cache/|wc -l 4847 [root@Squid ~]# ##################################前面的就是普通代理模式 #######設置開機自啓動 /application/squid/sbin/squid -D ##-D 參數指後臺運行 echo "start squid">>/etc/rc.local echo "/application/squid/sbin/squid -D">>/etc/rc.local tail -2 /etc/rc.local ######################################################
設置啓動腳本
#!/bin/sh ################################################ #this scripts is created by gao at 20150908 #gao lvnian ################################################ #!/bin/bash # # chkconfig: 345 89 24 # description: squid is a web cache server # processname: squid . /etc/rc.d/init.d/functions function_start_squid() { /application/squid/sbin/squid -D if [ $? == "0" ];then action " $1 squid..." /bin/true sleep 3 echo "-----------------------check ing-----------------------------" netstat -lntup|egrep '31|squid' echo "-----------------------check over-----------------------------" else action " $1 squid..." /bin/failed echo "please check the log" fi } #stop function function_stop_squid() { /application/squid/sbin/squid -k shutdown if [ $? == "0" ];then action "$1 squid..." /bin/true else action "$1 squid..." /bin/failed echo "please check the log" fi } #restart function function_restart_squid() { printf "Restarting squid...\n" function_stop_squid sleep 2 function_start_squid } case $1 in start) function_start_squid ;; stop) function_stop_squid ;; restart) function_restart_squid ;; *) printf "Usage: $0 {start|stop|restart}\n" esac ======================================================== 把這個文件放到/etc/init.d/目錄下並命名爲squid chmod +x /etc/init.d/squid chkconfig --add squid ======================================================== [root@Squid ~]# chmod +x /etc/init.d/squid [root@Squid ~]# chkconfig --add squid [root@Squid ~]# chkconfig --list squid squid 0:off 1:off 2:off 3:on 4:on 5:on 6:off [root@Squid ~]#
##設置日誌輪詢
/application/squid/sbin/squid -k rotate 查看日誌 [root@Squid ~]# ll /application/squid/var/logs/ total 276 -rw-r----- 1 squid squid 99547 Sep 8 13:05 access.log -rw-r----- 1 squid squid 28301 Sep 8 13:05 cache.log -rw-r--r-- 1 root squid 6 Sep 8 13:03 squid.pid -rw-r----- 1 squid squid 134582 Sep 8 13:05 store.log [root@Squid ~]# /application/squid/sbin/squid -k rotate [root@Squid ~]# ll /application/squid/var/logs/ total 284 -rw-r----- 1 squid squid 0 Sep 8 13:08 access.log -rw-r----- 1 squid squid 100020 Sep 8 13:08 access.log.0 -rw-r----- 1 squid squid 458 Sep 8 13:08 cache.log -rw-r----- 1 squid squid 28301 Sep 8 13:05 cache.log.0 -rw-r--r-- 1 root squid 6 Sep 8 13:03 squid.pid -rw-r----- 1 squid squid 0 Sep 8 13:08 store.log -rw-r----- 1 squid squid 135372 Sep 8 13:08 store.log.0 [root@Squid ~]# #######設置切換時名稱改成時間類型 腳本 echo "0 0 * * * /bin/sh /server/script/rotate_squid.sh" >>/var/spool/cron/root crontab -l #################### mkdir -p /server/script/ cat > /server/script/rotate_squid.sh <EOF #!/bin/sh cd /application/squid/var/logs/ [ -f access.log ] && mv access.log access_`data +%F`.log /application/squid/sbin/squid -k rotate EOF =================================================================== [root@Squid script]# cat rotate_squid.sh #!/bin/sh cd /application/squid/var/logs/ [ -f access.log ] && access.log access_`data +%F`.log /application/squid/sbin/squid -k rotate [root@Squid script]# [root@Squid script]# cat rotate_squid.sh #!/bin/sh cd /application/squid/var/logs/ [ -f access.log ] && { for n in `ls *.log` do mv $n $n.`date +%F.%N` done } /application/squid/sbin/squid -k rotate [root@Squid script]# ================================================================
到這來一個完整的squid代理服務器就安裝完成了
###配置管理頁面
1.squid有一個cachemgr.cgi 的程序,能夠用web來顯示內容,這個對調整squid的參數很方便,平時咱們安裝完squid後,其實就有這個程序了,只有咱們在apache下作相應的配置便可
======================================== ScriptAlias "/squid" "/application/squid3.0/libexec/cachemgr.cgi" <Location "/squid"> Orderdeny,allow Denyfrom all Allowfrom all </Location> #cachemgr_passwd lvnian config ##這個若是啓動。第一個lvnian是密碼,第二個config是行爲 ##修改一個端口爲8080(可選) Listen 8080 ======================================== 2.直接打開測試接能夠了,默認好像是不用密碼和用戶的,記住端口便可 http://ip/squid http://10.0.0.4:8080/squid
實戰
配置ACL
############################## acl lvnian url_regex -i ^http://.*baidu.* ##禁止訪問http開頭,且保護oldboy字符串的網站 acl lvnian1 url_regex -i ^http://.*51.*| .*taobao.* ##禁止訪問http開頭,且保護oldboy字符串的網站,或者包含aobao字符串的網站網站 http_access deny lvnian http_access deny lvnian1
提示:注意放置的位置:
[root@Squid etc]# vim squid.conf acl manager proto cache_object acl localhost src 127.0.0.1/32 acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 acl localnet src 10.0.0.0/8 # RFC1918 possible internal network acl localnet src 172.16.0.0/12 # RFC1918 possible internal network acl localnet src 192.168.0.0/16 # RFC1918 possible internal network acl SSL_ports port 443 acl Safe_ports port 80 # http acl Safe_ports port 21 # ftp acl Safe_ports port 443 # https acl Safe_ports port 70 # gopher acl Safe_ports port 210 # wais acl Safe_ports port 1025-65535 # unregistered ports acl Safe_ports port 280 # http-mgmt acl Safe_ports port 488 # gss-http acl Safe_ports port 591 # filemaker acl Safe_ports port 777 # multiling http acl CONNECT method CONNECT acl Safe_ports port 70 # gopher acl Safe_ports port 210 # wais acl Safe_ports port 1025-65535 # unregistered ports acl Safe_ports port 280 # http-mgmt acl Safe_ports port 488 # gss-http acl Safe_ports port 591 # filemaker acl Safe_ports port 777 # multiling http acl CONNECT method CONNECT ########################### acl lvnian url_regex -i ^http://.*baidu.* acl lvnian1 url_regex -i ^http://.*51.*| .*taobao.* http_access deny lvnian http_access deny lvnian1 ############################ http_access allow manager localhost http_access deny manager http_access deny !Safe_ports http_access deny CONNECT !SSL_ports http_access allow localnet http_access deny all icp_access allow localnet icp_access deny all htcp_access allow localnet htcp_access deny all http_port 3128 hierarchy_stoplist cgi-bin ? "squid.conf" 50L, 1932C written [root@Squid etc]# /etc/init.d/squid restart Restarting squid... squid... [ OK ] squid... [ OK ] -----------------------check ing----------------------------- udp 0 0 0.0.0.0:3130 0.0.0.0:* 7112/(squid) -----------------------check over----------------------------- [root@Squid etc]#
這個時候你就沒法訪問51cto的網站,百度網站,以及淘寶的網站了。
部署完成