Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am currently ALWAYS getting a 502 on a query my users are doing... which usually returns 872 rows and takes 2.07 to run in MySQL. It is however returning a LOT of information. (Each row contains a lot of stuff). Any ideas?

Running the Django (tastypie Rest API), Nginx and uWSGI stack.

Server Config with NGINX

# the upstream component nginx needs to connect to
upstream django {
    server unix:///srv/www/poka/app/poka/nginx/poka.sock; # for a file socket
}

# configuration of the server
server {
    # the port your site will be served on
    listen  443;


    # the domain name it will serve for
    server_name xxxx; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 750M;   # adjust to taste

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /srv/www/poka/app/poka/nginx/uwsgi_params; # the uwsgi_params file you installed
    }
}

UWSGI config

# process-related settings
# master
master          = true
# maximum number of worker processes
processes   = 2
# the socket (use the full path to be safe
socket          = /srv/www/poka/app/poka/nginx/poka.sock
# ... with appropriate permissions - may be needed
chmod-socket    = 666
# clear environment on exit
vacuum          = true

pidfile = /tmp/project-master.pid # create a pidfile
harakiri = 120 # respawn processes taking more than 20 seconds
max-requests = 5000 # respawn processes after serving 5000 requests
daemonize = /var/log/uwsgi/poka.log # background the process & log
log-maxsize = 10000000
#http://uwsgi-docs.readthedocs.org/en/latest/Options.html#post-buffering
post-buffering=1
logto = /var/log/uwsgi/poka.log # background the process & log
like image 218
abisson Avatar asked Feb 28 '14 23:02

abisson


2 Answers

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.

like image 179
Danack Avatar answered Nov 02 '22 20:11

Danack


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
like image 6
emazzotta Avatar answered Nov 02 '22 21:11

emazzotta