我使用Nginx作为Web服务器来服务我的网站.
我向我的用户提供上传功能(他们可以提交高达5Mb的图片)所以我有指令:client_max_body_size 5M;在我的服务器配置中.
我注意到的是,如果我尝试上传任何文件,网络服务器不会阻止上传更大的文件.例如,假设我尝试上传700Mb的真正大视频(电影).服务器不会立即拒绝上传,但它会缓冲整个数据(花费这么长时间并减慢服务器速度),并且只有在上传结束时它才会返回413 Request实体太大的错误.
所以问题是:当传输的数据开始克服我的client_max_body_size限制时,有没有办法正确配置Nginx来阻止大文件上传?
我认为用我的实际设置继续制作是非常不安全的,我无法在谷歌上找到任何有用的东西.
编辑:
我使用php和Symfony2作为后端……
编辑(再次):
这是我的error.log中出现的内容:
2013/01/28 11:14:11 [error] 11328#0: *37 client intended to send too large body: 725207449 bytes,client: 33.33.33.1,server: www.local.example.com,request: "POST /app_dev.php/api/image/add/byuploader HTTP/1.1",host: "local.example.com",referrer: "http://local.example.com/app_dev.php/"`
奇怪的是我用tail -f error.log监视我的nginx error.log,并在上传开始时(在它结束之前)立即显示消息.因此nginx会进行某种预防性检查,但它不会停止/分块上传请求……
我还试图验证php是否通过发出echo’something’来控制上传;死();在页面上谁处理上传但它没有打印任何东西,并没有停止上传请求.所以它应该是nginx或一些php内部使整个上传继续,直到它完全传输…
另一个编辑:
这是我的顶级视图(监视nginx和php)正在进行大文件上传(nginx过载):
编辑:
这是我的nginx配置
server {
listen 80;
server_name www.local.example.com local.example.com;
access_log /vagrant/logs/example.com/access.log;
error_log /vagrant/logs/example.com/error.log;
root /vagrant/example.com/web;
index app.php;
client_max_body_size 6M;
location /phpmyadmin {
root /usr/share;
index index.php;
location ~* \.php {
fastcgi_intercept_errors on;
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
}
location / {
try_files $uri @rewriteapp;
}
location @rewriteapp {
rewrite ^(.*)$/app.php/$1 last;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ ^/(app|app_dev)\.php(/|$) {
fastcgi_intercept_errors on;
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
}
最佳答案
有用信息:https://stackoverflow.com/questions/4947107/nginx-upload-client-max-body-size-issue
我引用了一些最有趣的答案(就我而言):
Most clients don’t read for responses until the entire request body is sent. Because nginx closes the connection the client sends data to the closed socket,causing a TCP RST.
如果是这样,它只是一个浏览器问题,不会影响(重载)我的Web服务器.它似乎抱怨我的内存检查表明,在上传大型(700mb)文件时,nginx和php-fastcgi都没有真正超载.
我可以通过多种方式解决浏览器问题,例如:
>添加< input type =“hidden”name =“MAX_FILE_SIZE”value =“5242880”/>在我的形式
>使用自定义413 nginx错误页面