錯誤內容
我們可以在error.log 里面可以看到
錯誤內容:upstream timed out (110: Connection timed out) while reading response header from upstream
錯誤原因
從錯誤日志我們可以知道,該錯誤是由於nginx 代理去獲取上游服務器的 返回值超時了。那么這個問題是什么導致的:
- 該請求獲取的數據比較多,后端處理該請求花費的時間較長。
- 也可能是代理服務器與上游服務器的網絡問題
我們通過定位出錯的url,來排查問題,最終確定問題是由於 該請求需要后端處理的時間比較長。
那么解決辦法可以是開發人員對該接口進行優化,也可以是我們通過nginx將超時時間設置長些。
錯誤解決辦法
nginx 超時時間設置
官網鏈接:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_read_timeout
Syntax: | proxy_read_timeout time; |
---|---|
Default: | proxy_read_timeout 60s; |
Context: | http,server,location |
Defines a timeout for reading a response from the proxied server. The timeout is set only between two successive read operations, not for the transmission of the whole response. If the proxied server does not transmit anything within this time, the connection is closed.
proxy_read_timeout 參數, 該指令是指從上游服務器兩次成功的讀操作耗時的超時時間,也就意味着從上游服務器成功讀操作后,過了60S,沒有再從上游服務器成功讀操作的話,就會關閉該連接。
默認值是 60s ,我們可以設置為240s,或者300s。來應對上游服務器處理請求慢的問題。
在nginx 的配置文件 在 http,server,location 三個位置任意一個位置
加上
proxy_read_timeout 240s;