【问题标题】:Nginx Error 413Nginx 错误 413
【发布时间】:2011-05-20 22:21:35
【问题描述】:

当我尝试将文件上传到我的站点时,我收到 Nginx “413 Request Entity Too Large”错误,但是在我的 nginx.conf 文件中,我已经明确指出最大大小约为 250MB此刻,并更改了 php.ini 中的最大文件大小(是的,我重新启动了进程)。错误日志给了我这个:

2010/12/06 04:15:06 [错误] 20124#0: *11975 客户端打算发送太大的正文:1144149 字节,客户端: 60.228.229.238,服务器:www.x.com,请求:“POST /上传 HTTP/1.1”,主机: “x.com”,推荐人: “http://x.com/”

据我所知,1144149 字节不是 250MB... 我这里有什么遗漏吗?

这是基本的 Nginx 配置:

user  nginx;
worker_processes  8;
worker_rlimit_nofile 100000;

error_log   /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
    use epoll;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    client_max_body_size 300M;
    tcp_nopush      on;
    tcp_nodelay     on;
    server_tokens   off;
    gzip            on;
    gzip_static     on;
    gzip_comp_level 5;
    gzip_min_length 1024;
    keepalive_timeout  300;
    limit_zone   myzone  $binary_remote_addr  10m;

    # Load config files from the /etc/nginx/conf.d directory
    include /etc/nginx/sites/*;
}

以及网站的虚拟主机:

server {
    listen      80;
    server_name www.x.com x.com;

    access_log  /var/log/nginx/x.com-access.log;

    location / {
        index   index.html index.htm index.php;
        root    /var/www/x.com;

        if (!-e $request_filename) {
            rewrite ^/([a-z,0-9]+)$ /$1.php last;
            rewrite ^/file/(.*)$ /file.php?file=$1;
        }

        location ~ /engine/.*\.php$ {
            return 404;
        }

        location ~ ^/([a-z,0-9]+)\.php$ {
            fastcgi_pass    127.0.0.1:9000;
            fastcgi_index   index.php;
            fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include     fastcgi_params;
        }
    }
}

【问题讨论】:

  • 您可能应该引用您已更改的 nginx 配置设置

标签: php file-upload nginx


【解决方案1】:

不知道您的 nginx 构建版本以及构建它的模块使这变得困难,但请尝试以下操作:

  1. 复制你的client_max_body_size 300M;进入 vhost 配置的 location / { } 部分。我不确定它是否正确地覆盖了默认值(即 1 MB)。

  2. 你在使用nginx_upload_module吗?如果是这样,请确保您的 upload_max_file_size 为 300MB;在你的配置中也行。

【讨论】:

    【解决方案2】:

    我的设置是:

    php.ini

    ...
    upload_max_filesize = 8M
    ...
    

    nginx.conf

    ...
    client_max_body_size 8m;
    ...
    

    nginx上传的时候报413错误

    然后我有一个想法:我不会让nginx显示错误413,client_max_body_size设置为大于upload_max_filesize的值,因此:

    php.ini

    ...
    upload_max_filesize = 8M
    ...
    

    nginx.conf

    ...
    client_max_body_size 80m;
    ...
    

    发生了什么?

    当你上传小于80MB时,nginx不会显示错误413,但是如果文件达到8MB,PHP会显示错误。

    这解决了我的问题,但是如果有人上传大于 80MB 的文件,就会发生错误 413,nginx 规则。

    【讨论】:

    • 我添加了client_max_body_size 64m;进入 conf 的 http { } 部分,重新​​启动它就可以了!谢谢!
    • 这为我解决了。我认为我的节点服务器上的正文解析器是问题,但完全忘记在 nginx 中设置这一行
    【解决方案3】:

    我还补充一点,您可以在 *.php 位置处理程序中定义它

    location ~ ^/([a-z,0-9]+)\.php$ {
    

    作为级联级别中的“较低”级别,这将是一种简单的方法来查看问题是否来自您的 nginx 配置或模块。

    它肯定不是来自 PHP,因为 413 错误“body too large”实际上是一个 NGinx 错误。

    【讨论】:

    • 不知道为什么我在这个答案上被否决了?需要解释一下吗?
    猜你喜欢
    • 1970-01-01
    • 2016-02-01
    • 2020-02-04
    • 2023-02-06
    • 2018-09-29
    • 1970-01-01
    • 2013-12-30
    • 1970-01-01
    • 2015-03-17
    相关资源
    最近更新 更多