今天發現nginx有不少的499錯誤,大約占了將近0.5%,而且是在新上線了一個含upstream的業務之后。
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中賦值。
upstream在以下幾種情況下會返回499:
(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。
(3)在一個upstream出錯,執行next_upstream時也會判斷連接是否可用,不可用則返回499。
總之,這個錯誤的比例升高可能表明服務器upstream處理過慢,導致用戶提前關閉連接。而正常情況下有一個小比例是正常的。