问题说明:413 Request Entity Too Large
状态码: 413 Request Entity Too Large
出现这个问题,就如上面的报错信息一样:请求实体太大超过限制!
一般多发生在文件上传中!
如果发生在文件上传中,要解决这样的问题,就要扩大上传文件的限制!
解决问题
服务方面
<!--SrpingMVC-->
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver">
<property name="maxUploadSize" value="157286400" /><!-- 上传最大文件限制 -->
<property name="maxInMemorySize" value="4096" /><!-- 读取文件到内存中的最大字节数,默认1024字节 -->
<property name="defaultEncoding" value="utf-8" />
</bean>
#SpringBoot 1.x
spring:
http:
multipart:
#enabled:true #是否自用http上传处理
tax-request-size:1000MB #最大请求文仲的大小
max-file-size:1000MB #设置单个文件最大长皮
# SpringBoot 2.X
spring:
servlet:
multipart:
#enabled:true #是否自用http上传处理
tax-request-size:1000MB #最大请求文仲的大小
max-file-size:1000MB #设置单个文件最大长皮
将上传服务的文件大小显示进行修改即可
服务器方面
#例如:Nginx
http {
#在http项中增加
client_max_body_size 100m;
}
<!--tomcat
从 apache-tomcat-7.0.63 开始, 如果将值设置为 0,表示 POST 最大值为 0,不限制 POST 大小需要将值设置为
-1。在此版本之前设置为 0 表示不限制 POST 大小。此处单位为b,例 20M 应写 maxPostSize=“20971520”
-->
<Connector port="8082" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8743" maxPostSize="-1" />
附录:Nginx其他参数
[root@dev-XXXXX ~]# cat /Data/app/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
charset utf-8;
#######
## http setting
#######
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 100; #这个参数表示http连接超时时间,默认是65s。要是上传文件比较大,在规定时间内没有上传完成,就会自动断开连接!所以适当调大这个时间。
fastcgi_connect_timeout 6000;
fastcgi_send_timeout 6000;
fastcgi_read_timeout 6000;
fastcgi_buffer_size 256k;
fastcgi_buffers 8 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
##
client_header_timeout 120s; #调大点
client_body_timeout 120s; #调大点
client_max_body_size 100m; #主要是这个参数,限制了上传文件大大小
client_body_buffer_size 256k;
## support more than 15 test environments
server_names_hash_max_size 512;
server_names_hash_bucket_size 128;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 9;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php;
gzip_vary on;
server {
listen 80;
server_name admin.wangshibo.com;
#if ($http_x_forwarded_for !~ ^(14.165.97.54|123.110.186.128|123.110.186.68)) {
# rewrite ^.*$ /maintence.php last;
#}
access_log /var/log/wangshibo.log main;
location / {
proxy_pass http://127.0.0.1:8484/;
proxy_connect_timeout 300; #这三个超时时间适量调大点
proxy_send_timeout 600;
proxy_read_timeout 600;
proxy_set_header X-Real-IP $remote_addr; # 获取客户端真实IP
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 获取代理者的真实ip
proxy_set_header X-Forwarded-Scheme $scheme; # 解决getScheme,isSecure,sendRedirect
proxy_buffer_size 32k;
proxy_buffers 32 256k;
proxy_busy_buffers_size 512k;
proxy_temp_file_write_size 512k;
}
location /static/video {
root /Data/app/tomcat-7-admin-wls/static/video;
}
}
}