3

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 (e.g. removing the tailing slash from my proxy_pass

3
  • 3
    Your Node application failed to respond and dropped the connection. That is where you should be looking. May 20, 2014 at 15:04
  • My node application is fine. It's working fine locally and I'm using the exact same repo with all the same dependencies. In addition, my error logs are clean.
    – bob_cobb
    May 21, 2014 at 6:06
  • make sure you have all the limits bumped up - ram, space, timeouts, like proxy_read_timeout 7200; and proxy_connect_timeout 7200;
    – ADM
    May 22, 2014 at 17:38

5 Answers 5

2

Might want to check with your Node Application see if its reporting any errors? In the past using nginx with php file uploads i would get the occasional 502 due to the size of my file so i increased it in the master nginx.conf using the following '#Body Size client_max_body_size 900m;' hope that helps

4
  • Ah tried that. Did not work, unfortunately.
    – bob_cobb
    May 21, 2014 at 6:06
  • This answer has two parts. My first guess was also a limit on the request body size, but there's lots of possibilities. The key point is that the upstream app that's failing, is likely to have logged an error.
    – mc0e
    May 22, 2014 at 18:46
  • Ok, so it looks like if I remove the image cropping with imagemagick it works fine. There has to be an issue with imagemagick and nginx, I would assume. Now I just need to know how to fix it.
    – bob_cobb
    May 24, 2014 at 23:46
  • Ah, so it turned out to be the app after all. Oct 19, 2020 at 19:21
0

Your error message sounds to me like your app is probably accepting the whole of the request, and then dying before it's constructed a response, and if that's so, then it's your app you need to debug, not nginx. Check the assumptions though - verify it directly.

Snoop on the traffic (eg with tcpdump, ngrep and/or wireshark) to check that nginx is successfully connecting to your app and passing it the whole request. The results of this check will confirm which end of the connection you need to debug.

It might be worth testing whether the failure is at all dependent on the size of the request. Try it with a really tiny file.

4
  • Ok, so it looks like if I remove the image cropping with imagemagick it works fine. There has to be an issue with imagemagick and nginx, I would assume. Now I just need to know how to fix it.
    – bob_cobb
    May 24, 2014 at 23:43
  • I very much doubt that has anything to do with nginx.
    – mc0e
    May 26, 2014 at 23:04
  • Increasing the keepalive worked, but it was a problem whenever I tried to resize an image. So yes, they were related.
    – bob_cobb
    May 27, 2014 at 1:34
  • That doesn't make much sense. Keepalive relates to waiting between requests, whereas you're reporting an error happening while a request is underway. Maybe you have two different problems and are confusing symptoms between them.
    – mc0e
    May 28, 2014 at 7:46
0

what about tuning the following nginx directives?

  • proxy_buffer_size
  • proxy_busy_buffers_size
  • proxy_temp_file_write_size
  • proxy_max_temp_file_size
1
  • 1
    Can you explain how and why one would want to tune these directives? May 28, 2014 at 14:26
0

I had the same issue and apart from client_max_body_size, I had to change these values:

client_body_timeout
client_header_timeout

These are responsible for the time a server will wait for a client body or client header to be sent after request.

Also, you might want to check:

keepalive_timeout
send_timeout
0

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/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .