私の顧客が、XHR2 フォーム データ (および CORS によるクロスドメイン リクエスト) を使用してフォーム POST 経由でリモート nginx Web サーバーにファイルをアップロードしようとしています。アップロードの途中で、Web サーバーは 408 を返し、その結果、Ajax エラー ハンドラーが処理を停止します。ファイルのサイズは 20 ~ 120 MB の範囲です。一部のファイル アップロードのアクセス ログは次のとおりです (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"
IE ではなく Chrome を使用するとファイルが問題なくアップロードされる場合もありますが、その逆の場合もあります。しかし、ほとんどの場合、どちらのブラウザも機能しません。
私は nginx wiki を読んでいましたが、408 エラーと相関関係にある設定は と の 2 つだけですclient_body_timeout
。client_header_timeout
この 2 つのディレクティブの意味を理解するのに苦労しています。両方とも 180 秒に増やしましたが、問題は解決しません。接続が遅いかどうか尋ねたところ、2.5 mbps のアップで、リクエスト ヘッダーを完全に受信するには十分な速度であるはずだと言いました (ただし、この 2 つのディレクティブが何を意味するのかは、ウィキ(「readstep」とは何かなど)。これまでにも他のお客様から 1GB のアップロードを弊社のサーバーに正常に受信したことがありますが、完了するまでに通常 1 時間ほどかかります。
最終的にお客様から正常に受け取った問題のあるファイルについては、さまざまなブラウザで同じファイルのアップロードを試みましたが、問題なく動作しました。
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 エラーを引き起こす可能性のある設定はありますか? この問題についてはエラー ログに何も記載されていません。
注: nginx 構成に変更を加える場合にのみ使用しますreload
。 を避けようとしていますrestart
が、構成の変更を完全に有効にするために必要な場合は、それを実行します。