我遇到了一个让我困惑的网络问题。情况是这样的:我无法从外部访问端口 80 和 433,但我可以转发 Docker 中的任何其他端口,并且它运行完美!我已经设置了 UFW 并安装了 K3s,但我的大部分服务都是使用 Docker Compose 运行的。
为了进一步调查,我启动了一个 Nginx 测试容器,事情变得更加奇怪。
我可以使用其内部 Docker IP 访问这个测试容器,但即使是简单的curl -I 127.0.0.1
操作也会导致 404 错误:
-> # curl -I 127.0.0.1
HTTP/1.1 404 Not Found
Content-Type: text/plain; charset=utf-8
X-Content-Type-Options: nosniff
Date: Wed, 20 Sep 2023 00:39:26 GMT
Content-Length: 19
当我发现我可以从另一个内部 IP(VPN 网络)访问它时,情节变得更加复杂:
-> # curl -I 192.168.10.1
HTTP/1.1 200 OK
Server: nginx/1.25.2
Date: Wed, 20 Sep 2023 00:41:32 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 15 Aug 2023 17:03:04 GMT
Connection: keep-alive
ETag: "64dbafc8-267"
Accept-Ranges: bytes
但问题是:当我停止容器时,您会看到这样的错误:
-> # curl -I 127.0.0.1
curl: (7) Failed to connect to 127.0.0.1 port 80 after 0 ms: Couldn't connect to server
然而,没有,我遇到了同样持续的 404 错误!
更新:更神秘的是,当我关闭 Docker 容器时,尝试访问192.168.10.1
也会导致 404 错误:
-> # curl -I 192.168.10.1
HTTP/1.1 404 Not Found
Content-Type: text/plain; charset=utf-8
X-Content-Type-Options: nosniff
Date: Wed, 20 Sep 2023 00:51:49 GMT
Content-Length: 19
我完全不知所措,不知道问题出在哪里。更令人困惑的是,我可以用端口 81 重复这个实验,一切都按预期工作。如果有人对如何调试这个问题有任何见解或建议,我将不胜感激!
这里还有一些输出:
-> # lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
docker-pr 9815 root 4u IPv4 54638 0t0 TCP *:http (LISTEN)
docker-pr 9823 root 4u IPv6 48079 0t0 TCP *:http (LISTEN)
-> # docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
975e74d4bdfd nginx:latest "/docker-entrypoint.…" 29 minutes ago Up 29 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp nginx_test_nginx-test_1
-> # ufw status
Status: active
To Action From
-- ------ ----
22/tcp ALLOW Anywhere
80/tcp ALLOW Anywhere
443 ALLOW Anywhere
Anywhere ALLOW 192.168.1.0/24
Anywhere ALLOW 192.168.178.0/24
81/tcp ALLOW Anywhere
22/tcp (v6) ALLOW Anywhere (v6)
80/tcp (v6) ALLOW Anywhere (v6)
443 (v6) ALLOW Anywhere (v6)
81/tcp (v6) ALLOW Anywhere (v6)
Anywhere on eth0 ALLOW FWD Anywhere on wghub
Anywhere on wghub ALLOW FWD Anywhere on wghub
Anywhere (v6) on eth0 ALLOW FWD Anywhere (v6) on wghub
Anywhere (v6) on wghub ALLOW FWD Anywhere (v6) on wghub
-> # netstat -ltnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:10010 0.0.0.0:* LISTEN 1374/containerd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 631/sshd: /usr/sbin
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 9815/docker-proxy
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 610/systemd-resolve
tcp6 0 0 :::22 :::* LISTEN 631/sshd: /usr/sbin
tcp6 0 0 :::80 :::* LISTEN 9823/docker-proxy
-> # cat /etc/default/docker
DOCKER_OPTS="--iptables=false"
更新:这是我的测试配置:
docker-compose.yml
version: '3'
services:
nginx-test:
image: nginx:latest
ports:
- "80:80"