【问题标题】:NAT port forwarding not working, while ssh tunnel doesNAT 端口转发不起作用,而 ssh 隧道起作用
【发布时间】:2019-03-11 15:47:15
【问题描述】:

我有一个 LXC 容器 apache2 安装(容器在 192.168.122.179:80 上监听)。

主机是 10.138.141.216。

如果我通过 ssh 隧道: ssh -L 45678:192.168.122.179:80 myuser@10.138.141.216 从我的电脑我得到 apache2 页面(浏览器到http://localhost:45678

但是如果我断开 ssh 隧道并且只使用 iptables http://10.138.141.216:45678 将不起作用:

[~]$ sudo iptables -t nat -L -n -v
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    9   564 DNAT       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:45678 to:192.168.122.179:80

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 RETURN     all  --  *      *       192.168.122.0/24     224.0.0.0/24
    2   656 RETURN     all  --  *      *       192.168.122.0/24     255.255.255.255
  606 36360 MASQUERADE  tcp  --  *      *       192.168.122.0/24    !192.168.122.0/24     masq ports: 1024-65535
    0     0 MASQUERADE  udp  --  *      *       192.168.122.0/24    !192.168.122.0/24     masq ports: 1024-65535
  725  101K MASQUERADE  all  --  *      *       192.168.122.0/24    !192.168.122.0/24

每当我在浏览器中刷新页面时,我都可以看到 pkts 和字节数发生变化,但我得到的只是“无法访问此站点”。

所以 ssh 隧道可以工作,而 iptables 规则不...

更新:

使用tcpdump我可以看到“tcp port eba unreachable”,为什么???

14:46:40.672318 IP 10.79.41.37.57504 > mymachine.eba: Flags [S], seq 2646922897, win 8192, options [mss 1360,nop,wscale 8,nop,nop,sackOK,unknown-33 0x21cc167ee1203a070000], length 0
14:46:40.672402 IP mymachine > 10.79.41.37: ICMP mymachine tcp port eba unreachable, length 72

wget http://192.168.122.179 在主机内部工作...

【问题讨论】:

    标签: apache iptables portforwarding lxc


    【解决方案1】:

    Apache 通常由主机配置。每个主机都监听来自特定域名的请求。

    为了使您的主机可以访问,您需要侦听 IP 本身或设置主机名或在 Apache 主机配置中设置默认主机。

    您可以使用 wget 或 curl 在 localhost 上执行 get 请求来测试您的主机配置是否有效。

    如果 Apache 工作正常,请确保没有任何冲突的 iptables 规则。

    【讨论】:

    • 但是我在 apache 配置文件中有 和“Listen 80”,那不是监听所有接口吗?
    • 您可以使用 curl 对其进行测试,从您的 ssh 连接中向 localhost 发送 GET 请求。
    • 我认为这不起作用,因为 iptables 不应该在环回接口上工作?
    • 这就是重点;测试您从 apache 获得的响应。如果您的 apache 确实有效。接下来的事情就是通过你的 iptables 得到它。
    【解决方案2】:

    我不知道为什么,但 LXC 默认添加了这个规则:

    REJECT     all  --  *      virbr0  0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable
    REJECT     all  --  virbr0 *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable
    

    这些在使用时是不可见的

    iptables -t any -L -n -v

    删除“-t any”显示它们。 删除这些规则就行了。

    【讨论】:

      猜你喜欢
      • 2015-04-02
      • 2017-06-01
      • 2016-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-09
      • 2023-04-05
      • 2017-01-24
      相关资源
      最近更新 更多