服務器異常代碼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服務器即可解決此問題。
