萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 伺服器教學

搭建 approx 快取代理,輕鬆解決內網多台 Ubuntu 系統更新頻寬與速度難題

管理多台內網 Ubuntu 機器時,每台都直接從外部鏡像站下載更新套件,不僅消耗寶貴的出口頻寬,速度也較慢。approx 是一個專為 Debian/Ubuntu 設計的 HTTP 套件快取代理,它能在第一台機器請求套件時從上游鏡像站下載並快取,後續機器直接從本地代理獲取,大幅節省頻寬與時間。自 Ubuntu 10.04 起,apt-proxy 已不再維護,approx 成為官方推薦替代方案。

approx 核心優勢

  • 按需下載、本地快取: 同一套件無論多少客戶端安裝,只從外網下載一次。
  • 簡化客戶端管理: 變更上游鏡像只需改約伺服器端 /etc/approx/approx.conf,客戶端 sources.list 保持指向代理即可。
  • 支援多種儲存庫: 主倉庫、安全更新、合作夥伴、Extras 等均可代理。
  • 磁碟佔用可控: 快取通常僅需數 GB 空間,可透過配置限制大小。

伺服器端部署 (Ubuntu 12.04 LTS 為例)

1. 安裝 approx

sudo apt-get update && sudo apt-get install approx

2. 規劃上游鏡像來源

從現有機器的 /etc/apt/sources.list 提取不重複的鏡像站域名:

grep -E "^[^#]" /etc/apt/sources.list | cut -d " " -f2 | uniq
# 輸出範例:
# https://archive.ubuntu.com/ubuntu/
# https://security.ubuntu.com/ubuntu

3. 配置 /etc/approx/approx.conf

定義代理映射關係(左側為代理路徑片段,右側為上游完整 URL):

ubuntu          https://cn.archive.ubuntu.com/ubuntu
ubuntu-extras   https://extras.ubuntu.com/ubuntu
ubuntu-partner  https://archive.canonical.com/ubuntu
ubuntu-security https://security.ubuntu.com/ubuntu

說明: 客戶端請求 https://approx-server:9999/ubuntu/... 時,approx 會轉向 https://cn.archive.ubuntu.com/ubuntu/...。

4. 重啟 inetd 使配置生效

sudo /etc/init.d/openbsd-inetd restart
# 或 systemctl restart openbsd-inetd (新版本)

服務將在 TCP 9999 埠 監聽。可用 netstat -ntlp | grep 9999 驗證。

客戶端配置

編輯每台客戶端的 /etc/apt/sources.list,將所有 deb / deb-src 行的主機部分替換為 approx 伺服器位址:

deb https://approx-server:9999/ubuntu/ precise main restricted
deb-src https://approx-server:9999/ubuntu/ precise main restricted

deb https://approx-server:9999/ubuntu/ precise-updates main restricted
deb-src https://approx-server:9999/ubuntu/ precise-updates main restricted

deb https://approx-server:9999/ubuntu/ precise universe
deb-src https://approx-server:9999/ubuntu/ precise universe

deb https://approx-server:9999/ubuntu/ precise-updates universe
deb-src https://approx-server:9999/ubuntu/ precise-updates universe

deb https://approx-server:9999/ubuntu/ precise multiverse
deb-src https://approx-server:9999/ubuntu/ precise multiverse

deb https://approx-server:9999/ubuntu/ precise-updates multiverse
deb-src https://approx-server:9999/ubuntu/ precise-updates multiverse

deb https://approx-server:9999/ubuntu/ precise-backports main restricted universe multiverse
deb-src https://approx-server:9999/ubuntu/ precise-backports main restricted universe multiverse

deb https://approx-server:9999/ubuntu precise-security main restricted
deb-src https://approx-server:9999/ubuntu precise-security main restricted

deb https://approx-server:9999/ubuntu precise-security universe
deb-src https://approx-server:9999/ubuntu precise-security universe

deb https://approx-server:9999/ubuntu precise-security multiverse
deb-src https://approx-server:9999/ubuntu precise-security multiverse
  • 將 approx-server 替換為約伺服器實際主機名或 IP。
  • 若內網有 DNS,建議在 /etc/hosts 或 DNS 中添加 approx-server 解析。
  • 原官方鏡像站行可註釋或刪除。

驗證與日常維護

  1. 客戶端執行 sudo apt-get update,觀察是否從 https://approx-server:9999/... 獲取套件清單。
  2. 在 approx 伺服器查看 /var/log/approx.log 確認請求轉發與快取命中情況。
  3. 定期清理舊快取:sudo approx-cache-clean (若可用) 或手動刪除 /var/cache/approx/ 下舊版本套件。
  4. 磁碟空間不足時,可調整 approx.conf 中的快取大小限制參數。

進階技巧

  • HTTPS 上游: 若上游強制 HTTPS,approx 1.6+ 版本支援 https:// 協定的上游 URL。
  • 認證代理: 若上游需認證,可在 URL 中嵌入 user:pass@。
  • 高可用: 部署兩台 approx 伺服器,客戶端配置多個代理位址或使用 DNS 輪詢。

透過 approx,內網 Ubuntu 群集的系統更新將變得高效、經濟且易於維護,是中小型 Linux 環境的必備基建元件。

copyright © 萬盛學電腦網 all rights reserved