Question

502 Bad Gateway (nginx/1.18.0 (Ubuntu)) with Django App

Hi DigitalOcean Team and Developers!

I’m currently hosting my Django App in DigitalOcean, i encountered an issue in the process, it’s related to a 502 Bad Gateway error i receive while trying to access my app. I’m actually deploying a Python/Django app with Nginx, Gunicorn and a MySQL database. I followed the tutorial word to word and saw there was a lot of users fighting this issue, i don’t know if there is something missing in the tutorial or if i missed something.

These are the error logs i’m receiving, status and my configurations from both NGINX and Gunicorn

NGINX Configurations: i was listening to port 80 in both server blocks which lead to a bad request 400 error response, once removed the 502 bad gateway error kicked in

server {

      server_name domain.com www.domain.com;

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

      location / {
         include proxy_params;
         proxy_pass https://unix:/home/lrodri04/project/project/project.sock;
     }


    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
    if ($host = www.domain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = domain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;

    listen [::]:80;

    server_name domain.com www.domain.com;
    return 404; # managed by Certbot
}

NGINX Status:

Command:
sudo nginx -t

Output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

NGINX Error Logs:

Command:
sudo tail -f /var/log/nginx/error.log


Output:
2021/09/07 02:02:00 [error] 145950#145950: *1 peer closed connection in SSL handshake while SSL handshaking to upstream, client: 138.94.121.91, server: domain.com, request: "GET / HTTP/1.1", upstream: "https://unix:/home/lrodri04/project/project/project.sock:/", host: "www.domain.com"
2021/09/07 02:05:43 [error] 145950#145950: *5 peer closed connection in SSL handshake while SSL handshaking to upstream, client: 138.94.121.91, server: domain.com, request: "GET / HTTP/1.1", upstream: "https://unix:/home/lrodri04/project/project/project.sock:/", host: "www.domain.com"
2021/09/07 02:10:29 [error] 145950#145950: *8 peer closed connection in SSL handshake while SSL handshaking to upstream, client: 138.94.121.91, server: domain.com, request: "GET / HTTP/1.1", upstream: "https://unix:/home/lrodri04/project/project/rpoject.sock:/", host: "www.domain.com"
2021/09/07 02:14:02 [error] 146423#146423: *2 peer closed connection in SSL handshake while SSL handshaking to upstream, client: 138.94.121.91, server: domain.com, request: "GET / HTTP/1.1", upstream: "https://unix:/home/lrodri04/project/project/project.sock:/", host: "www.domain.com"
2021/09/07 02:18:05 [error] 146571#146571: *2 peer closed connection in SSL handshake while SSL handshaking to upstream, client: 138.94.121.91, server: domain.com, request: "GET / HTTP/1.1", upstream: "https://unix:/home/lrodri04/project/project/project.sock:/", host: "www.domain.com"
2021/09/07 02:19:31 [error] 146571#146571: *2 peer closed connection in SSL handshake while SSL handshaking to upstream, client: 138.94.121.91, server: domain.com, request: "GET / HTTP/1.1", upstream: "https://unix:/home/lrodri04/project/project/project.sock:/", host: "www.domain.com"
2021/09/07 02:23:37 [alert] 146889#146889: *2 open socket #14 left in connection 7
2021/09/07 02:23:37 [alert] 146889#146889: aborting
2021/09/07 02:24:19 [error] 146954#146954: *2 peer closed connection in SSL handshake while SSL handshaking to upstream, client: 138.94.121.91, server: domain.com, request: "GET / HTTP/1.1", upstream: "https://unix:/home/lrodri04/project/project/project.sock:/", host: "www.domain.com"
2021/09/07 02:28:39 [error] 147104#147104: *2 peer closed connection in SSL handshake while SSL handshaking to upstream, client: 138.94.121.91, server: domain.com, request: "GET / HTTP/1.1", upstream: "https://unix:/home/lrodri04/project/project/project.sock:/", host: "www.domain.com"

NGINX Status:

Command:
sudo systemctl status nginx

● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2021-09-07 02:27:53 UTC; 16min ago
       Docs: man:nginx(8)
    Process: 147090 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 147102 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 147103 (nginx)
      Tasks: 2 (limit: 1136)
     Memory: 6.8M
     CGroup: /system.slice/nginx.service
             ├─147103 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             └─147104 nginx: worker process

Sep 07 02:27:53 Sealena systemd[1]: Starting A high performance web server and a reverse proxy server...
Sep 07 02:27:53 Sealena systemd[1]: Started A high performance web server and a reverse proxy server.

Gunicorn Configurations:

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

[Service]
User=lrodri04
Group=www-data
WorkingDirectory=/home/lrodri04/project/project
ExecStart=/home/lrodri04/environments/project/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/lrodri04/project/project/project.sock project.wsgi:application

[Install]
WantedBy=multi-user.target

Gunicorn Status:

● gunicorn.service - gunicorn daemon
     Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2021-09-07 02:05:08 UTC; 50min ago
   Main PID: 146128 (gunicorn)
      Tasks: 4 (limit: 1136)
     Memory: 221.2M
     CGroup: /system.slice/gunicorn.service
             ├─146128 /home/lrodri04/environments/Sealena/bin/python3 /home/lrodri04/environments/Sealena/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/lrodri04/Sealena/Sealena/Sealen>
             ├─146458 /home/lrodri04/environments/Sealena/bin/python3 /home/lrodri04/environments/Sealena/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/lrodri04/Sealena/Sealena/Sealen>
             ├─146977 /home/lrodri04/environments/Sealena/bin/python3 /home/lrodri04/environments/Sealena/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/lrodri04/Sealena/Sealena/Sealen>
             └─147133 /home/lrodri04/environments/Sealena/bin/python3 /home/lrodri04/environments/Sealena/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/lrodri04/Sealena/Sealena/Sealen>

Sep 07 02:19:31 Sealena gunicorn[146606]: [2021-09-07 02:19:31 +0000] [146606] [INFO] Worker exiting (pid: 146606)
Sep 07 02:19:31 Sealena gunicorn[146780]: [2021-09-07 02:19:31 +0000] [146780] [INFO] Booting worker with pid: 146780
Sep 07 02:22:55 Sealena gunicorn[146458]:  - - [07/Sep/2021:02:22:55 +0000] "GET / HTTP/1.0" 400 143 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome>
Sep 07 02:23:28 Sealena gunicorn[146780]:  - - [07/Sep/2021:02:23:28 +0000] "GET / HTTP/1.0" 400 143 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome>
Sep 07 02:24:19 Sealena gunicorn[146128]: [2021-09-07 02:24:19 +0000] [146128] [CRITICAL] WORKER TIMEOUT (pid:146141)
Sep 07 02:24:19 Sealena gunicorn[146141]: [2021-09-07 02:24:19 +0000] [146141] [INFO] Worker exiting (pid: 146141)
Sep 07 02:24:20 Sealena gunicorn[146977]: [2021-09-07 02:24:20 +0000] [146977] [INFO] Booting worker with pid: 146977
Sep 07 02:28:39 Sealena gunicorn[146128]: [2021-09-07 02:28:39 +0000] [146128] [CRITICAL] WORKER TIMEOUT (pid:146780)
Sep 07 02:28:39 Sealena gunicorn[146780]: [2021-09-07 02:28:39 +0000] [146780] [INFO] Worker exiting (pid: 146780)
Sep 07 02:28:40 Sealena gunicorn[147133]: [2021-09-07 02:28:40 +0000] [147133] [INFO] Booting worker with pid: 147133


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.

KFSys
Site Moderator
Site Moderator badge
September 8, 2021
Accepted Answer

Hi @Lrodri04,

In 99% of the cases, when using such a setup and you get a 502 Bad Gateway, and your Nginx starts without a problem, you can exclude Nginx as the source of the issue.

Most probably it’s a problem with the configuration or code of the Application. I’ll recommend turning your attention to your Python/Django application and a misconfiguration there.

Additionally, I can take a look at the tutorial you’ve used to deploy your app and try to see if there could be some potential issues somewhere.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel