Nginx狀態代碼499,nginx狀態499

來源:互聯網
上載者:User

Nginx狀態代碼499,nginx狀態499
1、問題描述

140.207.202.187 - - [18/May/2016:10:30:58 +0800] "POST/v3/violations HTTP/1.1" 499 0 "-" "-"42.236.10.71 - - [18/May/2016:10:30:59 +0800] "POST /v3/violationsHTTP/1.1" 499 0 "-" "-"
2、問題分析google

499 / ClientClosed Request

    An Nginx HTTP server extension. This codeis introduced to log the case when the connection is closed by client whileHTTP server is processing its request, making server unable to send the HTTP header back

維基百科

499Client Closed Request (Nginx)

Used in Nginx logs to indicate when the connection has been closed by client while the server is still processing itsrequest, making server unable to send a status code back

nginx源碼
./src/http/ngx_http_core_module.c:        if (status == NGX_ERROR || status == 499) {./src/http/ngx_http_request.h:#define NGX_HTTP_CLIENT_CLOSED_REQUEST     499./src/http/ngx_http_special_response.c:    ngx_null_string,                     /* 499, client has closed connection */

這是nginx定義的一個狀態代碼,用於表示這樣的錯誤:伺服器返回http頭之前,用戶端就提前關閉了http串連,這很有可能是因為伺服器端處理的時間過長,用戶端“不耐煩”了。

進一步尋找

尋找“NGX_HTTP_CLIENT_CLOSED_REQUEST”

./src/http/ngx_http_upstream.c:                                               NGX_HTTP_CLIENT_CLOSED_REQUEST);./src/http/ngx_http_upstream.c:                                               NGX_HTTP_CLIENT_CLOSED_REQUEST);./src/http/ngx_http_upstream.c:                                               NGX_HTTP_CLIENT_CLOSED_REQUEST);./src/http/ngx_http_upstream.c:                                           NGX_HTTP_CLIENT_CLOSED_REQUEST);./src/http/ngx_http_upstream.c:                                           NGX_HTTP_CLIENT_CLOSED_REQUEST);./src/http/ngx_http_upstream.c:                                           NGX_HTTP_CLIENT_CLOSED_REQUEST);./src/http/ngx_http_request.c:        || rc == NGX_HTTP_CLIENT_CLOSED_REQUEST./src/http/ngx_http_spdy.c:                                              NGX_HTTP_CLIENT_CLOSED_REQUEST);./src/http/ngx_http_spdy.c:        ngx_http_spdy_finalize_connection(sc, NGX_HTTP_CLIENT_CLOSED_REQUEST);./src/http/ngx_http_spdy.c:        ngx_http_spdy_finalize_connection(sc, NGX_HTTP_CLIENT_CLOSED_REQUEST);./src/http/ngx_http_spdy.c:    ngx_http_spdy_finalize_connection(sc, NGX_HTTP_CLIENT_CLOSED_REQUEST);./src/http/modules/tfs/ngx_http_tfs_server_handler.c:                     return NGX_HTTP_CLIENT_CLOSED_REQUEST;./src/http/modules/tfs/ngx_http_tfs.c:    if (rc == NGX_HTTP_CLIENT_CLOSED_REQUEST./src/http/modules/tfs/ngx_http_tfs.c:                                          NGX_HTTP_CLIENT_CLOSED_REQUEST);./src/http/modules/lua/ngx_http_lua_socket_tcp.c:                        if (rc == NGX_HTTP_CLIENT_CLOSED_REQUEST) {./src/http/modules/lua/ngx_http_lua_control.c:        && rc != NGX_HTTP_CLIENT_CLOSED_REQUEST)./src/http/modules/lua/ngx_http_lua_control.c:        && rc != NGX_HTTP_CLIENT_CLOSED_REQUEST./src/http/modules/lua/ngx_http_lua_subrequest.c:            && rc != NGX_HTTP_CLIENT_CLOSED_REQUEST))./src/http/modules/lua/ngx_http_lua_util.c:        return NGX_HTTP_CLIENT_CLOSED_REQUEST;./src/http/modules/lua/ngx_http_lua_util.c:        return NGX_HTTP_CLIENT_CLOSED_REQUEST;./src/http/modules/lua/ngx_http_lua_util.c:    return NGX_HTTP_CLIENT_CLOSED_REQUEST;./src/http/ngx_http_spdy_v3.c:                                              NGX_HTTP_CLIENT_CLOSED_REQUEST);./src/http/ngx_http_spdy_v3.c:        ngx_http_spdy_finalize_connection(sc, NGX_HTTP_CLIENT_CLOSED_REQUEST);./src/http/ngx_http_spdy_v3.c:        ngx_http_spdy_finalize_connection(sc, NGX_HTTP_CLIENT_CLOSED_REQUEST);./src/http/ngx_http_spdy_v3.c:    ngx_http_spdy_finalize_connection(sc, NGX_HTTP_CLIENT_CLOSED_REQUEST);./src/http/ngx_http_request.h:#define NGX_HTTP_CLIENT_CLOSED_REQUEST     499

第一處顯示

#if (NGX_HAVE_KQUEUE)    if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {        if (!ev->pending_eof) {            return;        }            ev->eof = 1;         c->error = 1;         if (ev->kq_errno) {            ev->error = 1;         }            if (!u->cacheable && u->peer.connection) {            ngx_log_error(NGX_LOG_INFO, ev->log, ev->kq_errno,                          "kevent() reported that client prematurely closed "                          "connection, so upstream connection is closed too");            ngx_http_upstream_finalize_request(r, u,                                               NGX_HTTP_CLIENT_CLOSED_REQUEST);            return;        }            ngx_log_error(NGX_LOG_INFO, ev->log, ev->kq_errno,                      "kevent() reported that client prematurely closed "                      "connection");        if (u->peer.connection == NULL) {            ngx_http_upstream_finalize_request(r, u,                                               NGX_HTTP_CLIENT_CLOSED_REQUEST);        }            return;    }    

KQUEUE 這個是在unix下使用的網路模型,這裡可以略過

第二處顯示

static voidngx_http_upstream_check_broken_connection(ngx_http_request_t *r,    ngx_event_t *ev){    int                  n;    char                 buf[1];    ngx_err_t            err;    ngx_int_t            event;    ngx_connection_t     *c;    ngx_http_upstream_t  *u;    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, ev->log, 0,                   "http upstream check client, write event:%d, \"%V\"",                   ev->write, &r->uri);    c = r->connection;    u = r->upstream;    if (c->error) {        if ((ngx_event_flags & NGX_USE_LEVEL_EVENT) && ev->active) {            event = ev->write ? NGX_WRITE_EVENT : NGX_READ_EVENT;            if (ngx_del_event(ev, event, 0) != NGX_OK) {                ngx_http_upstream_finalize_request(r, u,                                               NGX_HTTP_INTERNAL_SERVER_ERROR);                return;            }        }        if (!u->cacheable) {            ngx_http_upstream_finalize_request(r, u,                                               NGX_HTTP_CLIENT_CLOSED_REQUEST);        }        return;    }

第三處顯示

voidngx_http_finalize_request(ngx_http_request_t *r, ngx_int_t rc){    ngx_connection_t          *c;    ngx_http_request_t        *pr;    ngx_http_core_loc_conf_t  *clcf;    c = r->connection;    ngx_log_debug5(NGX_LOG_DEBUG_HTTP, c->log, 0,                   "http finalize request: %d, \"%V?%V\" a:%d, c:%d",                   rc, &r->uri, &r->args, r == c->data, r->main->count);    if (rc == NGX_DONE) {        ngx_http_finalize_connection(r);        return;    }    if (rc == NGX_OK && r->filter_finalize) {        c->error = 1;    }    if (rc == NGX_DECLINED) {        r->content_handler = NULL;        r->write_event_handler = ngx_http_core_run_phases;        ngx_http_core_run_phases(r);        return;    }    if (r != r->main && r->post_subrequest) {        rc = r->post_subrequest->handler(r, r->post_subrequest->data, rc);    }    if (rc == NGX_ERROR        || rc == NGX_HTTP_REQUEST_TIME_OUT        || rc == NGX_HTTP_CLIENT_CLOSED_REQUEST        || c->error)    {        if (ngx_http_post_action(r) == NGX_OK) {            return;        }        if (r->main->blocked) {            r->write_event_handler = ngx_http_request_finalizer;        }        ngx_http_terminate_request(r, rc);        return;    }

在收到讀寫事件處理之前時串連不可用

1、在一個upstream出錯,執行next_upstream時也會判斷串連是否可用,不可用則返回499

2、server處理請求未結束,而client提前關閉了串連

 

3、故障排除

nginx配置中加上

proxy_ignore_client_abort  on;  #表示代理服務端不要主要主動關閉用戶端串連。 

預設 proxy_ignore_client_abort 是關閉的,

關閉時:如果用戶端端主動關閉請求或者用戶端網路斷掉, Nginx 會記錄 499,同時 request_time 是「後端已經處理」的時間,而upstream_response_time 為“-“ (已驗證)。

開啟時:如果用戶端端主動關閉請求或者用戶端網路斷掉,Nginx 會等待後端處理完(或者逾時),然後記錄「後端的返回資訊」到日誌。所以,如果後端返回 200,就記錄 200 ;如果後端放回 5XX ,那麼就記錄 5XX 。

如果逾時(預設60s,可以用 proxy_read_timeout 設定),Nginx 會主動中斷連線,記錄 504

註:只在做反向 Proxy的時候加入,作為其他伺服器的時候,關閉為好,預設設定是關閉的!

  

參考文章

http://blog.hexu.org/archives/1085.shtml

http://stackoverflow.com/questions/12973304/nginx-499-error-codes

https://httpstatuses.com/499

http://wmcxy.iteye.com/blog/2026835

http://ju.outofmemory.cn/entry/228858

http://www.90arther.net/jekyll/update/2015/11/19/repeat-request.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.