简体   繁体   中英

Nginx 502 Bad Gateway when uploading files

I get the following error when I try to upload files to my node.js based web app:

2014/05/20 04:30:20 [error] 31070#0: *5 upstream prematurely closed connection while reading response header from upstream, client: ... [clipped]

I'm using a front-end proxy here:

  upstream app_mywebsite {
      server 127.0.0.1:3000;
  }

  server {
      listen 0.0.0.0:80;
      server_name {{ MY IP}} mywebsite;
      access_log /var/log/nginx/mywebsite.log;

      # pass the request to the node.js server with the correct headers and much more can be added, see nginx config options
      location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://app_mywebsite;
        proxy_redirect off;
    # web socket support
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
      }
   }

This is my nginx.conf file:

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 2048;
    multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 20;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    # default_type application/octet-stream;
    default_type text/html;
    charset UTF-8;

    ##
    # Logging Settings
    ##

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

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    gzip_vary on;
    gzip_proxied any;
    gzip_min_length 256;
    gzip_comp_level 5;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##

    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

Any idea on how to better debug this? The things I've found haven't really worked (eg removing the tailing slash from my proxy_pass

Try adding the following to your server{} block, I was able to solve an Nginx reverse proxy issue by defining these proxy attributes:

# define buffers, necessary for proper communication to prevent 502s
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;

The issue may be caused by PM2. If you're enabled watching, the app will restart on every single file change(and new uploads too). The solution could be disabling watching completely or adding the uploads folder to ignore list. More: https://pm2.keymetrics.io/docs/usage/watch-and-restart/

So in the end I ended up changing in my keepalive from 20 to 64 and it seems to handle large files fine now. The bummer about it is that I re-wrote from scratch the image upload library I was using node-imager , but at least I learned something from it.

server {
  location / {
    keepalive 64
  }
}

Try this:

client_max_body_size - Maximum uploadable file size

http {
    send_timeout                10m;
    client_header_timeout       10m;
    client_body_timeout         10m;
    client_max_body_size        100m;

    large_client_header_buffers 8 32k;
}

and server section:

server {
    location / {
        proxy_buffer_size       32k;
    }
}

large_client_header_buffers 8 32k and proxy_buffer_size 32k - is enough for most scripts, but you can try 64k, 128k, 256k...

(sorry, im not english speaking) =)

Try adding the following below to the http section of your /etc/nginx/nginx.conf :

fastcgi_read_timeout 400s;

and restart nginx.

Futher reading: nginx docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM