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

使用Docker/Docker Compose运行Express Gateway时始终返回Bad Gateway

MERN微服务API网关返回Bad Gateway问题排查

我正在构建基于NodeJS的MERN架构微服务应用,已完成项目并完成Docker化,但运行API网关与用户服务时,始终返回Bad Gateway错误。以下是相关配置文件:

gateway.config.yml

http:
  port: 8080
admin:
  port: 9876
  host: localhost
apiEndpoints:
  api:
    host: localhost
    paths: '/ip'
  user:
    host: localhost
    paths: '/user/*'
  gig: 
    host: localhost
    paths: ['/gig', '/gig/*']
  message: 
    host: localhost
    paths: ['/message', '/message/*']
  order: 
    host: localhost
    paths: ['/order', '/order/*']
serviceEndpoints:
  httpbin:
    url: 'https://httpbin.org'
  userService: 
    url: 'http://user-service:8001'
  gigService: 
    url: 'http://gig-service:8002'
  messageService: 
    url: 'http://message-service:8004'
  orderService: 
    url: 'http://order-service:8003'
policies:
  - basic-auth
  - cors
  - expression
  - key-auth
  - log
  - oauth2
  - proxy
  - rate-limit
pipelines:
  default:
    apiEndpoints:
      - api
    policies:
    # Uncomment `key-auth:` when instructed to in the Getting Started guide.
    # - key-auth:
      - proxy:
          - action:
              serviceEndpoint: httpbin 
              changeOrigin: true
  userPipeline:
    apiEndpoints:
      - user
    policies:
    # Uncomment `key-auth:` when instructed to in the Getting Started guide.
    # - key-auth:
      - proxy:
          - action:
              serviceEndpoint: userService
              changeOrigin: true
  gigPipeline:
    apiEndpoints:
      - gig
    policies:
    # Uncomment `key-auth:` when instructed to in the Getting Started guide.
    # - key-auth:
      - proxy:
          - action:
              serviceEndpoint: gigService
              changeOrigin: true
  messagePipeline:
    apiEndpoints:
      - message
    policies:
    # Uncomment `key-auth:` when instructed to in the Getting Started guide.
    # - key-auth:
      - proxy:
          - action:
              serviceEndpoint: messageService
              changeOrigin: true
  orderPipeline:
    apiEndpoints:
      - order
    policies:
    # Uncomment `key-auth:` when instructed to in the Getting Started guide.
    # - key-auth:
      - proxy:
          - action:
              serviceEndpoint: orderService
              changeOrigin: true

docker-compose.yml

version: '3.8'

services:
  api-service:
    image: aswanth600/api:latest
    container_name: api-container
    ports:
      - "8080:8080"
    networks:
      - my-network  

  user-service:
    image: aswanth600/user:latest
    container_name: user-container
    ports:
      - "8001:8001"
    networks:
      - my-network  

  order-service:
    image: aswanth600/order:latest
    container_name: order-container
    ports:
      - "8003:8003"
    networks:
      - my-network  

  gig-service:
    image: aswanth600/gig:latest
    container_name: gig-container
    ports:
      - "8002:8002"
    networks:
      - my-network  

  message-service:
    image: aswanth600/message:latest
    container_name: message-container
    ports:
      - "8004:8004"
    networks:
      - my-network  


networks:
  my-network:
    driver: bridge

用户服务Dockerfile

FROM node:alpine

WORKDIR /app
COPY package.json .
RUN npm install
COPY . .

CMD ["npm", "run", "dev"]

排查与解决步骤

  • 检查用户服务容器状态:执行docker-compose logs user-service查看服务启动日志,确认是否有代码报错、端口绑定失败等问题。
  • 验证跨容器网络连通性:进入api-service容器(docker exec -it api-container sh),执行curl http://user-service:8001,测试能否直接访问用户服务。如果请求失败,确认两个容器都加入了my-network网络。
  • 确认服务监听地址:用户服务的NodeJS应用必须监听0.0.0.0而不是localhost,否则容器外部无法访问。检查服务启动代码,确保端口绑定的是0.0.0.0,比如app.listen(8001, '0.0.0.0')。
  • 核对网关路径匹配规则:确保请求路径符合网关配置的/user/*规则,比如正确请求http://localhost:8080/user/profile,而非http://localhost:8080/profile。
  • 调整代理配置:尝试将网关proxy策略中的changeOrigin改为false,部分服务会依赖原始请求头的Host字段,开启changeOrigin可能导致服务无法识别请求来源。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 23:24:53