Question

I'm getting a 502 Bad Gateway to my django project

Posted April 5, 2020 3.3k views
NginxPostgreSQLDjangoUbuntu 18.04

Hi everyone,

I’m getting a 502 Bad Gateway to my django project.

Everything was working until i directed my domain name to my ip adress. Can anyone help?

local_settings.py
SECRET_KEY = 'n'
ALLOWED_HOSTS = ['159.65.235.171','.petergitz.com','.www.petergitz.com']
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME':'portfoliodb',
        'USER':'dbuser',
        'PASSWORD':'####',
        'HOST':'localhost',
        'PORT':'5432',
    }
}
/etc/systemd/system/gunicorn.socket

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target
/etc/systemd/system/gunicorn.service

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=peter
Group=www-data
WorkingDirectory=/home/peter/portfolio-project
ExecStart=/home/peter/myenv/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          portfolio.wsgi:application

[Install]
WantedBy=multi-user.target
gunicorn status
● gunicorn.service - gunicorn daemon
   Loaded: loaded (/etc/systemd/system/gunicorn.service; disabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sun 2020-04-05 13:14:41 UTC; 23h ago
 Main PID: 1184 (code=exited, status=1/FAILURE)

Apr 05 13:14:36 portfolio systemd[1]: Started gunicorn daemon.
Apr 05 13:14:36 portfolio gunicorn[1184]: [2020-04-05 13:14:36 +0000] [1184] [INFO] Starting gunicorn 20.0.4
Apr 05 13:14:36 portfolio gunicorn[1184]: [2020-04-05 13:14:36 +0000] [1184] [ERROR] Retrying in 1 second.
Apr 05 13:14:37 portfolio gunicorn[1184]: [2020-04-05 13:14:37 +0000] [1184] [ERROR] Retrying in 1 second.
Apr 05 13:14:38 portfolio gunicorn[1184]: [2020-04-05 13:14:38 +0000] [1184] [ERROR] Retrying in 1 second.
Apr 05 13:14:39 portfolio gunicorn[1184]: [2020-04-05 13:14:39 +0000] [1184] [ERROR] Retrying in 1 second.
Apr 05 13:14:40 portfolio gunicorn[1184]: [2020-04-05 13:14:40 +0000] [1184] [ERROR] Retrying in 1 second.
Apr 05 13:14:41 portfolio gunicorn[1184]: [2020-04-05 13:14:41 +0000] [1184] [ERROR] Can't connect to /run/gunicorn.sock
Apr 05 13:14:41 portfolio systemd[1]: gunicorn.service: Main process exited, code=exited, status=1/FAILURE
Apr 05 13:14:41 portfolio systemd[1]: gunicorn.service: Failed with result 'exit-code'.
/etc/nginx/sites-available/portfolio

server {
    listen 80;
    server_name  159.65.235.171 petergitz.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/peter/portfolio-project;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}
sudo tail -F /var/log/nginx/error.log
2020/04/06 11:49:47 [error] 20452#20452: *5 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 104.128.63.109, server: 159.65.235.171, request: "GET /cms/wp-includes/wlwmanifest.xml HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/cms/wp-includes/wlwmanifest.xml", host: "petergitz.com"
2020/04/06 12:18:46 [error] 20452#20452: *17 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 150.107.138.37, server: 159.65.235.171, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/", host: "159.65.235.171:80"
2020/04/06 12:47:32 [error] 20452#20452: *19 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 41.205.25.203, server: 159.65.235.171, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/", host: "159.65.235.171:80"
edited by MattIPv4

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.

×
Submit an Answer
1 answer

Hi there,

I just came accross this quesiton.

You are getting the 502 error as the Gunicorn service is not running.

What I could suggest for Django is to follow the steps here on how to Set Up Django with Nginx, and Gunicorn:

https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04

Regarding the 502 error, this means that your backend service is not running so Nginx is failing to connect to the backend, for more information on how to troubleshoot the 502 error, I could suggest following the steps from this answer here:

https://www.digitalocean.com/community/questions/502-bad-gateway-nginx-2

Regards,
Bobby

by Justin Ellingwood
Django is a powerful web framework that can help you get your Python application or website off the ground. Django includes a simplified development server for testing your code locally, but for anything even slightly production related, a more secure and powerful web server...