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

502 bad gateway upstream prematurely closed connection while reading response header from upstream
app engine upstream prematurely closed connection while reading response header from upstream
artifactory nginx upstream prematurely closed connection while reading upstream
php-fpm upstream prematurely closed connection while reading response header from upstream
upstream prematurely closed fastcgi stdout while reading response header from upstream
keepalive_timeout nginx 502
uwsgi timeout
upstream timed out (110: connection timed out) while reading response header from upstream uwsgi

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

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.

nginx uwsgi websockets 502 Bad Gateway upstream prematurely , nginx uwsgi websockets 502 Bad Gateway upstream prematurely closed connection while reading response header from upstream · django  TL;DR. What http/tcp phenomenon is occurring when Nginx logs upstream prematurely closed connection while reading response header from upstream when attempting to reverse proxy over HTTP to a local Django instance (with no WSGI middleware)?


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

uWSGI and NGINX 502: upstream prematurely closed connection , restrict process virtual memory size and may be responsible for 502 error code with message "upstream prematurely closed connection". "upstream prematurely closed connection while reading response header from upstream" My script continues to run and completes after 70 seconds, however my browser connection has died before then (502 error).


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;
}

What is solution of "120 upstream prematurely closed connection , gatway error and tail has given this error log - *120 upstream prematurely closed connection while reading response header from upstream. about getting a Flask app up and running with Gunicorn and Nginx, check out:. Hi list My topology is : client ---> nginx 1.6.2 (port 80) ---> nginx 0.7.69 with mogilefs module (port 2080) ---> mogilefs . I want to upload a 8G file to mogilefs , the uploading URL is


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

uWSGI, I have NGINX connecting to it via uWSGI. I have set “uwsgi_read_timeout 90;”. uWSGI - upstream prematurely closed connection while reading response header from upstream · Server · NGINX with a 504 error as expected. I don't quite understand what is catching the 502 bad gateway error even Dismiss Join GitHub today. GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.


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.

first install does not work 502 Bad Gateway - Administrators, The 3 services go up but display the 502 Bad Gateway error. Check your logfiles, that sounds like uwsgi isn't running or nginx/apache cannot connect to it. 2018/09/25 13:33:41 [error] 6861#0: *142 upstream prematurely closed connection while reading response header from upstream, client: 10.0.1.61  Hi list, we are seeing sporadic nginx errors "upstream prematurely closed connection while reading response header from upstream" with nginx/1.6.2 which seems to be some kind of race condition.


[uWSGI] Help with nginx "prematurely closed connection" error , a Django application with uwsgi - every request results in a nginx error closed connection while reading response header from upstream,  502 Bad Gateway upstream prematurely closed connection while reading response header from upstream with flask, uWSGI, nginx Hot Network Questions How much data will 12550824 x 12550824 world take up in minecraft?


NGINX 502 Bad Gateway: Gunicorn, Learn to troubleshoot 502 errors that can occur when you combine NGINX NGINX will return a 502 Bad Gateway error if it can't successfully proxy a that prompts NGINX to include proxy information in the headers of its requests, *17 upstream prematurely closed connection while reading response  Damn it. I wanted to move to gunicorn because with Apache2+mod_wsgi, I have the same behavior (always on the same group of page I realize now), and reading apache2 log just gives Truncated or oversized response headers received from daemon process with a wonderful Segmentation fault (11).


Error 502:django-Nginx-Uwsgi - Questions, Hi, There is an error aleatory on page with Django/nginx/uwsgi. the log show: upstream prematurely closed connection while reading response  Hi all, I recently released an app on AWS ECS using "uwsgi-nginx-flask: python3.7" The problem is that sporadically, without any reason, the server starts responding with "upstream prematurely closed connection while reading response".