解决 Failed to load resource: the server responded with a status of 413 (Request Entity Too Large)
今天做一个上传功能,却提示 "Failed to load resource: the server responded with a status of 413 (Request Entity Too Large)"
查了一下原因,居然是 Nginx 报出的错误,问题已解决,录之:
解决方法:
在nginx 配置文件,nginx.conf 里设置, client_max_body_size 为 6M 即可, 上传大一些的图了,
# set client body size to 6M client_max_body_size 6M;
注: 改完之后,记得重启一下 nginx 哦 或 nginx -s reload
参考文档:
---------------------------------------
Nginx 413 Request Entity Too Large
How do I fix this problem and allow image upload upto 2MB in size using nginx web-server working in reverse proxy or stand-alone mode on Unix like operating systems?
You need to configure both nginx and php to allow upload size.
Nginx configuration
The client_max_body_size directive assigns the maximum accepted body size of client request, indicated by the line Content-Length in the header of request. If size is greater the given one, then the client gets the error “Request Entity Too Large” (413). To fix this issue edit your nginx.conf. Open the Terminal or login to the remote server using ssh client. Type the following command to edit your nginx.conf using a text editor such as vi or joe:
# vi /etc/nginx/nginx.conf
OR
# vi /usr/local/nginx/conf/nginx.conf
Add the following line to http or server or location context to increase the size limit in nginx.conf, enter:
# set client body size to 2M #client_max_body_size 2M; |
Save and close the file. Reload the nginx webserver, enter:
# /usr/local/nginx/sbin/nginx -s reload
OR
# /sbin/nginx -s reload
OR use the following on RHEL/CentOS/Debian/Ubuntu Linux:
# service nginx reload
PHP configuration (optional)
Your php installation also put limits on upload file size. Edit php.ini and set the following directives
;This sets the maximum amount of memory in bytes that a script is allowed to allocatememory_limit = 32M ;The maximum size of an uploaded file.upload_max_filesize = 2M ;Sets max size of post data allowed. This setting also affects file upload. To upload large files, this value must be larger than upload_max_filesizepost_max_size = 3M |
Save and close the file. Make sure you reload/restart back-end apache or nginx web server as per your setup. See “PHP Increase Upload File Size Limit” tutorial for more information.