502 Bad Gateway

Hello again Django community,

I have just deployed a new Ubuntu server 22.04 and started a Django 5.0.7 test project called donuts (basic test project only). When I use runserver 0.0.0.0:8060 I am getting the expected success page / Django rocketship. When trying to deploy via uwsgi / nginx I am getting a 502 Bad Gateway error. Doing a systmctl status on nginx.service and uwsgi.service shows everything is operational without errors. Looking at the nginx error log I get …

2024/07/29 19:52:09 [error] 5716#5716: *11 upstream prematurely closed connection while reading response header from upstream, client: 192.168.1.1, server: mmbella.org, request: “GET / HTTP/1.1”, upstream: “uwsgi://unix:/run/uwsgi/donuts.sock:”, host: “mmbella.org

I would be happy to share the uwsgi ini and nginx files if needed.

Any suggestions?

Thanks
Marc

Yes, we’ll need to see the command you’re using to run uwsgi, your uwsgi ini file, the configuration section in nginx for your uwsgi connection, and any error messages in the uwsgi logs (not the summary shown by systemctl status - it can be misleading since uwsgi itself might be running, but the Django application has failed.)

Hello Ken,

UWSGI output…

*** Starting uWSGI 2.0.24-debian (64bit) on [Tue Jul 30 01:07:11 2024] ***
compiled with version: 13.2.0 on 17 April 2024 16:23:48
os: Linux-6.8.0-39-generic #39-Ubuntu SMP PREEMPT_DYNAMIC Fri Jul  5 21:49:14 UTC 2024
nodename: mmbella
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: /home/marc/webdev/donuts
detected binary path: /usr/bin/uwsgi-core
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 30115
your memory page size is 4096 bytes
detected max file descriptor number: 1024
*** starting uWSGI Emperor ***
*** has_emperor mode detected (fd: 6) ***
*** has_emperor mode detected (fd: 7) ***
[uWSGI] getting INI configuration from donuts.ini
*** Starting uWSGI 2.0.24-debian (64bit) on [Tue Jul 30 01:07:11 2024] ***
compiled with version: 13.2.0 on 17 April 2024 16:23:48
os: Linux-6.8.0-39-generic #39-Ubuntu SMP PREEMPT_DYNAMIC Fri Jul  5 21:49:14 UTC 2024
nodename: mmbella
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: /etc/uwsgi/apps-enabled
detected binary path: /usr/bin/uwsgi-core
chdir() to /home/marc/webdev/donuts
your processes number limit is 30115
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
[uWSGI] getting INI configuration from bellazzini.ini
thunder lock: disabled (you can enable it with --thunder-lock)
*** Starting uWSGI 2.0.24-debian (64bit) on [Tue Jul 30 01:07:11 2024] ***
compiled with version: 13.2.0 on 17 April 2024 16:23:48
os: Linux-6.8.0-39-generic #39-Ubuntu SMP PREEMPT_DYNAMIC Fri Jul  5 21:49:14 UTC 2024
nodename: mmbella
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 4

USWGI SERVICE FILE

[Unit]
Description=uWSGI Emperor service

[Service]
ExecStartPre=/bin/bash -c 'mkdir -p /run/uwsgi; chown marc:www-data /run/uwsgi'
ExecStart=/usr/bin/uwsgi --emperor /etc/uwsgi/apps-enabled
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all

[Install]
WantedBy=multi-user.target
~                                                                                                 
~                                       

UWSGI INI FILE

[uwsgi]
project = donuts 
uid = marc 
base = /home/%(uid)

chdir = %(base)/webdev/%(project)
home = %(base)/Env/%(project)
module = %(project).wsgi:application

master = true
processes = 5

socket = /run/uwsgi/%(project).sock
chown-socket = %(uid):www-data
chmod-socket = 660
vacuum = true

~                     

NGINX

server {
listen 80;
server_name mmbella.org;

location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
    root /home/marc/webdev/donuts;
}

location /media/ {
root /home/marc/webdev/donuts;
}


location / {
    include         uwsgi_params;
    uwsgi_pass      unix:/run/uwsgi/donuts.sock;
}
}

Looks good so far.

What are the permissions on /run/uwsgi/donuts.sock?

What are the contents of your /etc/uwsgi/apps-enabled directory? What are the contents of the file(s) in that directory?

I believe that in the uwsgi_pass directive, you need to use the /// notation to indicate root. e.g. uwsgi_pass unix:///run/uwsgi/donuts.sock; (That’s the notation I use.)

Side note: For future reference, please identify the actual name of the file being posted - don’t only identify it as its function.

Also, when posting code, error messages, or any other preformatted text, enclose the text between lines of three backtick - ` characters. This means you’ll have a line of ```, then the text, then another line of ```. This forces the forum software to keep that text properly formatted. (I’ve taken the liberty of adjusting your previous post.)

Hi Ken,

Permissions …

srw-rw---- 1 marc www-data 0 Jul 30 01:27 /run/uwsgi/donuts.sock

Contents of /etc/uwsgi/apps-enabled

-rw-r--r-- 1 root root 424 Nov 25  2023 README
lrwxrwxrwx 1 root root  40 Jul 29 01:13 bellazzini.ini -> /etc/uwsgi/apps-available/bellazzini.ini
lrwxrwxrwx 1 root root  36 Jul 29 19:36 donuts.ini -> /etc/uwsgi/apps-available/donuts.ini

Contents of donuts.ini

[uwsgi]
project = donuts 
uid = marc 
base = /home/%(uid)

chdir = %(base)/webdev/%(project)
home = %(base)/Env/%(project)
module = %(project).wsgi:application

master = true
processes = 5

socket = /run/uwsgi/%(project).sock
chown-socket = %(uid):www-data
chmod-socket = 660
vacuum = true

PS - I changed the formatting to unix:///run/uwsgi/donuts.sock and no change

There needs to be more log data for uwsgi somewhere. You should be able to find something showing more detail about the application being executed. Look in syslog and any other files in /var/log or /var/log/uwsgi that even remotely looks like it might contain log data for your project.

  1. Did you restart both Nginx and uWSGI services?
sudo systemctl restart nginx
sudo systemctl restart uwsgi

  1. What is the output of sudo ls -l /run/uwsgi/donuts.sock ?

Hello Anefta,

Yest both nginx and uwsgi restarted and daemon-reloaded when files were changed.

Output is


srw-rw---- 1 marc www-data 0 Jul 30 10:44 /run/uwsgi/donuts.sock

Surprisingly,

uwsgi logs are empty …


sudo ls -l /var/log/uwsgi/app/
total 0

Tail from syslog after restart uwsgi and nginx and reloading web page …


2024-07-30T10:50:00.407957+00:00 mmbella systemd[1]: Starting sysstat-collect.service - system activity accounting tool...
2024-07-30T10:50:00.416745+00:00 mmbella systemd[1]: sysstat-collect.service: Deactivated successfully.
2024-07-30T10:50:00.417123+00:00 mmbella systemd[1]: Finished sysstat-collect.service - system activity accounting tool.
2024-07-30T10:51:05.830379+00:00 mmbella uwsgi[11037]: [emperor] *** RAGNAROK EVOKED ***
2024-07-30T10:51:05.839151+00:00 mmbella uwsgi[11037]: Tue Jul 30 10:51:05 2024 - [emperor] stop the uwsgi instance donuts.ini
2024-07-30T10:51:05.839274+00:00 mmbella uwsgi[11037]: Tue Jul 30 10:51:05 2024 - [emperor] stop the uwsgi instance bellazzini.ini
2024-07-30T10:51:05.839761+00:00 mmbella uwsgi[11039]: SIGINT/SIGTERM received...killing workers...
2024-07-30T10:51:05.845795+00:00 mmbella systemd[1]: Stopping uwsgi.service - uWSGI Emperor service...
2024-07-30T10:51:05.852497+00:00 mmbella uwsgi[11038]: SIGINT/SIGTERM received...killing workers...
2024-07-30T10:51:05.852897+00:00 mmbella uwsgi[11039]: Tue Jul 30 10:51:05 2024 - received message 0 from emperor
2024-07-30T10:51:05.853155+00:00 mmbella uwsgi[11038]: Tue Jul 30 10:51:05 2024 - received message 0 from emperor
2024-07-30T10:51:06.831351+00:00 mmbella uwsgi[11039]: worker 1 buried after 1 seconds
2024-07-30T10:51:06.832110+00:00 mmbella uwsgi[11038]: worker 5 buried after 1 seconds
2024-07-30T10:51:07.833549+00:00 mmbella uwsgi[11039]: worker 2 buried after 2 seconds
2024-07-30T10:51:07.833886+00:00 mmbella uwsgi[11039]: worker 3 buried after 2 seconds
2024-07-30T10:51:07.834096+00:00 mmbella uwsgi[11039]: worker 4 buried after 2 seconds
2024-07-30T10:51:07.834327+00:00 mmbella uwsgi[11039]: worker 5 buried after 2 seconds
2024-07-30T10:51:07.834475+00:00 mmbella uwsgi[11039]: goodbye to uWSGI.
2024-07-30T10:51:07.834614+00:00 mmbella uwsgi[11039]: VACUUM: unix socket /run/uwsgi/bellazzini.sock removed.
2024-07-30T10:51:07.835577+00:00 mmbella uwsgi[11038]: worker 1 buried after 2 seconds
2024-07-30T10:51:07.836066+00:00 mmbella uwsgi[11038]: worker 2 buried after 2 seconds
2024-07-30T10:51:07.836369+00:00 mmbella uwsgi[11038]: worker 3 buried after 2 seconds
2024-07-30T10:51:07.836655+00:00 mmbella uwsgi[11038]: worker 4 buried after 2 seconds
2024-07-30T10:51:07.836809+00:00 mmbella uwsgi[11038]: goodbye to uWSGI.
2024-07-30T10:51:07.836933+00:00 mmbella uwsgi[11038]: VACUUM: unix socket /run/uwsgi/donuts.sock removed.
2024-07-30T10:51:08.831639+00:00 mmbella uwsgi[11037]: Tue Jul 30 10:51:08 2024 - [emperor] removed uwsgi instance donuts.ini
2024-07-30T10:51:08.831988+00:00 mmbella uwsgi[11037]: Tue Jul 30 10:51:08 2024 - [emperor] removed uwsgi instance bellazzini.ini
2024-07-30T10:51:09.832527+00:00 mmbella uwsgi[11037]: Tue Jul 30 10:51:09 2024 - The Emperor is buried.
2024-07-30T10:51:09.836065+00:00 mmbella systemd[1]: uwsgi.service: Deactivated successfully.
2024-07-30T10:51:09.836301+00:00 mmbella systemd[1]: Stopped uwsgi.service - uWSGI Emperor service.
2024-07-30T10:51:09.851574+00:00 mmbella systemd[1]: Starting uwsgi.service - uWSGI Emperor service...
2024-07-30T10:51:09.887436+00:00 mmbella uwsgi[11121]: *** Starting uWSGI 2.0.24-debian (64bit) on [Tue Jul 30 10:51:09 2024] ***
2024-07-30T10:51:09.887631+00:00 mmbella uwsgi[11121]: compiled with version: 13.2.0 on 17 April 2024 16:23:48
2024-07-30T10:51:09.887684+00:00 mmbella uwsgi[11121]: os: Linux-6.8.0-39-generic #39-Ubuntu SMP PREEMPT_DYNAMIC Fri Jul 5 21:49:14 UTC 2024
2024-07-30T10:51:09.895681+00:00 mmbella uwsgi[11123]: nodename: mmbella
2024-07-30T10:51:09.895722+00:00 mmbella uwsgi[11123]: machine: x86_64
2024-07-30T10:51:09.896222+00:00 mmbella uwsgi[11123]: clock source: unix
2024-07-30T10:51:09.896416+00:00 mmbella uwsgi[11123]: pcre jit disabled
2024-07-30T10:51:09.896546+00:00 mmbella uwsgi[11123]: detected number of CPU cores: 4
2024-07-30T10:51:09.896708+00:00 mmbella uwsgi[11123]: current working directory: /etc/uwsgi/apps-enabled
2024-07-30T10:51:09.897097+00:00 mmbella uwsgi[11123]: detected binary path: /usr/bin/uwsgi-core
2024-07-30T10:51:09.897282+00:00 mmbella uwsgi[11123]: chdir() to /home/marc/webdev/bellazzini
2024-07-30T10:51:09.897459+00:00 mmbella uwsgi[11123]: your processes number limit is 30115
2024-07-30T10:51:09.897598+00:00 mmbella uwsgi[11123]: your memory page size is 4096 bytes
2024-07-30T10:51:09.897700+00:00 mmbella uwsgi[11123]: detected max file descriptor number: 1024
2024-07-30T10:51:09.897824+00:00 mmbella uwsgi[11123]: lock engine: pthread robust mutexes
2024-07-30T10:51:09.898733+00:00 mmbella uwsgi[11122]: mapped 437520 bytes (427 KB) for 5 cores
2024-07-30T10:51:09.899025+00:00 mmbella uwsgi[11122]: *** Operational MODE: preforking ***
2024-07-30T10:51:09.899083+00:00 mmbella uwsgi[11122]: *** no app loaded. going in full dynamic mode ***
2024-07-30T10:51:09.899123+00:00 mmbella uwsgi[11122]: *** uWSGI is running in multiple interpreter mode ***
2024-07-30T10:51:09.899181+00:00 mmbella uwsgi[11122]: !!!!!!!!!!!!!! WARNING !!!!!!!!!!!!!!
2024-07-30T10:51:09.899221+00:00 mmbella uwsgi[11122]: no request plugin is loaded, you will not be able to manage requests.
2024-07-30T10:51:09.899263+00:00 mmbella uwsgi[11122]: you may need to install the package for your language of choice, or simply load it with --plugin.
2024-07-30T10:51:09.899303+00:00 mmbella uwsgi[11122]: !!!!!!!!!!! END OF WARNING !!!!!!!!!!
2024-07-30T10:51:09.899353+00:00 mmbella uwsgi[11122]: spawned uWSGI master process (pid: 11122)
2024-07-30T10:51:09.899454+00:00 mmbella uwsgi[11123]: thunder lock: disabled (you can enable it with --thunder-lock)
2024-07-30T10:51:09.899524+00:00 mmbella uwsgi[11121]: Tue Jul 30 10:51:09 2024 - [emperor] vassal donuts.ini has been spawned
2024-07-30T10:51:09.899606+00:00 mmbella uwsgi[11123]: uwsgi socket 0 bound to UNIX address /run/uwsgi/bellazzini.sock fd 3
2024-07-30T10:51:09.899647+00:00 mmbella uwsgi[11123]: setuid() to 1000
2024-07-30T10:51:09.899703+00:00 mmbella uwsgi[11123]: your server socket listen backlog is limited to 100 connections
2024-07-30T10:51:09.899744+00:00 mmbella uwsgi[11123]: your mercy for graceful operations on workers is 60 seconds
2024-07-30T10:51:09.900303+00:00 mmbella uwsgi[11121]: Tue Jul 30 10:51:09 2024 - [emperor] vassal donuts.ini is ready to accept requests
2024-07-30T10:51:09.900482+00:00 mmbella uwsgi[11122]: spawned uWSGI worker 1 (pid: 11124, cores: 1)
2024-07-30T10:51:09.900556+00:00 mmbella uwsgi[11122]: spawned uWSGI worker 2 (pid: 11125, cores: 1)
2024-07-30T10:51:09.901002+00:00 mmbella uwsgi[11123]: mapped 437520 bytes (427 KB) for 5 cores
2024-07-30T10:51:09.901106+00:00 mmbella uwsgi[11123]: *** Operational MODE: preforking ***
2024-07-30T10:51:09.901156+00:00 mmbella uwsgi[11123]: *** no app loaded. going in full dynamic mode ***
2024-07-30T10:51:09.901198+00:00 mmbella uwsgi[11123]: *** uWSGI is running in multiple interpreter mode ***
2024-07-30T10:51:09.901238+00:00 mmbella uwsgi[11123]: !!!!!!!!!!!!!! WARNING !!!!!!!!!!!!!!
2024-07-30T10:51:09.901282+00:00 mmbella uwsgi[11123]: no request plugin is loaded, you will not be able to manage requests.
2024-07-30T10:51:09.901323+00:00 mmbella uwsgi[11123]: you may need to install the package for your language of choice, or simply load it with --plugin.
2024-07-30T10:51:09.901364+00:00 mmbella uwsgi[11123]: !!!!!!!!!!! END OF WARNING !!!!!!!!!!
2024-07-30T10:51:09.901403+00:00 mmbella uwsgi[11123]: spawned uWSGI master process (pid: 11123)
2024-07-30T10:51:09.901479+00:00 mmbella uwsgi[11122]: spawned uWSGI worker 3 (pid: 11126, cores: 1)
2024-07-30T10:51:09.901544+00:00 mmbella uwsgi[11121]: Tue Jul 30 10:51:09 2024 - [emperor] vassal bellazzini.ini has been spawned
2024-07-30T10:51:09.901624+00:00 mmbella uwsgi[11122]: spawned uWSGI worker 4 (pid: 11128, cores: 1)
2024-07-30T10:51:09.901689+00:00 mmbella uwsgi[11123]: spawned uWSGI worker 1 (pid: 11127, cores: 1)
2024-07-30T10:51:09.901752+00:00 mmbella uwsgi[11121]: Tue Jul 30 10:51:09 2024 - [emperor] vassal bellazzini.ini is ready to accept requests
2024-07-30T10:51:09.901837+00:00 mmbella uwsgi[11122]: spawned uWSGI worker 5 (pid: 11129, cores: 1)
2024-07-30T10:51:09.901920+00:00 mmbella uwsgi[11123]: spawned uWSGI worker 2 (pid: 11130, cores: 1)
2024-07-30T10:51:09.901986+00:00 mmbella uwsgi[11123]: spawned uWSGI worker 3 (pid: 11131, cores: 1)
2024-07-30T10:51:09.902035+00:00 mmbella uwsgi[11123]: spawned uWSGI worker 4 (pid: 11132, cores: 1)
2024-07-30T10:51:09.902083+00:00 mmbella uwsgi[11123]: spawned uWSGI worker 5 (pid: 11133, cores: 1)
2024-07-30T10:51:20.417072+00:00 mmbella systemd[1]: Stopping nginx.service - A high performance web server and a reverse proxy server...
2024-07-30T10:51:20.440938+00:00 mmbella systemd[1]: nginx.service: Deactivated successfully.
2024-07-30T10:51:20.441271+00:00 mmbella systemd[1]: Stopped nginx.service - A high performance web server and a reverse proxy server.
2024-07-30T10:51:20.456252+00:00 mmbella systemd[1]: Starting nginx.service - A high performance web server and a reverse proxy server...
2024-07-30T10:51:20.504905+00:00 mmbella systemd[1]: Started nginx.service - A high performance web server and a reverse proxy server.
2024-07-30T10:51:26.068538+00:00 mmbella uwsgi[11132]: -- unavailable modifier requested: 0 --
2024-07-30T10:51:26.068870+00:00 mmbella uwsgi[11132]: announcing my loyalty to the Emperor...
2024-07-30T10:51:26.069877+00:00 mmbella uwsgi[11121]: Tue Jul 30 10:51:26 2024 - [emperor] vassal bellazzini.ini is now loyal

Patial log gave you before on uwsgi was from command line start of uwsgi because I could not find the uwsgi log

These lines concern me. For some reason, uwsgi is not seeing your app definition.

From what I’m seeing in your uwsgi file, your project-level dir is:
/home/marc/webdev/donuts. Your wsgi file would be in /home/marc/webdev/donuts/donuts/wsgi.py. Please confirm that this is accurate.

What I don’t see in your uwsgi file is a plugins = python3 line, nor do I see the parameter being passed in your systemd file. You might want to try adding it to the uwsgi file.

Hello Ken,

Yes the wsgi.py file is in that directory…


-rw-rw-r-- 1 marc marc 389 Jul 29 18:21 /home/marc/webdev/donuts/donuts/wsgi.py

Contents…


import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'donuts.settings')

application = get_wsgi_application()

I put the plugins = python3 into the donuts.ini file (was that where you wanted me to try?) daemon-reload and restarted both uwsgi and nginx. Still same error.

I think I am going to try and install gunicorn and see if I have better luck. This is the first time this has happened to me. I have been using ngingx and uwsgi for years without difficulty and copying over the ini files and systemd service files over to new projects (with edits of course) without difficulty. I vaguely remember someone else having a recent post about a 502 bad gateway. I wonder if there is a bug in the new version? Any other thoughts?

My most recent deployment consists of the following major packages:

Python 3.12.4

Package             Version
------------------- ------------
Django              5.0.6
django-crispy-forms 2.2
django-extensions   3.2.3
psycopg2-binary     2.9.9
uWSGI               2.0.26

(Many packages removed for brevity)

This works perfectly for me in a configuration virtually identical to yours. (I’ve got a couple not-critical parameters that you don’t, like a logto line.)

I also don’t use the emperor across different projects. I don’t remember why right off-hand, but I think it has to do with needing to use different Python and uwsgi versions between projects. (We create separate systemd or supervisord control files for each project. We find it easier to manage that way. Your mileage may vary.)

I see you’ve got a second app defined. Is there any possibility of conflict between the two? As a quick test, it might be worth disabling bellazzini in both your uwsgi and nginx configurations and seeing if donuts starts correctly.

(Also from that log file):

2024-07-30T10:51:26.068538+00:00 mmbella uwsgi[11132]: -- unavailable modifier requested: 0 --

From the docs at Things To Know:

A good symptom to recognize an unloaded plugin is messages like “Unavailable modifier requested” in your logs. If you are using distribution supplied packages, double check that you have installed the plugin for your language of choice.

If you did add the plugin line and restarted uwsgi, it might be worth reading through the logs again for updated information. It’s possible that one issue has been fixed to only uncover another.

Are you using the version of uwsgi for your python virtualenv, or are you using a system global uwsgi? (Are they different?)

Yes, I can see where gunicorn may be an easier deployment.

  1. Ensure that the /run/uwsgi/ directory and the socket file have the correct permissions
sudo chown -R marc:www-data /run/uwsgi
sudo chmod -R 775 /run/uwsgi

  1. Ensure that uWSGI is configured to log properly
[uwsgi]
project = donuts
uid = marc
base = /home/%(uid)

chdir = %(base)/webdev/%(project)
home = %(base)/Env/%(project)
module = %(project).wsgi:application

master = true
processes = 5

socket = /run/uwsgi/%(project).sock
chown-socket = %(uid):www-data
chmod-socket = 660
vacuum = true

# Logging
logto = /var/log/uwsgi/%(project).log

Once everything is configured and restarted, tail the logs to check for any errors or issues:

tail -f /var/log/uwsgi/donuts.log
tail -f /var/log/nginx/error.log

Thank you Anefta. I purged uwsgi and installed gunicorn and now it works. No further 502 error.

Hello Ken,

Thank you for the additional follow up. I gave up on uwsgi and installed gunicorn. 502 error solved and it works but now I am having issues with serving the static files. Never had these problems before in prior versions of nginx/django/uwsgi/ubuntu. Something has to be buggy. I will start a new post on the static file issue. Would appreciate your input as usual.