502 Bad Gateway, upstream prematurely closed.

September 5, 2016 9.5k views
Django Nginx Debian

There is an error on two pages. How can i fix 502 Bad Gateway? Thanks for help. Have a nice day…
http://luxger.com/galeri/motif - 502 error, 178 images
http://luxger.com/galeri/gokyuzu - 502 error, 131 images
http://luxger.com/galeri/gokyuzu-agac-cicek - no Error, 286 images
Nginx Error log:

...upstream prematurely closed connection while reading response header from upstream, client:, server: www.luxgergitavan.com, request: "GET /galeri/motif HTTP/1.1",... 

File: /etc/nginx/nginx.conf

worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 768;
http {

    # Basic Settings
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    server_names_hash_bucket_size 128;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    # Logging Settings

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log warn;

    # Gzip Settings

    gzip on;
    gzip_disable "msie6";

    # Virtual Host Configs

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-available/*;


File: /etc/nginx/sites-available/luxger.com

upstream luxger_app_server {
   server unix:/var/www/luxger.com/run/gunicorn.sock fail_timeout=0;


server {
        listen 80;
        #listen [::]:80 default_server ipv6only=on;
        client_max_body_size 4G;
        sendfile on;
        keepalive_timeout 0;
        large_client_header_buffers 8 32k;

        root /var/www/luxger.com/;

        server_name www.luxgergitavan.com luxgergitavan.com luxger.com www.luxger.com;

        access_log /var/www/luxger.com/logs/nginx-access.log;
        error_log /var/www/luxger.com/logs/nginx-error.log;

    keepalive_timeout 0;
    large_client_header_buffers 8 32k;

    location /static/ {
        alias   /var/www/luxger.com/static/;

    location /media/ {
        alias   /var/www/luxger.com/media/;

    location /static/admin {
        alias   /var/www/luxger.com/static/admin/;

        location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
                proxy_buffers 8 32k;
        proxy_buffer_size 64k;
        if (!-f $request_filename) {
            proxy_pass http://luxger_app_server;

        #error_page 404 /404.html;

        # redirect server error pages to the static page /50x.html
        #error_page 500 502 503 504 /50x.html;
        #location = /50x.html {
        #       root /usr/share/nginx/html;
1 comment
  • Nginx is reporting that the upstream service is closing connections before sending data. Unfortunately you have only included details on your nginx configuration and nothing on the app behind it that appears to be the source of the issue. What is nginx acting as a proxy for in your configuration?

1 Answer

Sorry to see that your question hasn’t received an answer yet. Unfortunately, after this much time, it is unlikely that an answer for this specific question will be provided. For people still landing here via search, you will want to check the upstream service’s (Gunicorn, php-fpm, NodeJS, etc.) logs and see why it is returning an error.

Have another answer? Share your knowledge.