服务器异常代码413问题

服务器异常代码413问题

近日与IOS,Android测试多文件上传时偶遇一个413请求实体过大异常。于是乎就开始解决这个问题.

  • 首先,项目采用的是Spring +Spring MVC+MyBatis为整体开发框架,所以最先想到的就是mvc-servlet.xml中是否做了设置。
<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="maxUploadSize" value="40960000" />
	</bean>

明明已经做了文件上传大小限制。怎么还会报请求实体内容过大呢。百思不得其解。就说前台是不是传的文件太大了。所以将maxUploadSize的值调到了100G,发现还是一样的问题。这就不好玩了。思前想后最后才想起来项目中采用了Nginx做负载均衡服务器,是不是Nginx中做了限制 。就上网去查询资料。从Nginx官网中可以看到这样 一句话Nginx_Http_core_module

> 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.

其大致意思是说,规定请求头的"Content-Length" 字段可以限制客户端允许请求实体的大小。如果实际请求实体大小超过所配置的值,将会返回 一个413错误给客户端(请求实体太大)。注意,浏览器不能正确的显示这个错误。设置值为0就意味着不检查客户端请求实体大小。

之后,官网又给出了配置Demo

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

配置语法就是client_max_body_size 允许上传文件的大小将其放在 http{}块中。保存之后重启nginx服务器即可解决此问题。

原文地址:https://www.cnblogs.com/tdg-yyx/p/6823082.html