今天发现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