【问题标题】:Forwarding traffic from 80 to 8080 [closed]将流量从 80 转发到 8080 [关闭]
【发布时间】:2017-05-07 07:19:32
【问题描述】:

我用 puppet 安装了 tomcat。它在标准 8080 端口上运行。 tomcat 进程以 tomcat 用户身份启动。我想将所有流量从端口 80 重定向到 8080。我的 iptables 设置如下所示:

纳特:

# iptables -L -t nat
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
REDIRECT   tcp  --  anywhere             anywhere             multiport dports http /* 099 forward port 80 to 8080 */ redir ports 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   

标准 iptables:

# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     icmp --  anywhere             anywhere             /* 000 accept all icmp */
ACCEPT     all  --  anywhere             anywhere             /* 001 accept all to lo interface */
REJECT     all  --  anywhere             loopback/8           /* 002 reject local traffic not on loopback interface */ reject-with icmp-port-unreachable
ACCEPT     all  --  anywhere             anywhere             /* 003 accept related established rules */ state RELATED,ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere             multiport dports ssh /* 004 accept ssh */
ACCEPT     tcp  --  anywhere             anywhere             multiport dports http,https /* 100 allow http and https access */
DROP       all  --  anywhere             anywhere             /* 999 drop all */

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination  

我看到netstat显示tomcat进程正在监听8080端口:

# netstat -tulpn | grep 80
tcp6       0      0 :::8080                 :::*                    LISTEN      16273/java      
tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN      16273/java      
tcp6       0      0 :::8009                 :::*                    LISTEN      16273/java  

你的 80 端口上似乎没有任何东西在监听。 telnet 到那台机器的 80 端口和 8080 拥抱。

如何将所有流量从 80 转发到 8080?

【问题讨论】:

    标签: tomcat iptables


    【解决方案1】:

    试试这个:

    sudo iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
    sudo iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
    

    并检查流量:

    sudo tcpdump -i any -n port 80
    

    如果你看不到数据包,你应该检查外部防火墙。

    【讨论】:

    • 你能告诉我为什么我的设置不起作用吗?
    • 我认为您的 INPUT 过滤器在最后一条规则 DROP all -- anywhere anywhere 处丢弃了端口 8080 数据包。所以我在第一个位置插入 ACCEPT 规则。您可以通过iptables -Ziptables -nv查看匹配规则
    • 有没有办法禁止网络客户端直接访问8080端口,但总是通过80端口?
    【解决方案2】:

    我已经用了很多年了:

    iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080
    

    但是,重要:这只适用于来自网络中其他主机的流量。即,您不能这样测试:

    curl localhost:8080
    

    也没有

    curl <<same-host-ip>>:8081  (the host that has the iptables configured)
    

    要检查此配置,您需要在其他主机中。

    看你的配置好像不需要另外的iptables规则了。

    【讨论】:

      【解决方案3】:

      试试这个:

      iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 127.0.0.1:8080
      

      请确保您的规则不会导致其他规则出现问题。所以可以肯定的是,清理所有东西,启动它并测试。如果可行,请添加您的其他规则。

      【讨论】:

      • 不,它没有帮助。在运行您的命令之前,我已经执行了 iptables -T nat --flushiptables --flush 来清理 iptables 一段时间。清理后的 iptables 8080 可以访问,但 80 仍然无法访问。
      猜你喜欢
      • 2014-09-11
      • 1970-01-01
      • 2012-01-10
      • 1970-01-01
      • 2015-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多