dongmeixian9665 2017-09-28 01:37
浏览 269

Nginx反向代理到Golang服务器的Docker问题

I have nginx reverse proxying to a golang server, each in its own docker container and running normally. I keep getting this error in nginx when trying to connect to Go container, BOTH on my local Mac machine and on the production server Linux Debian. It was working on my local Mac machine a week ago and all of a sudden it doesn't anymore

nginx_1  | 2017/09/28 01:29:54 [error] 5#5: *12 upstream timed out (110: Connection timed out) while connecting to upstream, client: 172.23.0.1, server: , request: "GET /api/about HTTP/1.1", upstream: "http://67.199.248.12:8080/api/about", host: "localhost"

can someone explain what's going on here? What is 67.199.248.12 and why isn't Nginx able to ping/connect to the Go container?

here's what's inside /etc/hosts in Go container

127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.23.0.5  31f4103f002c

here's my nginx.conf

upstream gogo {
  server go:8080 weight=10 max_fails=3 fail_timeout=30s;
}

server {
  listen 80;

  location /api/ {
    resolver            127.0.0.1 valid=30s;
    proxy_pass          http://gogo;
    proxy_redirect      off;
    proxy_set_header    Host $host;
    proxy_set_header    X-Real-IP $remote_addr;
    proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header    X-Forwarded-Host $server_name;
  }
}

I'm using docker-compose v3 to run nginx and docker

  nginx:
    restart: always
    image: nginx
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf
    ports:
      - "80:80"
    command: /bin/bash -c "nginx -g 'daemon off;'"

  go:
    build:
      context: ./api
    expose:
      - "8080"
    depends_on:
      - nginx

go code:

func main() {
    router := mux.NewRouter().StrictSlash(true)
    router.HandleFunc("/api/about", About).Methods("GET")
    log.Fatal(http.ListenAndServe(":8080", router))
}
  • 写回答

1条回答 默认 最新

  • douoyou3348 2017-09-28 02:23
    关注

    It seems like containers can't ping each other ?

    I see two possibilities :

    First : Ensure each container are on the same network

    docker run --network=host ...
    

    or create a network

    docker network create nginx_go
    docker run --network=nginx_go ...
    

    Second : Use docker-compose

    version: '3.3'
    
    services:
        nginx:
          image: nginx
          # others details like volumes...
        server:
            image: go
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!