解决Nginx Docker容器与Node.js微服务的502 Bad Gateway问题
问题:开发环境下VS Code启动Node.js服务时Nginx出现502 Bad Gateway
采用微服务架构,部署了3个Node.js服务、MongoDB数据库,用Nginx做代理。docker-compose运行所有服务完全正常,但开发环境下从VS Code启动Node.js服务时,Nginx容器报502 Bad Gateway错误。已经将Node.js服务监听地址设为0.0.0.0,重新加载Nginx容器后问题依旧。
相关配置文件
docker-compose.yml
version: "3" name: "shopping-store" services: nosql-db: image: mongo ports: - "27018:27017" container_name: nosql-db volumes: - ./db/:/data/db products: build: dockerfile: Dockerfile context: ./products container_name: shopping-store-products ports: - "8002:8002" depends_on: - "nosql-db" env_file: - ./products/.env.dev shopping: build: dockerfile: Dockerfile context: ./shopping container_name: shopping-store-shopping ports: - "8003:8003" depends_on: - "nosql-db" env_file: - ./shopping/.env.dev customer: build: dockerfile: Dockerfile context: ./customer container_name: shopping-store-customer ports: - "8001:8001" depends_on: - "nosql-db" env_file: - ./customer/.env.dev nginx-proxy: build: dockerfile: Dockerfile context: ./proxy depends_on: - products - shopping - customer ports: - 80:80
nginx.conf
worker_processes 4; events { worker_connections 1024; } http { server { listen 80; charset utf-8; location / { proxy_pass http://products:8002; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } location ~ ^/shopping { rewrite ^/shopping/(.*) /$1 break; proxy_pass http://shopping:8003; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } location /customer { rewrite ^/customer/(.*)$ /$1 break; proxy_pass http://customer:8001; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } } }
Nginx日志截图

解决方案
核心原因
VS Code本地启动的Node.js服务运行在宿主机上,而Nginx运行在Docker容器内。当前Nginx配置里的http://products:8002这类地址是Docker内部的服务域名,只能解析到Docker网络中的容器服务,无法直接访问宿主机上的本地服务,因此触发502错误。
具体修复步骤
1. 修改Nginx代理地址
将Nginx配置中的服务域名替换为宿主机的可访问地址:
- Linux/macOS/Windows:直接使用Docker内置的宿主机别名
host.docker.internal(无需手动查找宿主机IP) - 若
host.docker.internal无法生效,可替换为宿主机的局域网IP(如192.168.1.100)
修改后的nginx.conf示例:
worker_processes 4; events { worker_connections 1024; } http { server { listen 80; charset utf-8; location / { proxy_pass http://host.docker.internal:8002; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } location ~ ^/shopping { rewrite ^/shopping/(.*) /$1 break; proxy_pass http://host.docker.internal:8003; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } location /customer { rewrite ^/customer/(.*)$ /$1 break; proxy_pass http://host.docker.internal:8001; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } } }
2. 确认Node.js服务监听配置
确保Node.js服务确实监听0.0.0.0而非127.0.0.1(127.0.0.1仅允许本地访问,Docker容器无法连接)。以Express为例:
app.listen(8002, '0.0.0.0', () => { console.log('Products service running on port 8002'); });
3. 重启Nginx容器
修改配置后,重新构建并启动Nginx容器:
docker-compose up -d --build nginx-proxy
可选:分离开发/生产环境配置
创建nginx.dev.conf专门用于开发环境,通过docker-compose的环境变量或不同配置文件加载,避免与生产环境配置冲突。
内容的提问来源于stack exchange,提问作者mr_robot
相关产品推荐
相关产品推荐

