Nginx 在上傳 XHR2 檔案時傳回 408 錯誤

Nginx 在上傳 XHR2 檔案時傳回 408 錯誤

我的一個客戶正在嘗試使用 XHR2 表單資料(以及使用 CORS 的跨網域請求)透過表單 POST 將檔案上傳到我們的遠端 nginx Web 伺服器。在上傳過程中,網頁伺服器傳回 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"

有時,使用 Chrome 而不是 IE 可以很好地上傳文件,有時反之亦然,但大多數時候這兩種瀏覽器都不適合他。

我一直在閱讀 nginx wiki,與 408 錯誤相關的唯一兩個設定是client_body_timeoutclient_header_timeout。我很難理解這兩個指令的意思。我將兩者都增加到 180 秒,但問題仍然存在。我問他連接是否很慢,他說他有 2.5 mbps,這應該足夠快以完全接收請求標頭(但同樣,根據維基百科,例如什麼是“readstep”)。我們之前已經成功從其他客戶收到了 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/*;


}

我們的上傳伺服器配置:

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

配置中是否有任何內容可能會阻礙成功上傳並導致這些討厭的 408 錯誤?錯誤日誌中沒有提及有關此問題的內容。

reload注意:我們僅在更改 nginx 配置時使用。我們正在努力避免,restart但如果我們的配置變更需要完全生效,那麼我們就會這樣做。

相關內容