今天發現nginx有很多的499錯誤,大約佔了將近0.5%,並且是在新上線了一個含upstream的業務以後。nginx
grep一下nginx源碼,定義在ngx_request_t.h緩存
/* * HTTP does not define the code for the case when a client closed * the connection while we are processing its request so we introduce * own code to log such situation when a client has closed the connection * before we even try to send the HTTP header to it */ #define NGX_HTTP_CLIENT_CLOSED_REQUEST 499
這下就很清楚了,這是nginx定義的一個狀態碼,用於表示這樣的錯誤:服務器返回http頭以前,客戶端就提早關閉了http鏈接。服務器
再grep下「NGX_HTTP_CLIENT_CLOSED_REQUEST」,發現目前這個狀態值只在ngx_upstream中賦值。code
upstream在如下幾種狀況下會返回499:server
(1)upstream 在收到讀寫事件處理以前時,會檢查鏈接是否可用:ngx_http_upstream_check_broken_connection,事件
if (c->error) { //connecttion錯誤 …… if (!u->cacheable) { //upstream的cacheable爲false,這個值跟http_cache模塊的設置有關。指示內容是否緩存。 ngx_http_upstream_finalize_request(r, u, NGX_HTTP_CLIENT_CLOSED_REQUEST); } }
如上代碼,當鏈接錯誤時會返回499。源碼
(2)server處理請求未結束,而client提早關閉了鏈接,此時也會返回499。it
(3)在一個upstream出錯,執行next_upstream時也會判斷鏈接是否可用,不可用則返回499。io
總之,這個錯誤的比例升高可能代表服務器upstream處理過慢,致使用戶提早關閉鏈接。而正常狀況下有一個小比例是正常的。stream