如何在Linux中設定透明代理

來源:互聯網
上載者:User

  1.什麼是透明代理?

  如果你問:我如何才能使得使用者的瀏覽器不需要任何代理設定就能使用我的Squid cacheProxy 伺服器上網?此時你就需要使用透明代理。透明代理讓你的用戶端不需設定任何代理,當包經過透時Proxy 伺服器時實際上被重新導向到squidProxy 伺服器的代理連接埠(如8080),即由本地Proxy 伺服器向外請求所需資料然後拷貝給用戶端。
  2.我需要什麼樣的環境才能實現透明代理?

  a.用戶端的windows PC的網關必須設成SquidProxy 伺服器,因為既然你的browser中沒有任何代理設定,你要訪問某個網站時,包必須經經過squidProxy 伺服器才能被重新導向,故這是最基本的條件。

  b.用戶端必須正確設定DNS伺服器。因為既然現在不用設定任何代理。則DNS必須由browser來解析,也就是要由用戶端的PC中TCP/IP中設定的DNS伺服器來正確解析出某個網站的IP地址來。

  c.伺服器端可以安裝squidProxy 伺服器,1.x or 2.x版本均可。

  3.配置Squid代理,啟動透明代理功能

  Squid-2

  加下面的行到你的/etc/squid/squid.conf中

  http_port 8080
  httpd_accel_host virtual
  httpd_accel_port 80
  httpd_accel_with_proxy on
  httpd_accel_uses_host_header on

 Squid-1.1

  加下面的行到/etc/squid.conf

  http_port 8080
  httpd_accel virtual 80
  httpd_accel_with_proxy on
  httpd_accel_uses_host_header on

  4. 重啟動squid. 用下面的命令:

    #/usr/sbin/squid -k reconfigure

  如提示核心不支援透明代理。則你需要重新編譯核心,enable 透明代理的支援。

  下面是你需要啟動的核心項目:

  [*] Network firewalls
  [ ] Socket Filtering
  [*] Unix domain sockets
  [*] TCP/IP networking
  [ ] IP: multicasting
  [ ] IP: advanced router
  [ ] IP: kernel level autoconfiguration
  [*] IP: firewalling
  [ ] IP: firewall packet netlink device
  [*] IP: always defragment (required for masquerading)

  [*] IP: transparent proxy support

  5. 下面的命令針對Linux 2.2.x核心:

  # Accept all on lookback
  /sbin/ipchains -A input -j ACCEPT -i lo
  #Accept my own IP, to prevent loops (repeat for each interface/alias)
  /sbin/ipchains -A input -j ACCEPT -p tcp -d 192.168.11.1/32 80
  #Send all traffic destined to port 80 to Squid on port 80
  /sbin/ipchains -A input -j REDIRECT 8080 -p tcp -s 192.168.11.0/24 -d 0/0 80

  下面的命令針對Linux 2.0.x核心:

  # Accept all on loopback
  ipfwadm -I -a accept -W lo
  # Accept my own IP, to prevent loops (repeat for each interface/alias)
  ipfwadm -I -a accept -P tcp -D 192.168.11.1/32 80
  # Send all traffic destined to port 80 to Squid on port 3128
  ipfwadm -I -a accept -P tcp -S 192.168.11.0/24 -D 0/0 80 -r 8080

  6.應注意的問題:

  a. 這種透明代理只能針對http協議,不能針對FTP協議
  b. PC的預設閘道應設成squid Proxy 伺服器
  c. 防火牆重新導向規則在其它的input規則的前面,注意順序。

  如:

    /etc/rc.d/rc.firewall:

#!/bin/sh
# rc.firewall Linux kernel firewalling rules
FW=/sbin/ipfwadm

# Flush rules, for testing purposes
for i in I O F # A # If we enabled accounting too
do
${FW} -$i -f
done

# Default policies:
${FW} -I -p rej # Incoming policy: reject (quick error)
${FW} -O -p acc # Output policy: accept
${FW} -F -p den # Forwarding policy: deny

# Input Rules:

# Loopback-interface (local access, eg, to local nameserver):
${FW} -I -a acc -S localhost/32 -D localhost/32

# Local Ethernet-interface:

# Redirect to Squid proxy server:
${FW} -I -a acc -P tcp -D default/0 80 -r 8080

# Accept packets from local network:
${FW} -I -a acc -P all -S localnet/8 -D default/0 -W eth0

# Only required for other types of traffic (FTP, Telnet):

# Forward localnet with masquerading (udp and tcp, no icmp!):
${FW} -F -a m -P tcp -S localnet/8 -D default/0
${FW} -F -a m -P udp -S localnet/8 -D default/0

Here all traffic from the local LAN with any destination gets redirected to the
local port 8080. Rules can be viewed like this:

IP firewall input rules, default policy: reject
type prot source destination ports
acc all 127.0.0.1 127.0.0.1 n/a
acc/r tcp 10.0.0.0/8 0.0.0.0/0 * -> 80 => 8080
acc all 10.0.0.0/8 0.0.0.0/0 n/a
acc tcp 0.0.0.0/0 0.0.0.0/0 * -> *

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.