【问题标题】:Docker Compose NGINX Reverse Proxy 502Docker Compose NGINX 反向代理 502
【发布时间】:2020-02-01 10:49:26
【问题描述】:

我有以下设置,我一生都无法弄清楚为什么我无法连接到 api。

nginx.conf

worker_processes auto;
worker_rlimit_nofile 200000;

events {
  use epoll;
  accept_mutex on;
  multi_accept on;
  worker_connections 1024;
}

http {
  error_log /etc/nginx/error_log.log warn;
  client_max_body_size 20m;

  server {
    listen 80;
    listen [::]:80;

    location / {
      proxy_pass http://api:8080/;
    }

    location /health {
      return 200;
      access_log off;
    }
  }
}

docker-compose.yml

version: "3.7"

services:
  nginx:
    container_name: nginx
    image: "nginx:latest"
    ports:
      - "8000:80"
    networks:
      - internal_net
    volumes:
      - ./container/nginx.conf:/etc/nginx/nginx.conf

  api:
    container_name: api
    build:
      context: .
      dockerfile: container/Dockerfile
    expose:
      - "8080"
    networks:
      - internal_net
    depends_on:
      - postgres
    command: ["./wait-for-it.sh", "postgres:5432", "--timeout=60", "--", "./52-server-go"]

  postgres:
    container_name: postgres
    image: "postgres:9.5-alpine"
    expose:
      - "5432"
    networks:
      - internal_net

networks:
  internal_net:
    driver: bridge

volumes:
  container:

我的 api 设置为在端口 8080 上运行,当我进入容器并针对该地址运行 curl 请求时,它可以工作。根据 compose 文件,它应该将该地址暴露给本地 compose 网络,该网络由包括 nginx 在内的所有服务共享。

根据 nginx 配置,它应该将每个请求(/health 检查除外)传递给api 服务。相反,返回的是来自 nginx 的 502。

我在哪里搞混了?我做错了什么?

【问题讨论】:

  • 没关系,8000 只是我用来从外部连接它的端口。

标签: go nginx docker-compose gorilla mux


【解决方案1】:

问题实际上出在我的 go 应用程序中。用golanggorilla/mux,只好改地址:

原件(破损)

    // Start server
    address := "127.0.0.1:8080"
    srv := &http.Server{
        Handler:      r,
        Addr:         address,
        WriteTimeout: 15 * time.Second,
        ReadTimeout:  15 * time.Second,
    }

修复

    // Start server
    address := "0.0.0.0:8080"
    srv := &http.Server{
        Handler:      r,
        Addr:         address,
        WriteTimeout: 15 * time.Second,
        ReadTimeout:  15 * time.Second,
    }

我不知道为什么,但这解决了它。

【讨论】:

    猜你喜欢
    • 2018-05-31
    • 1970-01-01
    • 2018-04-15
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    相关资源
    最近更新 更多