Nginx 在上传过程中返回 XHR2 文件上传的 408 错误

dan*_*oon 6 nginx upload ajax http-status-code-408

我的一位客户正尝试使用 XHR2 表单数据(以及带有 CORS 的跨域请求)通过表单 POST 将文件上传到我们的远程 nginx 网络服务器。在上传过程中,网络服务器返回 408 并且 ajax 错误处理程序因此停止处理。这些文件在 20-120MB 范围内。部分文件上传的访问日志如下(他在Chrome 31和IE11上都试过):

[24/Dec/2013:16:44:18 -0500] "OPTIONS / HTTP/1.1" 200 0 "http://www.example.com/files/upload" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36"
[24/Dec/2013:16:47:50 -0500] "POST / HTTP/1.1" 408 0 "http://www.example.com/files/upload" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36"
...
[27/Dec/2013:01:23:51 -0500] "OPTIONS / HTTP/1.1" 200 0 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"
[27/Dec/2013:01:33:11 -0500] "POST / HTTP/1.1" 408 0 "http://www.example.com/files/upload" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"
Run Code Online (Sandbox Code Playgroud)

有时使用 Chrome 而不是 IE 上传文件对他来说非常好,有时反之亦然,但大多数时候这两种浏览器都不适合他。

我一直在阅读 nginx wiki,唯一与 408 错误相关的两个设置是client_body_timeoutclient_header_timeout。我很难理解这两个指令的含义。我将两者都增加到 180 秒,但问题仍然存在。我问他是否连接速度很慢,他说他有 2.5 mbps,这应该足够快以完全接收请求标头(但同样,我们不确定这 2 个指令根据维基的含义,例如就像“阅读步骤”是什么一样)。我们之前已经成功地从其他客户那里接收到 1GB 上传到我们的服务器,这通常需要大约一个小时才能完成。

对于我们最终从客户那里成功收到的有问题的文件,我们尝试在各种浏览器中上传相同的文件,并且效果很好。

我读过使用 SSL 可能会导致超时,但服务器没有启用 SSL,我们只使用 http。

我们的 nginx.conf:

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

events {
    worker_connections 5000;
    # multi_accept on;
}

http {
    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay off;
    keepalive_timeout 25;

    # Increase client/head body_timeout to prevent 408's for slow Internet connections??
    client_header_timeout 180;
    client_body_timeout 180;


    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;

    ##
    # Logging Settings
    ##

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

        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 5;
        gzip_min_length 256;

        gzip_types
        application/atom+xml
        text/javascript
        application/javascript
        application/json
        application/rss+xml
        application/vnd.ms-fontobject
        application/x-font-ttf
        application/x-web-app-manifest+json
        application/xhtml+xml
        application/xml
        font/opentype
        image/svg+xml
        image/x-icon
        text/css
        text/plain
        text/x-component; 

        # Increase FastCGI buffers
        fastcgi_read_timeout 1500;
        fastcgi_buffers 8 16K;
        fastcgi_buffer_size 32K;

    ##
    # Virtual Host Configs
    ##
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;


}
Run Code Online (Sandbox Code Playgroud)

我们的上传服务器配置:

server {
    listen 80;
    server_name upload.example.com;

    root /var/www/releases/latest/_UPLOAD/public;

    # remove trailing slash, that throws ZF router
    if (!-d $request_filename) {
        rewrite ^/(.*)/$ /$1 permanent;
    }

    # 1.2G upload limit + 10M for post data (which is extremely liberal)
    client_max_body_size 1210M;
    client_body_buffer_size 4M;    

    proxy_max_temp_file_size 0;    

    location = /favicon.ico {
        access_log     off;
        log_not_found  off;
    }

    location = /apple-touch-icon.png {
        access_log off;
        log_not_found off;
    }

    location / {
        # This is for AJAX file uploads... need to set CORS headers
        if ($request_method = OPTIONS ) {
            add_header Access-Control-Allow-Origin '$scheme://www.example.com';
            add_header Access-Control-Allow-Methods 'POST, OPTIONS';
            add_header Access-Control-Allow-Headers 'X-Requested-With, Content-Type, Content-Range, Content-Disposition';
            return 200;
        }

        try_files $uri $uri/ /index.php?$args;
        index index.php index.html index.htm;
    }

    location ~ \.php$ {
        try_files $uri $uri/ /index.php?$args;
        index index.php index.html index.htm;

        fastcgi_pass   unix:/var/run/php5-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/releases/latest/_UPLOAD/public$fastcgi_script_name;
        fastcgi_param  APPLICATION_ENV production;
        fastcgi_param  PATH /usr/bin:/bin:/usr/sbin:/sbin;
        fastcgi_intercept_errors on;
        include        fastcgi_params;
    }

    error_page 403 =404 /404.html;
    error_page 404 /404.html;
    location = /404.html {
        root /var/www/releases/latest/_UPLOAD/public;
        internal;
    }

    error_page 500 502 503 504 = /50x.html;
    location = /50x.html {
        root /var/www/releases/latest/_UPLOAD/public;
        internal;
    }
}
Run Code Online (Sandbox Code Playgroud)

配置中是否有任何内容可能会阻碍成功上传并导致这些讨厌的 408 错误?错误日志中未提及有关此问题的任何内容。

注意:我们只reload在对 nginx 配置进行更改时使用。我们正在努力避免,restart但如果我们的配置更改需要完全生效,那么我们会这样做。