Nginx-HTTP状态码:499

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

定义在ngx_request_t.

/*
* 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连接

………………………………..
ngx_string(ngx_http_error_495_page), /* 495, https certificate error */
ngx_string(ngx_http_error_496_page), /* 496, https no certificate */
ngx_string(ngx_http_error_497_page), /* 497, http to https */
ngx_string(ngx_http_error_404_page), /* 498, canceled */
ngx_null_string,                     /* 499, client has closed connection */
………………………………………………………….

 

upstream在以下几种情况下会返回499:

从上面的解释可以看出,499代码很有可能是因为服务器端处理请求过长,客户端不能承受就断开了连接。
要解决此问题,就需要在程序上面做些优化了。
查看下“NGX_HTTP_CLIENT_CLOSED_REQUEST”,发现这个状态值只在ngx_upstream中赋值

(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处理过慢,导致用户提前关闭连接。而正常情况下有一个小比例是正常的。

 

问题的核心就是要排查为什么服务端处理时间过长
查看监控: 机器负载情况,网络情况。
解决问题:
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

 

原创文章,作者:赛福,如若转载,请注明出处:https://www.safecdn.cn/300.html

本站不销售、不代购、不提供任何支持,仅分享网络信息,请自行辨别,请遵纪守法、文明上网。