You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VPS上Docker容器仅本地可访问问题求助(附nftables配置)

问题排查与解决

1. 开放公网接口的8080端口

你的nftables input链默认策略为drop,但仅允许了公网接口eth0上的TCP 51829和UDP 51821端口,Docker映射的8080端口未被纳入允许规则,导致外部公网流量被拦截。需在input链中添加对应规则:

在iif $pub_iface udp dport $wg_port accept行后新增:

iif $pub_iface tcp dport 8080 accept

修改后的完整input链:

chain input {
        type filter hook input priority 0; policy drop;
        ct state { established, related } accept
        iif lo accept
        ip protocol icmp accept
        ip6 nexthdr ipv6-icmp accept
        meta l4proto ipv6-icmp accept
        iif $pub_iface tcp dport 51829 accept
        iif $pub_iface udp dport $wg_port accept
        iif $pub_iface tcp dport 8080 accept
        iifname $wg_iface accept
        ct state invalid drop
        reject
}

2. 确保Docker网络转发规则兼容nftables

Docker默认依赖iptables管理网络规则,若系统优先使用nftables,需做以下适配:

方式一:配置Docker直接使用nftables

编辑Docker配置文件/etc/docker/daemon.json,添加:

{
  "iptables": false,
  "nftables": true
}

重启Docker服务生效:

systemctl restart docker

方式二:在nftables forward链中放行Docker网桥流量

若不想修改Docker配置,可在forward链中添加允许Docker网桥进出流量的规则:
在ct state { established, related } accept行后新增:

iifname "docker0" accept
oifname "docker0" accept

修改后的完整forward链:

chain forward {
        type filter hook forward priority 0; policy drop;
        ct state { established, related } accept
        iifname "docker0" accept
        oifname "docker0" accept
        iifname $wg_iface oifname $wg_iface accept
        iifname $wg_iface oifname $pub_iface accept
        ct state invalid drop
        reject with icmpx type host-unreachable
}

3. 验证修改

  1. 重新加载nftables配置:
nft -f /etc/nftables.conf
  1. 重启Docker容器确保端口映射生效:
docker restart apache-server
  1. 从外部机器测试访问http://[你的VPS公网IP]:8080

核心原因总结

  • 公网端口未显式放行:nftables的input链默认拒绝所有流量,必须手动添加允许8080端口的规则。
  • Docker网络转发受限:容器流量需通过网桥转发,nftables的forward链默认拒绝未授权流量,需放行Docker网桥的相关流量或让Docker适配nftables。

内容的提问来源于stack exchange,提问作者Jonathan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 13:30:41