Squid 是一款功能強大的開源代理伺服器軟體,廣泛用於網頁緩存、訪問控制與流量優化。本文將詳細介紹如何在 Windows 系統上部署 Squid,配置反向代理與緩存加速,並提供完整的安裝、配置與測試步驟。
配置文件中 # 代表註釋,啟用選項需移除前綴 #;TAG 標籤對應各功能項設定。
前往官方或可信鏡像站下載 Squid for Windows(本教程以 Squid 2.7.STABLE6 為例,不同版本配置細節可能略有差異)。
將壓縮包解壓至 C:\squid(或其他磁碟根目錄),目錄結構保持完整。
為提升 I/O 性能,建議將緩存與日誌放置於大容量磁碟(如 D 盤):
D:\squid\var\logs # 存放存取日誌、錯誤日誌
D:\squid\var\cache # 存放磁碟緩存資料
建立專用服務帳號(如 squidApp),歸屬於標準用戶組,授予 D:\squid 目錄完整讀寫權限,確保 Squid 進程能正常寫入緩存與日誌。
進入 C:\squid\etc,先備份原始配置,再重命名預設文件:
squid.conf.default → squid.confmime.conf.default → mime.confcachemgr.conf.default → cachemgr.confacl 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.16.65.231/255.255.0.0 # 指定內網網段
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 # 未註冊端口
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 all
http_access allow localnet
http_access deny all
http_port 80 vhost # 80 端口啟用 vhost 加速模式
預設 HTTP 端口為 3128,可在配置文件搜尋 http_port 確認。
cache_peer 172.20.65.203 parent 80 0 no-query originserver
parent:指定為父節點80:後端 Web 伺服器端口0:ICP 服務端口(0 表示不使用)no-query originserver:直接從源站獲取內容,不發送 ICP 查詢maximum_object_size_in_memory 1024 KB
cache_dir ufs D:/squid/var/cache 1024 16 256 # 注意路徑分隔符
maximum_object_size 4096 KB
cache_swap_low 90
cache_swap_high 95
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
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$ 20 50% 120 reload-into-ims
refresh_pattern -i \.xml$ 20 50% 120 reload-into-ims
refresh_pattern -i \.html$ 20 90% 120 reload-into-ims
refresh_pattern -i \.jpg$ 20 90% 120 reload-into-ims
refresh_pattern -i \.png$ 20 90% 120 reload-into-ims
refresh_pattern -i \.gif$ 20 90% 120 ignore-reload
refresh_pattern -i \.js$ 20 90% 120 reload-into-ims
refresh_pattern -i \.htm$ 20 90% 120 reload-into-ims
cache_mgr webmaster@breeze
hosts_file C:/Windows/System32/drivers/etc/hosts
C:\squid\sbin\squid.exe -i -n Squid_Proxy
C:\squid\sbin\squid.exe -z
此步驟會建立 cache_dir 指定的目錄結構,首次啟動前必須執行。
打開 services.msc,找到 Squid_Proxy 並啟動。若啟動失敗,90% 為配置文件語法錯誤,請檢查事件查看器或執行 squid -k parse 驗證。
| 命令 | 用途 |
|---|---|
squid -k reconfigure | 熱重載配置文件 |
squid -k rotate | 輪替日誌文件 |
squid -k shutdown | 優雅停止服務 |
squid -d 1 | 前台調試模式(輸出啟動過程) |
在客戶端配置代理指向 172.20.65.201:80,訪問任意網站,查看 access.log:
常用分析命令:
# 查看內存命中
grep TCP_MEM_HIT /path/to/access.log
# 查看磁碟命中
grep TCP_HIT /path/to/access.log
# 查看回源請求
grep TCP_MISS /path/to/access.log
編輯 crontab(Linux 環境下),每週六凌晨 2 點執行:
0 2 * * 6 /usr/local/squid/sbin/squid -k rotate
Windows 可使用任務計劃程序達成相同效果。
error_directory /usr/share/squid/errors/Simplify_Chinese
本文涵蓋了 Windows 下 Squid 代理伺服器的完整部署流程:從下載、安裝、目錄規劃、權限設定、核心配置(ACL、端口、父節點、緩存策略、日誌)、服務安裝、初始化、啟動驗證到日常維護命令。掌握這些步驟,即可快速構建高性能的正向/反向代理服務,實現訪問加速、帶寬節省與訪問控制。