首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

Windows 環境下使用 Squid 搭建高效代理伺服器完整指南

     編輯:PHP 相關知識

前言

Squid 是一款功能強大的開源代理伺服器軟體,廣泛用於網頁緩存、訪問控制與流量優化。本文將詳細介紹如何在 Windows 系統上部署 Squid,配置反向代理與緩存加速,並提供完整的安裝、配置與測試步驟。

實驗環境規劃

  • Squid 伺服器:172.20.65.201,監聽 80 端口
  • 後端 Web 伺服器:172.20.65.203,運行於 80 端口
  • 測試客戶端:172.20.65.231

配置文件中 # 代表註釋,啟用選項需移除前綴 #;TAG 標籤對應各功能項設定。

一、下載與安裝

1. 獲取安裝包

前往官方或可信鏡像站下載 Squid for Windows(本教程以 Squid 2.7.STABLE6 為例,不同版本配置細節可能略有差異)。

2. 解壓部署

將壓縮包解壓至 C:\squid(或其他磁碟根目錄),目錄結構保持完整。

二、建立緩存與日誌目錄

為提升 I/O 性能,建議將緩存與日誌放置於大容量磁碟(如 D 盤):

D:\squid\var\logs   # 存放存取日誌、錯誤日誌
D:\squid\var\cache  # 存放磁碟緩存資料

三、設定目錄權限

建立專用服務帳號(如 squidApp),歸屬於標準用戶組,授予 D:\squid 目錄完整讀寫權限,確保 Squid 進程能正常寫入緩存與日誌。

四、配置文件準備

進入 C:\squid\etc,先備份原始配置,再重命名預設文件:

  • squid.conf.default → squid.conf
  • mime.conf.default → mime.conf
  • cachemgr.conf.default → cachemgr.conf

五、核心配置詳解

1. 訪問控制列表 (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.16.65.231/255.255.0.0  # 指定內網網段

2. 端口與協定定義

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

3. 訪問權限規則(順序敏感)

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

4. 監聽端口與加速模式

http_port 80 vhost  # 80 端口啟用 vhost 加速模式

預設 HTTP 端口為 3128,可在配置文件搜尋 http_port 確認。

5. 父節點(後端伺服器)配置

cache_peer 172.20.65.203 parent 80 0 no-query originserver
  • parent:指定為父節點
  • 80:後端 Web 伺服器端口
  • 0:ICP 服務端口(0 表示不使用)
  • no-query originserver:直接從源站獲取內容,不發送 ICP 查詢

6. 緩存參數調優

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

7. 日誌配置

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

8. 緩存刷新策略

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

9. 管理員郵箱與本地解析

cache_mgr webmaster@breeze
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

此步驟會建立 cache_dir 指定的目錄結構,首次啟動前必須執行。

3. 啟動服務

打開 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:

  • TCP_DENIED:配置錯誤(權限、ACL 不通)
  • TCP_MEM_HIT / 200:命中內存緩存,響應極快
  • TCP_HIT:命中磁碟緩存
  • TCP_MISS:未命中,回源獲取

常用分析命令:

# 查看內存命中
grep TCP_MEM_HIT /path/to/access.log

# 查看磁碟命中
grep TCP_HIT /path/to/access.log

# 查看回源請求
grep TCP_MISS /path/to/access.log

九、進階技巧:日誌輪替與中文錯誤頁

1. 定時輪替日誌

編輯 crontab(Linux 環境下),每週六凌晨 2 點執行:

0 2 * * 6 /usr/local/squid/sbin/squid -k rotate

Windows 可使用任務計劃程序達成相同效果。

2. 啟用中文錯誤提示

error_directory /usr/share/squid/errors/Simplify_Chinese

總結

本文涵蓋了 Windows 下 Squid 代理伺服器的完整部署流程:從下載、安裝、目錄規劃、權限設定、核心配置(ACL、端口、父節點、緩存策略、日誌)、服務安裝、初始化、啟動驗證到日常維護命令。掌握這些步驟,即可快速構建高性能的正向/反向代理服務,實現訪問加速、帶寬節省與訪問控制。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved