上传文件时Nginx 502 Bad Gateway

时间:2014-05-20 05:04:38

标签: node.js nginx webserver

当我尝试将文件上传到基于node.js的web应用程序时出现以下错误:

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

我在这里使用前端代理:

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

这是我的nginx.conf文件:

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

有关如何更好地调试此问题的任何想法?我发现的东西并没有真正发挥作用(例如从我的proxy_pass中删除拖尾斜杠

5 个答案:

答案 0 :(得分:5)

尝试将以下内容添加到server{}块中,我可以通过定义这些代理属性来解决Nginx反向代理问题:

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

答案 1 :(得分:1)

所以最后我最终将我的保持活动从20改为64,现在好像处理大文件了。令人沮丧的是,我从头开始重新编写了我使用node-imager的图片上传库,但至少我从中学到了一些东西。

server {
  location / {
    keepalive 64
  }
}

答案 2 :(得分:1)

该问题可能是由PM2引起的。如果您启用了观看功能,则该应用程序将在每次文件更改(也包括新上传的内容)时重新启动。解决方案可能是完全禁用观看功能,或者添加上载文件夹以忽略列表。 更多:https://pm2.keymetrics.io/docs/usage/watch-and-restart/

答案 3 :(得分:0)

试试这个:

client_max_body_size - 最大可上传文件大小

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

    large_client_header_buffers 8 32k;
}

和服务器部分:

server {
    location / {
        proxy_buffer_size       32k;
    }
}

large_client_header_buffers 8 32k proxy_buffer_size 32k - 对于大多数脚本来说已经足够了,但你可以试试64k,128k,256k ......

(对不起,我不是说英语) =)

答案 4 :(得分:0)

尝试将以下内容添加到http的{​​{1}}部分:

/etc/nginx/nginx.conf

并重新启动nginx。

进一步阅读:nginx docs