nginx报错(413 Request Entity Too Large)解决办法

nginx报错(413 Request Entity Too Large)解决办法
在做网站内容编辑时,附带了一个压缩ZIP文件,大小在4M左右,保存的时候服务端报错:413 Request Entity Too Large

分析
虽然后台程序添加了上传文件大小限制,但是这个提示明显不是后台报的,而且文件的大小还没有超过后台设置的大小,从页面的提示信息看,是Nginx报的错误,很有可能是nginx自身限制了上传文件的大小。况且我后台使用了Nginx做反向代理,那就更有可能是nginx的问题了。

解决方法
只需要修改nginx的配置文件nginx.conf,在http{}段中添加或修改client_max_body_size值为更大值即可。如:

http {
    include       mime.types;
    default_type  application/octet-stream;
    //添加或修改下面一行即可
    client_max_body_size       20m;

    ......
    sendfile        on;
     ......
     }

然后检查配置是否正确并重启nginx

[root@abc ~]# /usr/local/nginx/sbin/nginx -t

[root@abc ~]# /usr/local/nginx/sbin/nginx -s reload

参考资料:
以下是nginx官方文档对client_max_body_size的说明:

Syntax:    client_max_body_size size;
Default:    
client_max_body_size 1m;
Context:    http, server, location

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. Setting size to 0 disables checking of client request body size.

意思就是说nginx默认request body为1M,超过这个范围就会报错413 Request Entity Too Large。

the end