Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Squid 緩存配置全攻略:cache_mem、cache_dir、ACL 與啟動調試

編輯:Linux 教學

前言

Squid 作為經典代理緩存服務器,核心價值在於「緩存」。正確配置內存緩存(cache_mem)、磁盤緩存(cache_dir)、訪問控制(ACL)與啟動參數,能顯著提升命中率、降低帶寬成本。本文以 CentOS 5.3 + 自編譯 Squid 為例,詳解生產級配置全流程。

一、核心緩存參數

參數默認值建議值說明
cache_mem256 MB物理內存 1/8 ~ 1/4熱門對象內存緩存(不含索引、元數據),實際佔用約 1.5-2 倍。
maximum_object_size_in_memory512 KB64 KB - 256 KB單對象進入內存緩存的最大體積,過大擠佔內存。
cache_dir無見下文磁盤緩存目錄、大小、層級。
maximum_object_size4 MB128 MB - 512 MB單對象進入磁盤緩存的最大體積,大文件不緩存節省空間。
cache_swap_low / cache_swap_high90% / 95%85% / 95%磁盤緩存水位,超過 high 強制清理,低於 low 停止清理。

二、cache_dir 詳細配置

# 格式:cache_dir 類型 目錄 大小(MB) L1目錄數 L2目錄數
cache_dir ufs /var/spool/squid 10000 16 256
  • 類型:ufs(傳統)、aufs(異步 IO,推薦)、diskd(獨立進程)、rock(高性能固定槽,Squid 4+)。
  • 大小:不可小於 cache_mem,否則啟動警告 WARNING cache_mem is larger than total disk cache space!。
  • L1/L2:16/256 即 16×256=4096 個二級目錄,適合百萬級對象;小規模可用 8/16。

三、完整 squid.conf 示例

# /etc/squid/squid.conf

# 1. 基礎網絡
http_port 3128                 # 代理端口
# http_port 8080 intercept    # 透明代理需配合 iptables REDIRECT

# 2. 緩存參數(假設 8GB 內存,500GB 磁盤)
cache_mem 2048 MB
maximum_object_size_in_memory 128 KB
cache_dir aufs /var/spool/squid 100000 16 256
maximum_object_size 256 MB
cache_swap_low 85
cache_swap_high 95

# 3. 訪問控制 (ACL)
acl localnet src 10.0.0.0/8
acl localnet src 172.16.0.0/12
acl localnet src 192.168.0.0/16
acl SSL_ports port 443
acl Safe_ports port 80 21 443 70 210 280 488 591 777
acl CONNECT method CONNECT

http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow localhost manager
http_access deny manager
http_access allow localnet
http_access allow localhost
http_access deny all          # 默認拒絕,按需放開

# 4. 管理信息
cache_mgr admin@example.com
visible_hostname proxy.example.com

# 5. 日志
access_log /var/log/squid/access.log squid
cache_log /var/log/squid/cache.log
# store_log none              # 關閉 store.log 節省 IO

# 6. 性能優化
refresh_pattern ^ftp:       1440 20% 10080
refresh_pattern ^gopher:    1440 0%  1440
refresh_pattern -i (/cgi-bin/|\?) 0 0% 0
refresh_pattern .           0 20% 4320

# 7. DNS
dns_nameservers 223.5.5.5 114.114.114.114

# 8. 內存池優化
memory_pools on
memory_pools_limit 100 MB

四、初始化緩存目錄

# 首次安裝或修改 cache_dir 後必須執行
squid -z
# 輸出:Creating Swap Directories ... done

五、前台調試啟動

squid -NCd1
# -N: 不守護進程(前台運行)
# -C: 忽略缺少的 cache_dir(調試用)
# -d1: 調試等級 1(輸出關鍵啟動信息到 stderr)

觀察輸出直到出現 Ready to serve requests. 表示啟動成功。按 Ctrl+C 停止。

六、正式啟動與服務管理

# Systemd
systemctl start squid
systemctl enable squid

# SysVinit
service squid start
chkconfig squid on

七、驗證緩存工作

  1. 查看日志:tail -f /var/log/squid/access.log,出現 TCP_HIT/TCP_MEM_HIT 表示命中。
  2. 客戶端測試:
    curl -x https://proxy:3128 https://httpbin.org/get -I
    # 首次:X-Cache: MISS
    # 二次:X-Cache: HIT
  3. 統計信息:squidclient -h localhost -p 3128 mgr:info 查看命中率、內存/磁盤使用量。

八、常見問題與解決

問題原因解決
啟動報 FATAL: Could not determine fully qualified hostname缺少 visible_hostname設置 visible_hostname proxy.example.com
報 WARNING cache_mem is larger than total disk cache space!cache_dir 大小 < cache_mem增大 cache_dir 大小或減小 cache_mem
客戶端報 The requested URL could not be retrievedhttp_access deny all 生效添加 http_access allow localnet 或具體 IP ACL
透明代理不生效缺少 iptables REDIRECT 規則iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3128
磁盤 IO 過高store_log 開啟、maximum_object_size 過大store_log none、調小 maximum_object_size

九、進階優化方向

  • 分級緩存:父代理 cache_peer、never_direct 構建 CDN 邊緣節點。
  • SSL Bump:ssl_bump 解密 HTTPS 緩存(需處理憑證信任、隱私合規)。
  • 存儲後端替換:cache_dir rock(高併發)、cache_dir diskd(隔離 IO)、第三方 libsquidaio。
  • 監控告警:squidclient mgr:info + Prometheus Exporter + Grafana 看板。

結語

Squid 緩存配置的核心在於「內存緩存熱數據、磁盤緩存溫數據、ACL 精準放行、日志精簡 IO」。按本文參數模板調整、squid -z 初始化、squid -NCd1 前台調試、驗證 TCP_HIT,即可構建高可用高性能代理緩存節點。

Copyright © Windows教程網 All Rights Reserved