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

Linux Squid 代理伺服器安裝與配置實戰:CentOS 5.9 環境

編輯:代理伺服器

環境資訊

CentOS release 5.9 (Final)
Kernel: 64-bit
yum 可用

一、安裝與服務啟用

# 安裝
yum -y install squid

# 設定開機自啟 (Runlevel 3/5)
chkconfig --level 35 squid on

# 初始化快取目錄 (首次必須)
squid -z

# 啟動服務
/etc/init.d/squid start

# 備份原始配置
cp /etc/squid/squid.conf /etc/squid/squid.conf.bak

二、核心配置修改 (/etc/squid/squid.conf)

使用 vim /etc/squid/squid.conf 編輯,關鍵行號參考 (版本略有差異,請搜尋關鍵字定位):

1. 限制允許使用代理的客戶端 IP (約 570 行)

# 定義 ACL:名稱為 xtone,來源 IP 為公司公網出口
acl xtone src 125.77.254.58/255.255.255.255

2. 允許該 ACL 存取 (約 636 行)

http_access allow xtone

3. 重啟生效

/etc/init.d/squid restart

三、支援 FTP 用戶端 CONNECT 代理 (約 620 行)

預設拒絕非 SSL 埠的 CONNECT,導致 FTP 軟體無法透過代理連線。註解掉:

# http_access deny CONNECT !SSL_ports

重啟 Squid 後,FTP Client 設定 HTTP/HTTPS Proxy 即可透過 CONNECT 指令建立 FTP 控制連線。

四、驗證與常用指令

  • 測試:瀏覽器設定 Proxy 指向 Squid IP:3128 (預設埠) → 訪問外網
  • 查看存取日誌:tail -f /var/log/squid/access.log
  • 查看快取命中:日誌中 TCP_HIT (記憶體命中)、TCP_MEM_HIT、TCP_REFRESH_HIT 等
  • 重新載入配置 (不重啟):squid -k reconfigure

五、進階擴充建議

  • 認證機制:配合 auth_param basic program /usr/lib/squid/ncsa_auth /etc/squid/passwd 實現帳號密碼驗證
  • 上游代理:cache_peer parent.proxy.com parent 8080 0 no-query default
  • 反向代理/快取:參考 Windows 版教學,cache_peer + vhost + cache_peer_domain
  • 透明代理:需 iptables REDIRECT 配合 http_port 3128 transparent
  • 效能調優:cache_mem、cache_dir、maximum_object_size 依流量與磁碟調整

六、結語

CentOS 下 yum install squid 一鍵部署,核心在於 acl + http_access 實現精細存取控制。生產環境建議:
✅ 僅允許授權 IP/帳號存取
✅ 關閉非必要埠 (如 ICP/HTCP 對外)
✅ 定期分析日誌偵測異常
✅ 配置日誌輪轉防止磁碟爆滿
✅ 重要節點部署雙機熱備 (Heartbeat/Corosync + VIP)

Copyright © Windows教程網 All Rights Reserved