我正在运行nginx / ruby-on-rails,我有一个简单的多部分表单来上传文件 . 一切正常,直到我决定限制我想要上传的文件的最大大小 . 为此,我将nginx client_max_body_size
设置为1m(1MB),并在该规则中断时响应HTTP 413(Request Entity Too Large)状态 .
The problem 是当我上传一个1.2 MB的文件,而不是显示HTTP 413错误页面时,浏览器挂起一点然后死于"Connection was reset while the page was loading"消息 .
我已经尝试过nginx提供的每一个选项,似乎没什么用 . 有没有人对此有任何想法?
这是我的nginx.conf:
worker_processes 1;
timer_resolution 1000ms;
events {
worker_connections 1024;
}
http {
passenger_root /the_passenger_root;
passenger_ruby /the_ruby;
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.x.com;
client_max_body_size 1M;
passenger_use_global_queue on;
root /the_root;
passenger_enabled on;
error_page 404 /404.html;
error_page 413 /413.html;
}
}
Thanks.
Edit
环境/ UA:Windows XP / Firefox 3.6.13
3 回答
nginx "fails fast"当客户端通过发送413响应并关闭连接通知它将发送大于
client_max_body_size
的主体 .大多数客户端在发送整个请求主体之前不会读取响应 . 因为nginx关闭了连接,所以客户端将数据发送到已关闭的套接字,从而导致TCP RST .
如果您的HTTP客户端支持它,处理此问题的最佳方法是发送
Expect: 100-Continue
标头 . 从1.2.7开始,Nginx正确支持此功能,如果Content-Length
超过最大主体大小,则会回复413 Request Entity Too Large
响应而不是100 Continue
.你的上传到底死了吗?崩溃前99%?客户端主体和缓冲区是关键,因为nginx必须缓冲传入的数据 . body配置(请求体的数据)指定nginx如何处理从多部分表单客户端到应用程序逻辑的二进制数据的批量流 .
clean
设置通过指示nginx将传入缓冲区存储在文件中,然后通过删除它从磁盘清除此文件来释放内存和消耗限制 .将
body_in_file_only
设置为clean
并调整client_max_body_size
的缓冲区 . 原始问题的配置已经有sendfile,也增加了超时 . 我使用下面的设置来修复此问题,适用于本地配置,服务器和http上下文 .来自the documentation:
我怀疑这是正在发生的事情,如果你使用Firebug或Live HTTP Headers(两个Firefox扩展)等工具来检查HTTP来回,你真的会继续 .