服务器排障 之 nginx 499 错误的解决

问题描述:python

Nginx 服务器大量499报错mysql

220.181.165.136 - - [18/May/2015:10:31:02 +0800] "POST /v1/jobsHTTP/1.1" 499 0 "" "bdHttpRequest/1.0.0"
 
115.239.212.7 - - [18/May/2015:10:31:03 +0800] "GET /v1/job/643309e3-dc73-4025-aa69-c9405c1d818fHTTP/1.1" 499 0"http://www.baidu.com/?tn=91638679_hao_pg&s_j=1""Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko"
 
140.207.202.187 - - [18/May/2015:10:30:58 +0800] "POST/v3/violations HTTP/1.1" 499 0 "-" "-"
 
42.236.10.71 - - [18/May/2015:10:30:59 +0800] "POST /v3/violationsHTTP/1.1" 499 0 "-" "-"
 
106.120.173.17 - - [18/May/2015:10:30:58 +0800] "POST/v3/violations HTTP/1.1" 499 0 "-" "Mozilla/5.0 (Windows NT6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131Safari/537.36"
 
180.97.35.164 - - [18/May/2015:10:30:52 +0800] "GET/v1/job/f86bdecc-2a61-4a42-bb7b-aa794b77f89b HTTP/1.1" 499 0"http://www.baidu.com/s?word=%E5%8D%81%E5%A0%B0%E5%A4%A9%E6%B0%94&tn=sitehao123&ie=utf-8""Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"

问题分析:nginx

1  499出现的缘由sql

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 backapi

维基百科定义:缓存

499Client Closed Request (Nginx)bash

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源码:

grep一下nginx源码,定义在ngx_request_t.h :

/*
* HTTP does notdefine the code for the case when a client closed
* the connectionwhile we are processing its request so we introduce
* own code to logsuch situation when a client has closed the connection
* before we even tryto send the HTTP header to it
*/
#define NGX_HTTP_CLIENT_CLOSED_REQUEST 499

这是nginx定义的一个状态码,用于表示这样的错误:服务器返回http头以前,客户端就提早关闭了http链接

继续grep :

wKioL1ZK_oWDZn9NAAC_HrhuQAs422.png


颇有多是由于服务器端处理的时间过长,客户端不耐烦

要解决此问题,就须要在程序上面作些优化了。

 

再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处理过慢,致使用户提早关闭链接。而正常状况下有一个小比例是正常的。

继续分析:

问题的核心就是要排查为何服务端处理时间过长

可能问题

1      后台python程序处理请求时间过长

2      mysql慢查询

经过查看监控:

1  cpu和内存的使用,都在正常范围

2  后台程序访问正常

3  MySQL没有慢查询


结果:

通过询问老大后得知,这个nginx为查询违章的api,用户提交查询后, python就去数据库或者交通局的网站查询。这个查询会有消耗必定的时间,因此,用户会主动断开链接

解决问题:

proxy_ignore_client_abort  on;  #让代理服务端不要主动关闭客户端的链接。

 

默认 proxy_ignore_client_abort 是关闭的,此时在请求过程当中若是客户端端主动关闭请求或者客户端网络断掉,那么 Nginx 会记录 499,同时 request_time 「后端已经处理」的时间,而upstream_response_time - (已验证)

 

若是使用了 proxy_ignore_client_abort on ;

那么客户端主动断掉链接以后,Nginx 会等待后端处理完(或者超时),而后记录「后端的返回信息」到日志。因此,若是后端返回 200就记录 200 ;若是后端放回 5XX ,那么就记录 5XX

若是超时(默认60s,能够用 proxy_read_timeout 设置)Nginx 会主动断开链接,记录 504

注:只在作反向代理的时候加入,做为其余服务器的时候,关闭为好,默认设置是关闭的!

相关文章
相关标签/搜索