GCP Debian计算实例无法接收HTTP/HTTPS流量问题排查
问题排查与修复方案
核心错误1:Node.js服务绑定本地回环地址
你的Node.js代码将hostname设为127.0.0.1,这会导致服务仅能在容器内部被访问,容器外部(包括GCP主机)无法连接该端口。必须将绑定地址改为0.0.0.0,让服务监听容器的所有网络接口:
const http = require('http'); // 修改为0.0.0.0,允许外部访问 const hostname = '0.0.0.0'; const port = 5000; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World'); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });
核心错误2:基础镜像无Node.js运行环境
你使用的alpine镜像默认没有安装Node.js,根本无法运行你的Node.js应用。需要替换为带有Node.js环境的镜像,比如node:alpine:
# 更换镜像为node:alpine,确保支持Node.js运行 docker run -it -p 80:5000 -p 8080:5000 -p 443:5001 -p 8443:5001 -v "$(pwd):/src" --name test-container node:alpine
进入容器后,切换到/src目录执行你的Node.js文件(例如node app.js)。
后续验证步骤
- 在容器内执行
curl http://0.0.0.0:5000,确认服务正常返回内容 - 在GCP主机上执行
curl http://localhost:80,验证端口映射生效 - 重新用
nmap扫描实例IP,检查80/8080端口是否开放
内容的提问来源于stack exchange,提问作者Casey Knight
相关产品推荐
相关产品推荐

