Docker与nginx反向代理到golang服务器的问题

我有nginx反向代理到一个golang服务器,每个在它自己的docker集装箱,并正常运行。 当我尝试连接到Go容器时,在我的本地Mac机器上和生产服务器Linux Debian上,我都不断收到nginx的这个错误。 一周前它在我的本地Mac机器上工作,突然之间,它就不复存在了

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" 

有人可以解释这里发生了什么? 什么是67.199.248.12,为什么Nginx不能ping /连接到Go容器?

Go容器中的/ etc / hosts里面有什么

 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 

这是我的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; } } 

我使用docker-compose v3来运行nginx和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 

去代码:

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

容器似乎不能互相ping通?

我看到两种可能性:

首先 :确保每个容器都在同一个networking上

 docker run --network=host ... 

或创build一个networking

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

第二 :用docker-compose

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