Nginx 上傳大文件超時解決辦法
情況如下:用nginx作代理服務(wù)器,上傳大文件時(本人測試上傳50m的文件),提示上傳超時或文件過大春宣。
原因是nginx對上傳文件大小有限制痢毒,而且默認(rèn)是1M。另外赶撰,若上傳文件很大,還要適當(dāng)調(diào)整上傳超時時間。
解決方法是在nginx的配置文件下豪娜,加上以下配置:
?
1
2
3
4
5
6
client_max_body_size? ? 50m; //文件大小限制餐胀,默認(rèn)1m
client_header_timeout? ? 1m;
client_body_timeout? ? ? 1m;
proxy_connect_timeout? ? 60s;
proxy_read_timeout? ? ? 1m;
proxy_send_timeout? ? ? 1m;
每個參數(shù)的意思:
client_max_body_size
限制請求體的大小,若超過所設(shè)定的大小瘤载,返回413錯誤否灾。
client_header_timeout
讀取請求頭的超時時間,若超過所設(shè)定的大小鸣奔,返回408錯誤墨技。
client_body_timeout
讀取請求實體的超時時間,若超過所設(shè)定的大小挎狸,返回413錯誤扣汪。
proxy_connect_timeout
http請求無法立即被容器(tomcat, netty等)處理,被放在nginx的待處理池中等待被處理锨匆。此參數(shù)為等待的最長時間崭别,默認(rèn)為60秒,官方推薦最長不要超過75秒统刮。
proxy_read_timeout
http請求被容器(tomcat, netty等)處理后紊遵,nginx會等待處理結(jié)果,也就是容器返回的response侥蒙。此參數(shù)即為服務(wù)器響應(yīng)時間暗膜,默認(rèn)60秒。
proxy_send_timeout
http請求被服務(wù)器處理完后鞭衩,把數(shù)據(jù)傳返回給Nginx的用時学搜,默認(rèn)60秒。
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
nginx.conf
在nginx使用過程中论衍,上傳文件的過程中瑞佩,通常需要設(shè)置nginx報文大小限制。避免出現(xiàn)413 Request Entity Too Large坯台。
于是奇葩的問題被我們遇到了炬丸,詳細(xì)配置請參考下面。我們的問題是蜒蕾,無論client_max_body_size設(shè)置在哪里稠炬,nginx -s reload后,依然一直報413.多次嘗試reload咪啡,始終無效首启。最終決定kill 進(jìn)程,restart撤摸,終于好了毅桃。
由此可見褒纲,nginx reload并不一定好使。有時候钥飞,為了保險起見莺掠。restart比較靠譜。不知道別人有沒有遇到同樣的問題代承。希望對大家有幫助汁蝶!~~
設(shè)置如下:
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.
可以選擇在http{ }中設(shè)置:client_max_body_size? 20m;
也可以選擇在server{ }中設(shè)置:client_max_body_size? 20m;
還可以選擇在location{ }中設(shè)置:client_max_body_size? 20m;
三者到區(qū)別是:http{} 中控制著所有nginx收到的請求渐扮。而報文大小限制設(shè)置在server{}中论悴,則控制該server收到的請求報文大小,同理墓律,如果配置在location中膀估,則報文大小限制,只對匹配了location 路由規(guī)則的請求生效耻讽。
? ? http{
#控制全局nginx所有請求報文大小
#client_max_body_size? 20m;
? ? ? ? ? ? ? ? server{
#控制該server的所有請求報文大小
#client_max_body_size? 20m;
? ? ? ? ? ? ? ? ? ? ? ? location a {
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? location b{
#控制滿足該路由規(guī)則的請求報文大小
#client_max_body_size? 20m;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? server {
? ? ? ? ? ? ? ? }
? ? }
記錄一個知識點:重啟nginx命令察纯,到/usr/sbin目錄下執(zhí)行命令:./nginx -s reload
若重啟失敗參考:https://blog.csdn.net/qq_16014497/article/details/80650905(先執(zhí)行sudo nginx -c /etc/nginx/nginx.conf,后執(zhí)行nginx -s reload即可)
————————————————
版權(quán)聲明:本文為CSDN博主「Java--成長之路」的原創(chuàng)文章针肥,遵循CC 4.0 BY-SA版權(quán)協(xié)議饼记,轉(zhuǎn)載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/weixin_47385625/article/details/114268931