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

Windows 環境部署 Squid 反向代理/快取伺服器完整實戰指南

編輯:代理伺服器

Squid 簡介與 Windows 移植版

Squid 是類 Unix 系統主流的快取/代理伺服器,支援正向代理、反向代理 (Reverse Proxy)、多級快取、CDN 邊緣節點。Windows 移植版功能完整,適合 Windows 基礎設施環境。
下載:官方 Windows Binaries (請自行尋找最新版)。

一、安裝與服務註冊

  1. 解壓至 C:\squid (路徑**不可含空格**,配置檔建議用正斜線 /)
  2. C:\squid\etc 下複製三檔去除 .default 後綴:
    cachemgr.conf, mime.conf, squid.conf
  3. 管理員命令列註冊服務:
    C:\squid\sbin\squid -i -n SquidProxy (服務名自訂,含空格用雙引號)
  4. 建立快取目錄:
    C:\squid\sbin\squid -z (自動建立 C:\squid\var\cache 結構)
  5. 啟動服務:net start SquidProxy 或 C:\squid\sbin\squid -n SquidProxy

查看日誌排錯:C:\squid\sbin\squid.exe.log、C:\squid\var\cache.log

二、核心配置檔解析 (squid.conf)

建議使用 Notepad++ 編輯 (語法高亮)。以下針對四大典型場景給出關鍵配置片段。

場景 1:單站點反向代理 (最簡單)

# 允許所有客戶端
acl all src all
http_access allow all

# 監聽 80 埠
http_port 80

# 定義上游源站 (origin server)
cache_peer 172.16.0.20 parent 80 0 no-query originserver name=web1

場景 2:多站點反向代理 (依 Host Header 分流)

# 依域名建立 ACL
acl site_a dstdomain www.a.com
acl site_b dstdomain www.b.com
http_access allow site_a
http_access allow site_b

# 加速模式:轉發 Host Header 給後端
http_port 80 vhost

# 兩個源站
cache_peer 172.16.0.20 parent 80 0 no-query originserver name=web_a
cache_peer 172.16.0.30 parent 80 0 no-query originserver name=web_b

# 域名綁定源站 (如 IIS 主機頭)
cache_peer_domain web_a www.a.com
cache_peer_domain web_b www.b.com

# 允許存取對應源站
cache_peer_access web_a allow site_a
cache_peer_access web_b allow site_b

# 快取參數
cache_mem 1024 MB
cache_dir ufs c:/squid/var/cache 10240 16 256

場景 3:後端負載平衡 (輪詢多台源站)

acl site_a dstdomain www.a.com
http_access allow site_a
http_port 80 vhost

# 兩台源站輪詢
cache_peer 172.16.0.20 parent 80 0 no-query originserver round-robin name=web_a1
cache_peer 172.16.0.30 parent 80 0 no-query originserver round-robin name=web_a2

cache_mem 1024 MB
cache_dir ufs c:/squid/var/cache 10240 16 256

場景 4:多層 Squid 叢集 (ICP 對等互連 + 負載平衡)

acl site_a dstdomain www.a.com
acl localnet src 172.16.0.0/24  # 內網段供 Squid 間通訊
http_access allow site_a
icp_access allow localnet

http_port 80 vhost
icp_port 3130  # ICP 通訊埠

# 兩台 Squid 互為鄰居 (Sibling)
cache_peer 172.16.0.10 sibling 80 3130
cache_peer 172.16.0.11 sibling 80 3130

# 共同後端源站輪詢
cache_peer 172.16.0.20 parent 80 0 no-query originserver round-robin name=web_a1
cache_peer 172.16.0.30 parent 80 0 no-query originserver round-robin name=web_a2

cache_mem 1024 MB
cache_dir ufs c:/squid/var/cache 10240 16 256

三、配置熱重載 (無需重啟服務)

C:\squid\sbin\squid -k reconfigure -n SquidProxy

四、服務移除

net stop SquidProxy
sc delete SquidProxy

五、關鍵參數說明表

指令/參數用途
http_port 80 vhost監聽 80 埠 + 啟用虛擬主機模式 (轉發 Host Header)
cache_peer IP parent 80 0 no-query originserver定義父層源站,不查詢 ICP,標記為源站
round-robin啟用輪詢負載平衡
cache_peer_domain name domain將 cache_peer 綁定特定域名 (實現虛擬主機分流)
cache_mem 1024 MB記憶體快取大小 (熱門物件)
cache_dir ufs path size L1 L2磁碟快取:路徑、總大小(MB)、一級/二級子目錄數
icp_port 3130ICP 協定通訊埠 (叢集模式必須)
sibling vs parentsibling: 對等節點 (查詢快取);parent: 上游源站

六、生產環境建議

  • 監控 cache.log、access.log,設定日誌輪轉
  • 定期 squid -k rotate 切割日誌
  • 關鍵指標:命中率 (HIT/MISS)、記憶體/磁碟使用率、CPU、網路 I/O
  • 安全:http_access deny all 預設拒絕,僅允許業務必要 ACL;封鎖管理介面 (cachemgr.cgi) 僅限內網存取
  • 考慮升級至 Nginx/Varnish 獲取更高併發效能,Squid 勝在成熟的快取階層協定 (ICP/HTCP/HTCP) 與 Windows 原生支援

結語

Windows 版 Squid 完整支援反向代理、快取、負載平衡、多層叢集,配置檔語法與 Linux 版一致,遷移零成本。掌握 cache_peer、cache_peer_domain、vhost、round-robin 核心指令,即可應對從單站點到多層 CDN 的各類部署需求。

Copyright © Windows教程網 All Rights Reserved