
我遇到了一個令人困惑的網路問題,讓我摸不著頭緒。情況是這樣的:我無法從外部存取端口 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"