歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> 學習Linux >> Nginx狀態碼499,nginx狀態499

Nginx狀態碼499,nginx狀態499

日期:2017/3/6 9:21:17   编辑:學習Linux

Nginx狀態碼499,nginx狀態499


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 void
ngx_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;
    }

第三處顯示

void
ngx_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

注:只在做反向代理的時候加入,作為其他服務器的時候,關閉為好,默認設置是關閉的!

  

參考文章

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

http://xxxxxx/Linuxjc/1153856.html TechArticle

Copyright © Linux教程網 All Rights Reserved