微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 使用nginx防止大文件上传

我使用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内部使整个上传继续,直到它完全传输…

一个编辑:
这是我的顶级视图(监视NginxPHP)正在进行文件上传(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)文件时,NginxPHP-fastcgi都没有真正超载.

我可以通过多种方式解决浏览器问题,例如:

>添加< input type =“hidden”name =“MAX_FILE_SIZE”value =“5242880”/>在我的形式
>使用自定义413 Nginx错误页面

原文地址:https://www.jb51.cc/nginx/435604.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐