502 Bad Gateway, upstream prematurely closed.

Posted September 5, 2016 21.1k views

There is an error on two pages. How can i fix 502 Bad Gateway? Thanks for help. Have a nice day… - 502 error, 178 images - 502 error, 131 images - no Error, 286 images
Nginx Error log:

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

File: /etc/nginx/nginx.conf

worker_processes 4;
pid /run/;

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/

upstream luxger_app_server {
   server unix:/var/www/ 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/;


        access_log /var/www/;
        error_log /var/www/;

    keepalive_timeout 0;
    large_client_header_buffers 8 32k;

    location /static/ {
        alias   /var/www/;

    location /media/ {
        alias   /var/www/;

    location /static/admin {
        alias   /var/www/;

        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?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
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.