title: Can't connect Ngxin reverse proxy to Gunicorn on Django container (Docker)
body: <p>I've being trying to make a stack with a Nginx container receiving requests as a reverse proxy to my Django container with Gunicorn (not worrying about static files or DB for now), but I keep getting a <strong>502 Bad Gateway</strong> error on Nginx.</p>
  
  <p>Nginx logs:</p>
  
  <pre><code>2017/04/28 12:10:10 [notice] 1#1: using the "epoll" event method
  2017/04/28 12:10:10 [notice] 1#1: nginx/1.12.0
  2017/04/28 12:10:10 [notice] 1#1: built by gcc 6.3.0 20170205 (Debian 6.3.0-6) 
  2017/04/28 12:10:10 [notice] 1#1: OS: Linux 4.4.0-63-generic
  2017/04/28 12:10:10 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
  2017/04/28 12:10:10 [notice] 1#1: start worker processes
  2017/04/28 12:10:10 [notice] 1#1: start worker process 7
  2017/04/28 12:10:10 [notice] 1#1: start worker process 8
  2017/04/28 12:10:10 [notice] 1#1: start worker process 9
  172.19.0.1 - - [28/Apr/2017:12:10:17 +0000] "GET / HTTP/1.1" 502 575 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"
  2017/04/28 12:10:17 [error] 7#7: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 172.19.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.53.53:8000/", host: "localhost"
  2017/04/28 12:10:28 [info] 7#7: *1 client closed connection while waiting for request, client: 172.19.0.1, server: 0.0.0.0:80
  2017/04/28 12:10:28 [info] 7#7: *4 client closed connection while waiting for request, client: 172.19.0.1, server: 0.0.0.0:80
  2017/04/28 12:10:28 [info] 7#7: *3 client closed connection while waiting for request, client: 172.19.0.1, server: 0.0.0.0:80
  </code></pre>
  
  <p>Gunicorn:</p>
  
  <pre><code>[2017-04-28 12:09:02 +0000] [1] [INFO] Starting gunicorn 19.7.1
  [2017-04-28 12:09:02 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
  [2017-04-28 12:09:02 +0000] [1] [INFO] Using worker: sync
  [2017-04-28 12:09:02 +0000] [9] [INFO] Booting worker with pid: 9
  [2017-04-28 12:09:02 +0000] [11] [INFO] Booting worker with pid: 11
  [2017-04-28 12:09:02 +0000] [13] [INFO] Booting worker with pid: 13
  </code></pre>
  
  <p>Dockerfile for nginx:</p>
  
  <pre><code>FROM nginx:stable
  COPY nginx.conf /etc/nginx/nginx.conf
  </code></pre>
  
  <p>nginx.conf file:</p>
  
  <pre><code>worker_processes 3;
  
  events { 
      worker_connections 1024; 
  }
  
  error_log /var/log/nginx/error.log debug;
  
  http {
      sendfile on;
  
      upstream app_servers {
          server app:8000;
      }
  
      server {
          listen 80;
  
          location / {
              proxy_pass         http://app_servers;
              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;
          }
      }
  }
  </code></pre>
  
  <p>Dockerfile for django:</p>
  
  <pre><code>FROM python:3.5-slim
  
  ENV PYTHONUNBUFFERED 1
  RUN mkdir /code
  
  WORKDIR /code
  
  ADD . /code
  
  EXPOSE 8000
  
  RUN pip install -r requirements.txt
  RUN python manage.py collectstatic --noinput
  
  CMD ["gunicorn", "marcos.wsgi:application", "--bind", "0.0.0.0:8000", "--workers", "3"]
  </code></pre>
  
  <p>I've tried to create a network manually and placing both containers in it running the following commands.</p>
  
  <pre><code>docker run --network marcos_net -h app marcos
  docker run --network marcos_net -h nginx -p 80:80 nginx
  </code></pre>
  
  <p>And with a docker-compose.yml file:</p>
  
  <pre><code>version: '3'  
  services:  
    app:
      image: myrepo/app
    nginx:
      image: myrepo/nginx
      ports:
        - "80:80"
      depends_on:
        - app
  </code></pre>
  
  <p>I've also tried to access the containers to ping one another like it says in the docs (<a href="https://docs.docker.com/engine/tutorials/networkingcontainers/" rel="nofollow noreferrer">https://docs.docker.com/engine/tutorials/networkingcontainers/</a>) and they are in fact in the same network. Also checked with the <code>docker network inspect</code> command. </p>
  
  <p>Both ways log the same error printed above.</p>
  
  <p>I've seen some similar questions, but they are from different docker-compose file versions, and they didn't solve the problem for me, so I don't think it is the same problem.</p>