【问题标题】:Can't get nftables to redirect port 80 to 8080无法让 nftables 将端口 80 重定向到 8080
【发布时间】:2016-11-08 21:26:07
【问题描述】:

我尝试设置我的服务器,以便将端口 80 的流量重定向到端口 8080,但它不起作用。 (如果我 telnet 到端口 80,我会收到“连接被拒绝”错误,并且使用 firefox 会出现“无法连接”。)

我已经能够使用 iptables 让它工作,但更喜欢使用 nftables。有人知道问题可能是什么吗? (如果相关,服务器在 linode.com 上运行,内核由 linode 提供。)

我在 /etc/nftables.conf 中有以下内容:

#!/usr/sbin/nft -f

flush ruleset

table ip fw {
        chain in {
                type filter hook input priority 0;

                # accept any localhost traffic
                iif lo accept

                # accept traffic originated from us
                ct state established,related accept

                # accept ssh, alternative http
                tcp dport { ssh, http, http-alt } ct state new counter accept

                counter drop
        }
}

table ip nat {
        chain prerouting {
                type nat hook prerouting priority 0;
                tcp dport http redirect to http-alt
        }

        chain postrouting {
                type nat hook postrouting priority 0;
        }
}

【问题讨论】:

    标签: linux redirect iptables nat netfilter


    【解决方案1】:

    如果您仅在本地主机上进行路由,请尝试使用

    table ip nat {
       chain output {
          type nat hook output priority 0;
          tcp dport http redirect to http-alt
       }
    }
    

    几年前,我在 iptables 中读到循环设备上的数据包不会遍历预路由链,而是通过输出链。那是我的问题。

    【讨论】:

    • 最后一行将变为ip daddr 127.0.0.1 tcp dport http redirect to http-alt,例如,如果您只想重定向定向到 127.0.0.1 的数据包,这将允许使用http://localhost/ 而不是http://localhost:8080/
    【解决方案2】:

    您是说table inet filter 而不是table ip fw

    如果是这样,我遇到了类似的问题。将 ip nat prerouting 优先级更改为 -101 可以正常工作,但我不知道为什么。它可能与NF_IP_PRI_NAT_DST (-100): destination NAT 的默认优先级有关。唯一有效的范围是 -101 到 -200。

    #!/usr/sbin/nft -f
    
    flush ruleset
    
    table inet filter {
       chain input {
          type filter hook input priority 0;
          counter
    
          # accept any localhost traffic
          iif lo accept
    
          # accept traffic originated from us
          ct state {established,related} accept
    
          # activate the following line to accept common local services
          tcp dport { 22, 80, 443, 9443 } ct state new accept
    
          # accept neighbour discovery otherwise IPv6 connectivity breaks.
          ip6 nexthdr icmpv6 icmpv6 type { nd-neighbor-solicit,  nd-router-advert, nd-neighbor-advert } accept
    
          # count and drop any other traffic
          counter drop
       }
    }
    
    table ip nat {
    
       chain input {
          type nat hook input priority 0;
          counter
       }
    
       chain prerouting {
          type nat hook prerouting priority -101;
          counter
          tcp dport 443 counter redirect to 9443
       }
    
       chain postrouting {
          type nat hook postrouting priority 0;
          counter
       }
    }
    

    counter 规则可以很容易地查看链是否正在处理;计数器值可以通过nft list ruleset查看。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-14
      • 1970-01-01
      • 2015-06-12
      • 1970-01-01
      • 2012-09-29
      相关资源
      最近更新 更多