502 Gateway Error Docker-compose Nginx-Proxy Django

I am trying to docker up my django application using this tutorial.

Technically, I have 2 problems:

  1. Cannot connect to the database
  2. 502 gateway error

Everything seems to work until i get this error:

nginx-proxy    | nginx.1  *1 connect() failed (111: Connection refused) while connecting to upstream, client: 111.222.333.444, server:, request: "GET /favicon.ico HTTP/2.0", upstream: "", host: "", referrer: ""

Also, it seems that my web server is unable to detect my postgresql database as it prompts a “waiting for postgres” due to the i have provided. I have configured my database using this tutorial:

Been stuck with this for a few days! I will attach my code here:

When I run netstat plant:

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 *               LISTEN      637/systemd-resolve 
tcp        0      0    *               LISTEN      840/sshd            
tcp        0      0*               LISTEN      27204/postgres      
tcp        0      0*               LISTEN      19618/containerd    
tcp        0      0 111.222.333.444:22       555.666.777.888:52289    ESTABLISHED 740/sshd: root@nott 
tcp        0   1080 111.222.333.444:22       999.111.222.333:13146    ESTABLISHED 16139/sshd: [accept 
tcp        0    356 111.222.333.444:22       555.666.777.888:52288    ESTABLISHED 32641/sshd: root@pt 
tcp6       0      0 :::22                   :::*                    LISTEN      840/sshd   

docker-compose file:

version: '3.8'

    container_name: container_name
      context: ./hello_main
    image: hello
    command: gunicorn --timeout 180 hello.wsgi:application --bind
      - static_volume:/home/app/web/staticfiles
      - media_volume:/home/app/web/mediafiles
      - 8000
      - ./.env
    container_name: nginx-proxy
    build: ./nginx
    image: nginx-proxy
    restart: always
      - 80:80
      - static_volume:/home/app/web/staticfiles
      - media_volume:/home/app/web/mediafiles
      - certs:/etc/nginx/certs
      - html:/usr/share/nginx/html
      - vhost:/etc/nginx/vhost.d
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - web


DockerFile stuff works fine.

I also configured settings on nginx based on this tutorial: I did not use the nginx.conf provided by the digitalocean server.


server {

    listen 80;

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;

location = /favicon.ico { access_log off; log_not_found off; }

location /staticfiles/ {
  alias /home/app/web/staticfiles/;
  add_header Access-Control-Allow-Origin *;

location /mediafiles/ {
  alias /home/app/web/mediafiles/;
  add_header Access-Control-Allow-Origin *;
  proxy_read_timeout 300;
  proxy_connect_timeout 300;
  proxy_send_timeout 300; 

DockerFile for NGINX:

FROM jwilder/nginx-proxy
COPY vhost.d/default /etc/nginx/vhost.d/default
COPY custom.conf /etc/nginx/conf.d/custom.conf


client_max_body_size 25M;
proxy_connect_timeout       600;
proxy_send_timeout          600;
proxy_read_timeout          600;
send_timeout                600;

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hi there @Bleung026,

As far as I can see you are running your database directly on the server itself. What you could do in order to connect to the database from a Docker container, you could use the following hostname:


Regarding the 502 error, it is most likely caused by the database connection, if your backend service is unable to obtain a database connection it is most likely crashing and the docker container is not starting.

You can verify that with the docker ps -a command.

Here is a quick video that explains why Nginx returns a 502 error:

Let me know how it goes. Regards, Bobby