zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Nginx 413 Request Entity Too Large错误解决

错误Nginx 解决 request too Entity large 413
2023-09-14 09:01:46 时间

Request Body Size Limit (官方文章)


When a client is uploading data, such as form fields, static files, or API payloads through an HTTP connection, all of this data is sent in the request body part of the HTTP request. The length of the request body should be defined in a Content-Length header.

当客户端通过HTTP连接上传诸如表单字段,静态文件或API之类的数据时,所有这些数据都在HTTP请求的请求体部分中发送。请求主体的长度应在Content-Length标头中定义

When the request body is larger than the allowed value, NGINX Unit responds with HTTP code 413 (Payload Too Large).

当请求主体大于允许值时,NGINX单元以HTTP代码响应413 (Payload Too Large)

This configuration parameter has the same meaning as the NGINX directive client_max_body_size. When you limit the request body size in NGINX Unit, make sure that your NGINX reverse proxy and load balancers are configured appropriately.

此配置参数与NGINX指令具有相同的含义client_max_body_size。当您限制NGINX单元中的请求包体大小时,请确保正确配置了NGINX反向代理和负载均衡。

Syntax:client_max_body_size size;
Default:
client_max_body_size 1m;
Context:httpserverlocation

Sets the maximum allowed size of the client request body, specified in the “Content-Length” request header field. If the size in a request exceeds the configured value, the 413 (Request Entity Too Large) error is returned to the client. Please be aware that browsers cannot correctly display this error. 

设置客户端请求正文的最大允许大小,在“ Content-Length”请求标头字段中指定。如果请求中的大小超过配置的值,则会向客户端返回413(请求实体太大)错误。请注意,浏览器无法正确显示此错误。

HTTP 状态码


413:用户上传的文件过大,需要调高 client_max_body_size 参数设置

Nginx出现的413 Request Entity Too Large错误,这个错误一般在上传文件的时候出现,打开nginx主配置文件nginx conf,找到http{}段,解决方法就是打开nginx主配置文件nginx.conf添加:client_max_body_size nM;

在文件上传时,如果上传文件比较大会出现报错:Status Code: 413 Request Entity Too Large,此时的响应头信息详细示例如下:

Request Method: POST
Status Code: 413 Request Entity Too Large
Remote Address: 10.147.26.139:80
Referrer Policy: no-referrer-when-downgrade

    这正是超过了nginx配置的上传文件大小限制导致的。配置项即是client_max_body_size size,其默认大小为1M,详细文档说明:client_max_body_size Sets the maximum allowed size of the client request body, specified in the Content-Length request header field. If the size in a request exceeds the configured value, the 413 (Request Entity Too Large) error is returned to the client. Please be aware that browsers cannot correctly display this error. 在视频上传网站,此项配置我们可以设置为1G以上,按需配置:client_max_body_size 2048M;

日志:包括nginx proxy以及nginx web上都有413错误记录: 

2020/05/09 15:28:06 [error] 2705932#0: *3448 client intended to send too large body: 302491285 bytes, POST /uploadfile?fileName
2020/05/09 15:28:06 [error] 8#8: *4949 client intended to send too large body: 302491285 bytes, client: 15.17.2.19

    最后,虽然nginx上的配置修改了,但是nginx后端的服务也需要进行相应修改,比如后端php的话就需要修改post_max_size,memory_limit,upload_max_filesize三项配置值:建议memory_limit的值比上传文件的大小值大点,因为memory_limit除了上传的文件外还有其它程序等所占用空间,不然实际可上传的文件大小肯定不到2048M。

post_max_size = 2048M
memory_limit = 2248M
upload_max_filesize = 2048M