萬盛學電腦網

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

CentOS 6 部署 Squid 透明代理完整實戰筆記

環境與前置作業

  • 虛擬機:VirtualBox + CentOS 6.0
  • Squid 版本:squid-3.1.4-1.el6.i686
  • 雙網卡橋接模式:eth0(外網 172.17.1.221/24)、eth1(內網 192.168.1.254/24)

關鍵提醒:/etc/sysconfig/network-scripts/ifcfg-eth* 中的 HWADDR 必須與虛擬機網卡 MAC 完全一致,否則網卡無法啟動。

Squid 核心配置

  1. 備份原配置:cp /etc/squid/squid.conf /etc/squid/squid.conf.bak
  2. 開放所有存取(生產環境請限定 ACL):
    http_access allow all
  3. 必加 visible_hostname localhost 於檔案末尾,否則服務無法啟動
  4. 啟用透明模式:
    http_port 3128 transparent

啟動與防火牆轉發腳本

service squid restart

建立 squid.sh 自動化 iptables 規則:

#!/bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
modprobe iptable_nat
/sbin/iptables -t nat -A POSTROUTING -j MASQUERADE
iptables -t nat -A PREROUTING -p udp --dport 53 -j DNAT --to 172.16.5.133
iptables -t nat -A PREROUTING -i eth1 -p tcp -s 192.168.1.0/24 --dport 80 -j REDIRECT --to-ports 3128

執行並持久化:

chmod +x squid.sh
./squid.sh
service iptables save
service iptables restart

客戶端驗證

項目設定值
IP192.168.1.x/24
閘道192.168.1.254
DNS192.168.1.254

瀏覽器不設定代理直接訪問 www.jb51.net,若能打開即透明代理成功。

非透明模式測試備註

IE → 工具 → 網際網路選項 → 連線 → 區域網路設定 → 代理伺服器:
IP: 192.168.1.254、Port: 3128。

copyright © 萬盛學電腦網 all rights reserved