Squid概述及相关配置说明
一、簡介
代理服務器英文全稱是Proxy Server,其功能就是代理網絡用戶去取得網絡信息。
Squid是一個緩存Internet 數據的軟件,其接收用戶的下載申請,并自動處理所下載的數據。當一個用戶想要下載一個主頁時,可以向Squid 發出一個申請,要Squid 代替其進行下載,然后Squid 連接所申請網站并請求該主頁,接著把該主頁傳給用戶同時保留一個備份,當別的用戶申請同樣的頁面時,Squid 把保存的備份立即傳給用戶,使用戶覺得速度相當快。Squid 可以代理HTTP、FTP、GOPHER、SSL和WAIS等協議并且Squid 可以自動地進行處理,可以根據自己的需要設置Squid,使之過濾掉不想要的東西。
1.1 工作流程
當代理服務器中有客戶端需要的數據時:
? ? ? ? a. 客戶端向代理服務器發送數據請求;
? ? ? ? b. 代理服務器檢查自己的數據緩存;
? ? ? ? c. 代理服務器在緩存中找到了用戶想要的數據,取出數據;
? ? ? ? d. 代理服務器將從緩存中取得的數據返回給客戶端。
當代理服務器中沒有客戶端需要的數據時:
? ? ? ? 1. 客戶端向代理服務器發送數據請求;
? ? ? ? 2. 代理服務器檢查自己的數據緩存;
? ? ? ? 3. 代理服務器在緩存中沒有找到用戶想要的數據;
? ? ? ? 4. 代理服務器向Internet 上的遠端服務器發送數據請求;
? ? ? ? 5. 遠端服務器響應,返回相應的數據;
? ? ? ? 6. 代理服務器取得遠端服務器的數據,返回給客戶端,并保留一份到自己的數據緩存中。
Squid代理服務器工作在TCP/IP的應用層。
1.2 Squid 分類
按照代理類型的不同,可以將Squid 代理分為正向代理和反向代理,正向代理中,根據實現方式的不同,又可以分為普通代理和透明代理。
-
普通代理:需要客戶機在瀏覽器中指定代理服務器的地址、端口;
-
透明代理:適用于企業的網關主機(共享接入Internet)中,客戶機不需要指定代理服務器地址、端口等信息,代理服務器需要設置防火墻策略將客戶機的Web訪問數據轉交給代理服務程序處理;
-
反向代理:是指以代理服務器來接受internet上的連接請求,然后將請求轉發給內部網絡上的服務器,并將從服務器上得到的結果返回給internet上請求連接的客戶端,此時代理服務器對外就表現為一個服務器。
官方地址:http://www.squid-cache.org/
參考文檔:http://www.squid-cache.org/Doc/config/
二、編譯安裝參數說明
配置參數示例:
./configure ?--prefix=/usr/local/squid \
--enable-async-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-languages="Simplify_Chinese English" \
--disable-poll \
--disable-wccp \
--disable-wccpv2 \
--disable-ident-lookups \
--disable-internal-dns \
--enable-basic-auth-helpers="NCSA" \
--enable-stacktrace \
--with-large-files \
--disable-mempools \
--with-filedescriptors=64000 \
--enable-ssl \
--enable-x-accelerator-vary \
--disable-snmp \
--with-aio \
--enable-linux-netfilter \
--enable-linux-tproxy
Squid編譯安裝參數說明:
-
--prefix=/usr/local/squid :指定軟件的安裝路徑
-
--prefix=PREFIX ?默認的安裝位置是/usr/local/squid
-
--enable-icmp ? ?啟用ICMP ping。squid能利用ICMP消息來確定回環時間尺寸,非常像ping程序,可以通過使用該選項來激活這些功能。
-
--disable-htcp ? ?禁用HTCP協議支持。HTCP是超文本緩存協議--類似于ICP的內部緩存協議
-
--localstatedir ? 數據及日志的目錄(未來會很大)
-
–enable-gnuregex :支持GNU正則表達式。
-
–disable-carp: Cache數組路由協議(CARP)用來轉發丟失的cache到父cache的數組或cluste
-
–enable-async-io=100等同于同時打開./configure如下三個選項
-
--with-aufs-threads=N_THREADS
-
--with-pthreads
-
--enable-storeio=ufs,aufs
-
這個主要是設置async模式來運行squid,我的理解是設置用線程來運行squid,如果服務器配置很不錯,有1G以上內存,cpu使用SMP的方式的話可以考慮設成160或者更高。如果服務器比較糟糕就根據實際情況設了。另外此項還另cache文件支持aufs
-
--disable-wccp用于阻止或分發HTTP請求到一個或多個caches
-
–enable-icmp :加入icmp支持
-
–enable-kill-parent-hack :關掉suqid的時候,要不要連同父進程一起關掉
-
--enable-cachemgr-hostname=localhost:指定cachemgr-hostname值為localhost
-
-- enable-snmp :此選項可以讓MRTG使用SNMP協議對服務器的流量狀態進行監測,因此必須選擇此項,使Squid支持SNMP接口。
-
–disable-ident-lookups :防止系統使用RFC931規定的身份識別方法。
-
--enable-delay-pools 開啟squid延時池功能
-
–enable-cahce-digests :加快請求時,檢索緩存內容的速度。
-
–enable-err-language=”Simplify_Chinese” 和
-
–enable-default-err-languages=”Simplify_Chinese” :指定出錯是顯示的錯誤頁面為簡體中文
-
--with-maxfd=65535指定最大文件描述
-
–enable-poll :指定使用Poll()函數,提升性能就是啦。
-
--disable-ident-lookups允許服務器利用客戶端的特殊TCP連接來發現用戶名
-
–enable-linux-netfilter :可以支持透明代理
-
--enable-large-cache-files開啟大文件支持,支持2GB以上的文件
-
--disable-internal-dns使用自己的內部DNS查詢
-
–enable-underscore :允許解析的URL中出現下劃線,因為默認squid會認為帶下劃線的URL地址是非法的,并拒絕訪問該地址
-
–enable-arp-acl :可以在規則設置中直接通過客戶端的MAC地址進行管理,防止客戶使用IP欺騙。
三、squid配置文件講解
http_port 3128 ? ? //設置監聽的IP與端口號
cache_mem 64 MB //額外提供給squid使用的內存,squid的內存總占用為 X * 10+15+“cache_mem”,其中X為squid的cache占用的容量(以GB為單位),
?//比如下面的cache大小是100M,即0.1GB,則內存總占用為0.1*10+15+64=80M,推薦大小為物理內存的1/3-1/2或更多。
maximum_object_size 4 MB //設置squid磁盤緩存最大文件,超過4M的文件不保存到硬盤
minimum_object_size 0 KB //設置squid磁盤緩存最小文件
maximum_object_size_in_memory 4096 KB //設置squid內存緩存最大文件,超過4M的文件不保存到內存
cache_dir ufs /var/spool/squid 100 16 256 //定義squid的cache存放路徑 、cache目錄容量(單位M)、一級緩存目錄數量、二級緩存目錄數量
logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh ? ? ? ?//log文件日志格式
access_log /var/log/squid/access.log combined //log文件存放路徑和日志格式
cache_log /var/log/squid/cache.log //設置緩存日志
logfile_rotate 60 //log輪循 60天
cache_swap_high 95 //cache目錄使用量大于95%時,開始清理舊的cache
cache_swap_low 90 //cache目錄清理到90%時停止。
acl localnet src 192.168.1.0/24 //定義本地網段
http_access allow localnet //允許本地網段使用
http_access deny all //拒絕所有
visible_hostname squid.david.dev //主機名
cache_mgr mchina_tang@qq.com //管理員郵箱
四、配置文件示例
squid常用命令:
/usr/local/squid/sbin/squid -z 初始化緩存空間
/usr/local/squid/sbin/squid 啟動
/usr/local/squid/sbin/squid -k shutdown 停止
/usr/local/squid/sbin/squid -k reconfigure 重新載入配置文件
/usr/local/squid/sbin/squid -k rotate 輪循日志
#acl all src 0.0.0.0/0.0.0.0 and http_access allow all選項定義了一個訪問控制列表。詳細情況參見和Squid軟件
#攜帶的文檔。這里的訪問控制列表允許所有對代理服務的訪問,因為這里該代理是加速web服務器。
acl all src 0.0.0.0/0.0.0.0 ? ? ? ? ? ? ? ? #允許所有IP訪問
acl manager proto http ? ? ? ? ? ? ? ? #manager url協議為http
acl localhost src 127.0.0.1/255.255.255.255 ?#允午本機IP
acl to_localhost dst 127.0.0.1 ? ? ? ? ? ? ? ? #允午目的地址為本機IP
acl Safe_ports port 80 ? ? ? ? ? ? ? ?# 允許安全更新的端口為80
acl CONNECT method CONNECT ? ? ? ?#請求方法以CONNECT
http_access allow all ? ? ? ? ? ? ? ?#允許所有人使用該代理.因為這里是代理加速web服務器
http_reply_access allow all ? ? ? ? ? ? ? ?#允許所有客戶端使用該代理
acl OverConnLimit maxconn 16 ? ? ? ?#限制每個IP最大允許16個連接,防止攻擊
http_access deny OverConnLimit
icp_access deny all ? ? ? ? ? ? ? ? ? ? ? ?#禁止從鄰居服務器緩沖內發送和接收ICP請求.
miss_access allow all ? ? ? ? ? ? ? ?#允許直接更新請求
ident_lookup_access deny all ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#禁止lookup檢查DNS
http_port 8080 transparent ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#指定Squid監聽瀏覽器客戶請求的端口號。
hierarchy_stoplist cgi-bin ? ? ? ? ? ? ? ? ?#用來強制某些特定的對象不被緩存,主要是處于安全的目的。
acl QUERY urlpath_regex cgi-bin \?
cache deny QUERY
cache_mem 1 GB ? ? ? ?#這是一個優化選項,增加該內存值有利于緩存。應該注意的是:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #一般來說如果系統有內存,設置該值為(n/)3M?,F在是3G 所以這里1G
fqdncache_size 1024 ? ? ? ?#FQDN 高速緩存大小
maximum_object_size_in_memory 2 MB ? ? ? ?#允許最大的文件載入內存
memory_replacement_policy heap LFUDA ?#動態使用最小的,移出內存cache
cache_replacement_policy heap LFUDA ? ? ? ? #動態使用最小的,移出硬盤cache
cache_dir ufs /home/cache 5000 32 512 ?#高速緩存目錄 ufs 類型使用的緩沖值最大允午1000MB空間,
#32個一級目錄,512個二級目錄
max_open_disk_fds 0 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #允許最大打開文件數量,0 無限制
minimum_object_size 1 KB ? ? ? ? ? ? ? ? ? ? ? ? #允午最小文件請求體大小
maximum_object_size 20 MB ? ? ? ? ? ? ? ? #允午最大文件請求體大小
cache_swap_low 90 ? ? ? ? ? ? ? ? ? ? ? ? ? ?#最小允許使用swap 90%
cache_swap_high 95 ? ? ? ? ? ? ? ? ? ? ? ? ? ?#最多允許使用swap 95%
ipcache_size 2048 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# IP 地址高速緩存大小 2M
ipcache_low 90 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#最小允許ipcache使用swap 90%
ipcache_high 95 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#最大允許ipcache使用swap 90%
access_log /var/log/squid/access.log squid ? ? ? ?#定義日志存放記錄
cache_log /var/log/squid/cache.log squid
cache_store_log none ? ? ? ? ? ? ? ? ? ? ? ?#禁止store日志
emulate_httpd_log on ? ? ? ?#將使Squid仿照Web服務器的格式創建訪問記錄。如果希望使用
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#Web訪問記錄分析程序,就需要設置這個參數。
refresh_pattern . 0 20% 4320 override-expire override-lastmod reload-into-ims ignore-reload ? #更新cache規則
acl buggy_server url_regex ^http://.... http:// ? ? ? ? ?#只允許http的請求
broken_posts allow buggy_server
acl apache rep_header Server ^Apache ? ? ? ? ? ? ? ? #允許apache的編碼
broken_vary_encoding allow apache
request_entities off ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#禁止非http的標分準請求,防止攻擊
header_access header allow all ? ? ? ? ? ? ? ? ? ? ? ?#允許所有的http報頭
relaxed_header_parser on ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#不嚴格分析http報頭.
client_lifetime 120 minute ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#最大客戶連接時間 120分鐘
cache_mgr sky@test.com ? ? ? ? ? ? ? ? ? ? ? ?#指定當緩沖出現問題時向緩沖管理者發送告警信息的地址信息。
cache_effective_user squid ? ? ? ? ? ? ? ? ? ? ? ?#這里以用戶squid的身份Squid服務器
cache_effective_group squid
icp_port 0 ? ? ? ? ? ? ? ? ? ? ? #指定Squid從鄰居服務器緩沖內發送和接收ICP請求的端口號。
? ? ? ? ? ? ? ? ? ? ?#這里設置為0是因為這里配置Squid為內部Web服務器的加速器,
? ? ? ? ? ? ? ? ? ? ?#所以不需要使用鄰居服務器的緩沖。0是禁用
# cache_peer 設置允許更新緩存的主機,因是本機所以127.0.0.1
cache_peer 127.0.0.1 parent 80 0 no-query default multicast-responder no-netdb-exchange
cache_peer_domain 127.0.0.1 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
hostname_aliases 127.0.0.1
error_directory /usr/share/squid/errors/Simplify_Chinese ? ? ? ?#定義錯誤路徑
always_direct allow all ? ? ? ? ? ? ? ?# cache丟失或不存在是允許所有請求直接轉發到原始服務器
ignore_unknown_nameservers on ? ? ? ?#開反DNS查詢,當域名地址不相同時候,禁止訪問
coredump_dir ?/var/log/squid ? ? ? ? ? ? ? ? #定義dump的目錄
max_filedesc 2048 ? ? ? ? ? ? ? ?#最大打開的文件描述
half_closed_clients off ? ? ? ?#使Squid在當read不再返回數據時立即關閉客戶端的連接。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #有時read不再返回數據是由于某些客戶關閉TCP的發送數據
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #而仍然保持接收數據。而Squid分辨不出TCP半關閉和完全關閉。
buffered_logs on #若打開選項“buffered_logs”可以稍稍提高加速某些對日志文件的寫入,該選項主要是實現優化特性。
#防止天涯盜鏈,轉嫁給百度
acl tianya referer_regex -i tianya
http_access deny tianya
deny_info ?tianya
#阻止baidu蜘蛛
acl baidu req_header User-Agent Baiduspider
http_access deny baidu
#限制同一IP客戶端的最大連接數
acl OverConnLimit maxconn 128
http_access deny OverConnLimit
#防止被人利用為HTTP代理,設置允許訪問的IP地址
acl myip dst 222.18.63.37
http_access deny !myip
#允許本地管理
acl Manager proto cache_object
acl Localhost src 127.0.0.1 222.18.63.37
http_access allow Manager Localhost
cachemgr_passwd 53034338 all
http_access deny Manager
#僅僅允許80端口的代理
acl all src 0.0.0.0/0.0.0.0
acl Safe_ports port 80 # http
http_access deny !Safe_ports
http_access allow all
#Squid信息設置
visible_hostname happy.swjtu.edu.cn
cache_mgr ?ooopic2008@qq.com
#基本設置
cache_effective_user squid
cache_effective_group squid
tcp_recv_bufsize 65535 bytes
#2.6的反向代理加速配置
cache_peer 127.0.0.1 parent 80 0 no-query originserver
#錯誤文檔
error_directory /usr/local/squid/share/errors/Simplify_Chinese
#單臺使用,不使用該功能
icp_port 0
hierarchy_stoplist cgi-bin ?
acl QUERY urlpath_regex cgi-bin \? .php .cgi .avi .wmv .rm .ram .mpg .mpeg .zip .exe
cache deny QUERY
acl apache rep_header Server ^Apache
broken_vary_encoding allow apache
refresh_pattern ^ftp: ? ? ? ? ? 1440 20% ? ? 10080
refresh_pattern ^gopher: ? ? ? ?1440 0% ? ?1440
refresh_pattern . ? ? ? ? ? ? 0 ? ?20% ? ? 4320
cache_store_log none
pid_filename /usr/local/squid/var/logs/squid.pid
emulate_httpd_log on
logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh
cache_log /usr/local/squid/var/logs/cache.log
access_log /usr/local/squid/var/logs/access.log combined
coredump_dir /usr/local/squid/var/cache
cache_dir ufs /usr/local/squid/var/cache 10000 16 256
dns_children 32
hosts_file /etc/hosts
cache_mem 400 MB
cache_swap_low 90
cache_swap_high 95
maximum_object_size 32768 KB
maximum_object_size_in_memory 4096 KB
emulate_httpd_log on
#防止盜鏈
acl picurl url_regex -i \.bmp$ \.png$ \.jpg$ \.gif$ \.jpeg$
acl mystie1 referer_regex -i happy.swjtu.edu.cn
http_access allow mystie1 picurl
acl nullref referer_regex -i ^$
http_access allow nullref
acl hasref referer_regex -i .+
http_access deny hasref picurl
本文轉自 jerry1111111 51CTO博客,原文鏈接:http://blog.51cto.com/jerry12356/1862270,如需轉載請自行聯系原作者
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的Squid概述及相关配置说明的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vue.js的一些事件绑定和表单数据双向
- 下一篇: linux下管理交换分区