Squid 是 GNU GPL 授權的代理/緩存伺服器,廣泛用於 Web 加速、LAN 共享上網、流量過濾安全防護。本教學以 **CentOS 5.x + Squid 3.0.STABLE25** 為例,提供高命中率配置參數。
wget https://www.squid-cache.org/Versions/v3/3.0/squid-3.0.STABLE25.tar.gz
tar zxf squid-3.0.STABLE25.tar.gz && cd squid-3.0.STABLE25
./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-arp-acl \
--enable-default-err-language=Simplify_Chinese \
--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=65536 \
--enable-ssl \
--enable-x-accelerator-var
make && make install && make install-pinger
# 加速器監聽埠
http_port 80 accel vhost vport
# 後端源站
cache_peer xx.xx.xx.xx parent 80 0 no-query originserver no-digest name=a
cache_peer_domain a www.111cn.net
# ACL 存取控制
acl manager proto cache_object
acl localhost src 127.0.0.1/255.255.255.255
acl SSL_ports port 443 563
acl Safe_ports port 80 8080
acl LanSrc src all
acl LanDst dst all
acl LanDstDM dstdomain www.111cn.net
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 LanSrc
http_access allow LanDst
http_access allow LanDstDM
http_access deny all
# 基礎設定
visible_hostname www.111cn.net
cache_mgr rocdk890@gmail.com
cache_effective_user 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
# 緩存替換策略與目錄
cache_replacement_policy lru
cache_dir aufs /var/cache1 32768 64 64
cache_dir aufs /var/cache2 32768 64 64
cache_mem 2048 MB
max_open_disk_fds 0
maximum_object_size 20 MB
maximum_object_size_in_memory 8 MB
# 連線保持
client_persistent_connections off
server_persistent_connections on
# 隱藏 Squid 標頭 (安全)
forwarded_for on
log_icp_queries off
via off
httpd_suppress_version_string off
ie_refresh off
tcp_recv_bufsize 32 KB
# 記憶體/快取優化
memory_replacement_policy lru
hosts_file /etc/hosts
request_header_max_size 128 KB
# 動態內容不緩存
hierarchy_stoplist cgi-bin ? .php
acl QUERY urlpath_regex cgi-bin? .php
cache deny QUERY
# 更新模式
refresh_pattern ^ftp: 60 20% 10080
refresh_pattern ^gopher: 60 0% 1440
refresh_pattern . 0 20% 1440
cache_dir aufs:使用 AUFS 儲存引擎,效能優於傳統 UFScache_mem 2048 MB:記憶體緩存 2GB,視實體 RAM 調整maximum_object_size 20 MB:允許緩存大檔案 (預設 4MB 偏小)removal_policies heap,lru:堆積+最近最少使用淘汰,提升命中率via off / forwarded_for on:隱藏代理身份但保留客戶端真實 IPcache deny QUERY:動態頁面 (PHP/CGI) 不緩存,保證資料即時性部署建議:生產環境請根據實際流量調整 `cache_dir` 大小與數量、`cache_mem`、檔案描述符上限。定期分析 `access.log` 優化 `refresh_pattern`。啟用 `cachemgr` 監控命中率與效能指標。