IPTABLES:将传出的 http 请求路由到 eth1

IPTABLES:将传出的 http 请求路由到 eth1

我正在尝试路由所有离开我的服务器的传出 HTTP 请求,使其通过另一台服务器上的代理 (tinyproxy)。我希望到代理的传输使用位于 eth1 上的 LAN。无论我如何尝试,请求都会从 eth0 发出。

我制定了这条规则:

 iptables -t nat -A OUTPUT -p tcp --dport 80 -j DNAT -o eth1 --to-destination 123.123.123.123:8888

但是,这不管用。请求通过 eth0 发出。我知道我在这里遗漏了一些重要的东西,但我没有主意。任何帮助都将不胜感激。

配置:

    eth0      Link encap:Ethernet  HWaddr 04:01:33:0E:
      inet addr:111.111.111.111  Bcast:111.111.111.255     Mask:255.255.255.0
      inet6 addr: fe80::601:33ff:121:4f01/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:1068434 errors:0 dropped:0 overruns:0 frame:0
      TX packets:499051 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000
      RX bytes:1087084291 (1.0 GiB)  TX bytes:104089629 (99.2 MiB)

eth1      Link encap:Ethernet  HWaddr 04:01:33:0E:
      inet addr:10.11.10.10  Bcast:10.11.255.255  Mask:255.255.0.0
      inet6 addr: fe80::601:33ff:121:4f02/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:8243 errors:0 dropped:0 overruns:0 frame:0
      TX packets:4746 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000
      RX bytes:10948267 (10.4 MiB)  TX bytes:633381 (618.5 KiB)

 route -n; cat /etc/network/interfaces
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
162.243.xx.xx   0.0.0.0         255.255.255.0   U     0      0        0 eth0
10.128.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1
0.0.0.0         162.243..xx.xx   0.0.0.0         UG    0      0        0 eth0
cat: /etc/network/interfaces: No such file or directory


iptables -t nat -vnL
Chain PREROUTING (policy ACCEPT 5983 packets, 352K bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain POSTROUTING (policy ACCEPT 15734 packets, 1020K bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 4693 packets, 331K bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 ACCEPT     all  --  *      *       0.0.0.0/0            162.243..xx.xx
 3550  239K ACCEPT     all  --  *      *       0.0.0.0/0            127.0.0.1
    0     0 ACCEPT     all  --  *      *       0.0.0.0/0            10.128.95.89
 7491  449K DNAT       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:80 to:10.128.3.132:8888

答案1

这是你的问题:根据你的配置,两个网卡都使用 Metric:1,如果你想使用 ETH1 作为主网卡,将度量标准更改为 0

您的配置:

eth0 链路封装:以太网 HWaddr 04:01:33:0E:UP 广播运行多播 MTU:1500 度量:1

eth1 链路封装:以太网 HWaddr 04:01:33:0E:UP 广播运行多播 MTU:1500 度量:1

答案2

您的路由表在这里发挥作用。

您的代理位于一个可路由到 Internet 的 IP 地址上。您的eth0接口也配置了一个可路由到 Internet 的 IP 地址,可能有一个指向您的 WAN 网关(Comcast、AT&T 等)的默认路由。您的eth1接口配置了一个不可路由到 Internet 的 IP 地址。发生的情况是,您的路由表通过接口看到到您的目标地址(代理服务器)的路由eth0,因此数据包沿着该路由传输。

我敢打赌你的 iptables 规则没有看到任何命中,这解释了原因。你试图在当前配置中完成什么需要eth0面。

希望这可以帮助

相关内容