php 错误413

sbtkgmzw  于 2022-11-21  发布在  PHP
关注(0)|答案(4)|浏览(130)

当我尝试上传一个文件到我的网站时,我得到了Nginx“413 Request Entity Too Large”错误,但是在我的nginx.conf文件中我已经明确地声明了最大大小为250 MB,并且在php.ini中也修改了最大文件大小(是的,我重新启动了进程).错误日志显示:
2010年12月6日04:15:06 [错误] 20124#0:*11975客户端拟发送过大正文:1144149字节,客户端:60.228.229.238,服务器:www.x.com,请求:“POST /上传HTTP/1.1”,主机:“x.com”,引用地址:“http://x.com/“
据我所知,1144149字节并不是250 MB......我是不是漏掉了什么?
下面是基本的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/*;
}

以及站点的vhost:

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;
        }
    }
}
zzwlnbp8

zzwlnbp81#

不知道你的nginx版本和它是用什么模块构建的,这很坚韧,但是尝试以下操作:
1.复制您的客户端_最大_正文_大小300 M;行插入到vhost配置的location / { }部分。我不确定它是否正确覆盖了默认值(1 MB)。
1.您使用的是nginx_upload_module吗?如果是,请确保您的upload_max_file_size为300 MB;行中的数据。

pinkon5k

pinkon5k2#

我的设置是:
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规则.

7vhp5slm

7vhp5slm3#

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

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

作为级联级别中“较低”的一个,这将是一个很容易的方法来查看问题是否来自您的nginx配置或模块。
它肯定不是来自PHP,因为413错误“body too large”实际上是一个NGinx错误。

q3qa4bjr

q3qa4bjr4#

请尝试以下步骤解决该错误。

  • 在文本编辑器中打开Nginx配置文件(nginx.conf)。
$ sudo nano /etc/nginx/nginx.conf
  • http块下添加指令client_max_body_size
http {
   # Basic Settings
   client max body size 16M;
   ...
}
  • 在文本编辑器中打开nginx默认文件
$ sudo nano /etc/nginx/sites-enabled/default
  • location块下添加指令client_max_body_size
location / {
   ...
   client_max_body_size 100M;
}
  • 使用以下命令重新启动Nginx。
$ sudo systemctl restart nginx

**可选:**如果在后端服务器上运行耗时的进程,则必须调整服务器的超时属性以避免504超时错误。

  • 在文本编辑器中打开Nginx默认文件
$ sudo nano /etc/nginx/sites-enabled/default
  • location块下添加指令proxy_connect_timeoutproxy_send_timeoutproxy_read_timeout
location /api {
    client_max_body_size 100M;
    proxy_connect_timeout 6000;
    proxy_send_timeout 6000;
    proxy_read_timeout 6000;
    proxy_pass http://localhost:5001;
}
  • 使用以下命令重新启动Nginx。
$ sudo systemctl restart nginx

相关问题