如何将外部 TCP 端口 80 路由到环回 (127.0.0.1) 端口 8080?

gri*_*yvp 3 linux firewall iptables port-forwarding

我有一个具有真实 IP 的测试 VDS 盒子。如果我在物理接口端口 80 上启动 Web 服务器,则可以通过其 IP 地址(和默认端口 80)从另一台计算机打开它:

python -m SimpleHTTPServer 80
Run Code Online (Sandbox Code Playgroud)

但是,如果我尝试将端口 80 从物理接口重新定向eth0到环回 127.0.0.1 端口 8080,我可以从端口 8080 上的另一台计算机连接它,但无法在端口 80 上连接,这只是无休止的“连接”。似乎没有发生重定向:

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to-destination 127.0.0.1:8080
python -m SimpleHTTPServer 8080
Run Code Online (Sandbox Code Playgroud)

我做错了什么?:(

PS “127.0.0.1:8080”上的绑定服务器产生相同的结果,但这并不重要,因为在“0.0.0.0:8080”上运行的服务器将接受重定向到“127.0.0.1:8080”的连接。AFAIK。:(

iptables -L结果:

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
Run Code Online (Sandbox Code Playgroud)

iptables -t nat -L结果:

Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
DNAT       tcp  --  anywhere             anywhere             tcp dpt:http to:127.0.0.1:8080

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination
Run Code Online (Sandbox Code Playgroud)

netstat -nlp结果:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      676/sshd        
tcp6       0      0 :::22                   :::*                    LISTEN      676/sshd        
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name    Path
unix  2      [ ACC ]     STREAM     LISTENING     7964     600/acpid           /var/run/acpid.socket
unix  2      [ ACC ]     STREAM     LISTENING     6590     1/init              @/com/ubuntu/upstart
unix  2      [ ACC ]     SEQPACKET  LISTENING     6760     231/udevd           /run/udev/control
unix  2      [ ACC ]     STREAM     LISTENING     7030     345/dbus-daemon     /var/run/dbus/system_bus_socket
Run Code Online (Sandbox Code Playgroud)

ifconfig -a结果:

eth0      Link encap:Ethernet  HWaddr 00:16:3e:da:1a:98  
          inet addr:5.14.223.181  Bcast:5.14.223.255  Mask:255.255.255.0
          inet6 addr: fe80::140:3eff:febe:201a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:371264 errors:0 dropped:59 overruns:0 frame:0
          TX packets:2093 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:17377545 (17.3 MB)  TX bytes:214428 (214.4 KB)
          Interrupt:25 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:10 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:552 (552.0 B)  TX bytes:552 (552.0 B)
Run Code Online (Sandbox Code Playgroud)

Nap*_*r_X 5

只需用此规则替换您的规则即可。

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
Run Code Online (Sandbox Code Playgroud)

它应该有效。这会将 eth0 上的所有 80 端口流量重定向到运行 tomcat 的本地主机的 8080 端口。


在没有 iptables 的情况下执行此操作的另一种方法(因为我什至不确定是否可以使用 iptables)是使用 xinetd 服务。要使用它,请在您的计算机上安装 xinetd(通常是默认安装的)。创建一个像这样的文件:

 # vim /etc/xinted.d/tomcat
Run Code Online (Sandbox Code Playgroud)

将此内容放入文件中:

service tomcat
{
    socket_type             = stream
        wait                    = no
        user                    = root
        redirect                = 127.0.0.1 8080
        bind                    = 10.31.33.101 80
}
Run Code Online (Sandbox Code Playgroud)

只需重新启动 xinted 服务即可。

 # service xinetd restart
Run Code Online (Sandbox Code Playgroud)

它会像魅力一样发挥作用。