首页 > 解决方案 > 通过 nginx 上传 1 GB 文件的问题

问题描述

我们使用 nginx 作为 Sails 服务器运行后的负载均衡器。我们必须将 1 Gb 文件上传到服务器。我们使用 Angular js 作为前端。当我们将 1 GB 文件上传到服务器时,它显示上传进度为 99%,然后显示400错误代码。

在查看它显示的错误日志时client prematurely closed connection

我们尝试了client_body_in_file_only模块upload。但失败了

我们如何解决这个问题?

我们的 Nginx 配置是

    load_module modules/ngx_http_upload_module.so;
    user www-data;
    worker_processes auto;
    pid /run/nginx.pid;

    events {
        worker_connections 768;
    }

    http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 360;
        types_hash_max_size 2048;
        proxy_ignore_client_abort on;
        proxy_buffering off;


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

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

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

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";


    upstream api{
                    ip_hash;
                    server 127.0.0.1:1337;
            }

    server {
            client_max_body_size 2048M;
            client_body_buffer_size 2048M;
            client_body_timeout 50m;
            client_header_timeout 50m;
            send_timeout 50m;

                listen 80;
            #   location /test/upload {
            #                add_header 'Access-Control-Allow-Origin' *;
            #                #add_header 'Access-Control-Allow-Headers' 'Content-Type';
            #       add_header 'Access-Control-Allow-Credentials' 'true' always;
            #                add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
            #                add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With' always;
            #                # required to be able to read Authorization header in frontend
            #                add_header 'Access-Control-Expose-Headers' 'Authorization' always;

            #                # Pass altered request body to this location
            #                upload_pass   @upload;
            #
            #                # Store files to this directory
            #                # The directory is hashed, subdirectories 0 1 2 3 4 5 6 7 8 9 should exist
            #                upload_store /tmp;
            #
            #                # Allow uploaded files to be read only by user
            #                upload_store_access user:r;
            #
            #                # Set specified fields in request body
            #                upload_set_form_field $upload_field_name.name "$upload_file_name";
            #                upload_set_form_field $upload_field_name.content_type "$upload_content_type";
            #                upload_set_form_field $upload_field_name.path "$upload_tmp_path";
            #
            #                # Inform backend about hash and size of a file
            #                upload_aggregate_form_field "$upload_field_name.md5" "$upload_file_md5";
            #                upload_aggregate_form_field "$upload_field_name.size" "$upload_file_size";
            #
            #                upload_pass_form_field "^submit$|^description$|^account$";
            #
            #                upload_cleanup 400 404 499 500-505;
            #        }
                location @upload {
                    proxy_send_timeout 50m;
                            proxy_read_timeout 50m;
                            proxy_pass   http://api;
                    proxy_http_version 1.1;
                            proxy_redirect     off;
                            proxy_set_header Upgrade $http_upgrade;
                            proxy_set_header Connection "upgrade";
                            proxy_set_header  Host              $host;
                            proxy_set_header  X-Forwarded-For   $proxy_add_x_forwarded_for;
                            proxy_set_header  X-Real-IP         $remote_addr;
                    }
                    location / {
                    proxy_send_timeout 50m;
                            proxy_read_timeout 50m;
                            proxy_pass http://api;
                            proxy_http_version 1.1;
                            proxy_redirect     off;
                            proxy_set_header Upgrade $http_upgrade;
                            proxy_set_header Connection "upgrade";
                            proxy_set_header  Host              $host;
                            proxy_set_header  X-Forwarded-For   $proxy_add_x_forwarded_for;
                            proxy_set_header  X-Real-IP         $remote_addr;
                    }
                location /test/upload {
                            proxy_send_timeout 50m;
                            proxy_read_timeout 50m;
                            proxy_pass http://api;
                            proxy_http_version 1.1;
                            proxy_redirect     off;
                            proxy_set_header Upgrade $http_upgrade;
                            proxy_set_header Connection "upgrade";
                            proxy_set_header  Host              $host;
                            proxy_set_header  X-Forwarded-For   $proxy_add_x_forwarded_for;
                            proxy_set_header  X-Real-IP         $remote_addr;
                    client_body_in_file_only clean;
                    client_body_buffer_size 16K;
                    client_body_temp_path /tmp;
                    }

            }

    }

标签: node.jsnginx

解决方案


推荐阅读