[Django]-Error: upstream prematurely closed connection while reading response header from upstream [uWSGI/Django/NGINX]

16👍

This is unlikely to be an nginx config issue.

It’s almost certainly that the backend is actually crashing (or just terminating the connection) rather than giving a malformed response. i.e. the error message is telling you what the problem is, but you’re looking in the wrong place to solve it.

You don’t give enough information to allow use to figure out what the exact issue is but if I had to guess:

which usually returns 872 rows and takes 2.07 to run in MySQL. It is however returning a LOT of information.

It’s either timing out somewhere or running out of memory.

👤Danack

7👍

I had the same issue, what fixed it for me is adding my domain in the
settings.py e.g.:

ALLOWED_HOSTS = ['.mydomain.com', '127.0.0.1', 'localhost']

By same issue, I mean I couldn’t even load the page, nginx would return a 502 without serving any pages where I could cause the application to crash.

And the nginx log contained:

Error: upstream prematurely closed connection while reading response header from upstream

2👍

In your @django location block you can try adding some proxy read and connect timeout properties. e.g.

location @django {
   proxy_read_timeout 300;
   proxy_connect_timeout 300;
   proxy_redirect off;

   # proxy header definitions
   ...
   proxy_pass http://django;
}

0👍

Sometimes it may be an authority problem. Check the project directory’s authority.

👤jhd

0👍

It might be an uwsgi configuration issue instead of Nginx. I saw that you had uwsgi processes = 2 and harakiri = 120, have you tried changing those as well as other fields there one by one?

I had same issue but it wasn’t my NGINX configuration, it was my UWSGI processes causing timeout errors when I posted JSONs from client side to server. I had processes as 5, I changed it to 1 and it solved the issue. For my application, I only needed to have 1 process run at time.

Here is the working UWSGI configuration autoboot ini file that solved the timeout issue and thus the 502 gateway issue (upstream closed prematurely).

autoboot.ini

#!/bin/bash

[uwsgi]
socket          = /tmp/app.sock

master          = true

chmod-socket    = 660
module          = app.wsgi
chdir           = home/app

close-on-exec = true # Allow linux shell via uWSGI

processes = 1
threads = 2
vacuum = true

die-on-term = true

Hope it helps.

0👍

Try this uwsgi config (uwsgi.ini):

[uwsgi]
 master          = true 
 socket          = /home/ubuntu/uwsgi.sock
 chmod-socket    = 666
 chdir           = /home/ubuntu/project
 wsgi-file       = /home/ubuntu/project/project/wsgi.py
 virtualenv      = /home/ubuntu/virtual
 vacuum          = true
 enable-threads  = true
 daemonize       = /home/ubuntu/logs/uwsgi.log

And run uwsgi –ini uwsgi.ini

and update nginx config to connect to the socket created

server {

   listen 80;
   server_name www.domain.com;



       location /static {
       
       alias /home/ubuntu/project/static;
       }

       location /media {
       
       alias /home/ubuntu/project/media;
       }
       
       location / {
       uwsgi_pass unix:///home/ubuntu/uwsgi.sock;
       include uwsgi_params;

        }

}

👤Nijo

Leave a comment