環境規劃
| 角色 | IP 位址 | 說明 |
| Squid Proxy | 172.20.65.201 | 監聽 80 埠,加速模式 |
| Web Server | 172.20.65.203 | 後端源站,80 埠 |
| Client | 172.20.65.231 | 測試客戶端 |
安裝步驟
- 下載:取得 Squid for Windows (建議 2.7.STABLE6 或更新版)
- 解壓:解至
C:\squid (或其他磁碟)
- 建立快取與日誌目錄:
D:\squid\var\cache # 磁碟快取
D:\squid\var\logs # 存取/快取/錯誤日誌
- 權限設定:建立服務帳號
squidApp,授予 D:\squid 讀寫權限
- 配置檔準備:
etc 目錄下將 .default 檔重命名移除後綴:
squid.conf.default → squid.conf
mime.conf.default → mime.conf
cachemgr.conf.default → cachemgr.conf
核心配置詳解 (squid.conf)
1. 存取控制清單 (ACL)
# 基礎 ACL
acl all src all
acl manager proto cache_object
acl localhost src 127.0.0.1/32
acl to_localhost dst 127.0.0.0/8
# 區域網路 (依實際網段調整)
acl localnet src 172.20.65.231/255.255.0.0
# 埠號 ACL
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 1025-65535 # 非註冊埠
acl CONNECT method CONNECT
2. 存取規則 (http_access) - 順序極重要!
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 allow all
http_access deny all # 最後防線
3. 監聽埠與加速模式
# 監聽 80 埠,vhost 開啟加速器模式
http_port 80 vhost
4. 上游伺服器 (父節點)
# 172.20.65.203 為後端 Web Server
# parent=父節點, 80=源站埠, 0=ICP 埠, no-query=不查詢 ICP, originserver=直接從源站取得
cache_peer 172.20.65.203 parent 80 0 no-query originserver
5. 快取參數調優
maximum_object_size_in_memory 1024 KB # 記憶體快取最大物件
cache_dir ufs D:/squid/var/cache 1024 16 256 # 路徑、大小(MB)、L1目錄、L2目錄
maximum_object_size 4096 KB # 磁碟快取最大物件
cache_swap_low 90 # 低水位 90%
cache_swap_high 95 # 高水位 95%
6. 日誌配置
access_log D:/squid/var/logs/access.log squid
cache_log D:/squid/var/logs/cache.log
cache_store_log D:/squid/var/logs/store.log
emulate_httpd_log on # Apache 格式方便分析
7. 重新整理模式 (快取策略)
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
# 靜態資源積極快取
refresh_pattern -i \.css$ 1440 50% 120 reload-into-ims
refresh_pattern -i \.xml$ 1440 50% 120 reload-into-ims
refresh_pattern -i \.html$ 1440 90% 120 reload-into-ims
refresh_pattern -i \.jpg$ 1440 90% 120 reload-into-ims
refresh_pattern -i \.png$ 1440 90% 120 reload-into-ims
refresh_pattern -i \.gif$ 1440 90% 120 ignore-reload
refresh_pattern -i \.js$ 1440 90% 120 reload-into-ims
refresh_pattern -i \.htm$ 1440 90% 120 reload-into-ims
8. 其他設定
cache_mgr admin@example.com
hosts_file C:/Windows/System32/drivers/etc/hosts
服務安裝與啟動
# 1. 安裝為 Windows 服務
C:\squid\sbin\squid.exe -i -n Squid_Proxy
# 2. 初始化快取目錄 (首次必須)
C:\squid\sbin\squid.exe -z
# 3. 啟動服務
services.msc → 找到 Squid_Proxy → 啟動
# 4. 常用指令
squid -k reconfigure # 熱重載配置
squid -k rotate # 日誌輪替
squid -k shutdown # 優雅關閉
squid -d 1 # 前台除錯模式
測試與驗證
- 客戶端瀏覽器設定代理:
172.20.65.201:80
- 存取
https://172.20.65.203 或外部網站
- 查看
access.log 關鍵字:
TCP_MEM_HIT:記憶體快取命中
TCP_HIT:磁碟快取命中
TCP_MISS:未命中,回源站
TCP_DENIED:存取被拒 (ACL 配置問題)
常見問題排查
- 服務啟動失敗:執行
squid -k parse 檢查語法,查看 cache.log 錯誤詳情
- 快取目錄權限:確認服務帳號對
D:\squid\var\cache 有讀寫權
- 上游連線失敗:檢查防火牆、
cache_peer IP/埠、源站是否正常
- 日誌爆滿:設定
logfile_rotate 10 + 排程 squid -k rotate
進階技巧
- 啟用中文錯誤頁:
error_directory /usr/share/squid/errors/Simplify_Chinese (Linux 路徑,Windows 需對應目錄)
- SSL Bump 解密 HTTPS 快取:需編譯支援 OpenSSL 版本,配置
ssl_bump 相關指令
- Purge 客戶端快取:
squidclient -m PURGE https://example.com/resource