1、前言
Nginx access log中出现大量的499状态码,在网上了解到出现499的原因大体都是说服务端处理时间过长,客户端主动关闭了连接。
2、处理方法
499错误是什么?让我们看看NGINX的源码中的定义:
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 */
可以看到,499对应的是 “client has closed connection”。这很有可能是因为服务器端处理的时间过长,客户端“不耐烦”了。
既然原因可能是服务端处理时间太长了,看一下upstream_response_time时间可以了解到后端程序处理了多久。
先了解一下什么是upstream_response_time和request_time分别是什么:
request_time:服务端从接受客户端请求的第一个字节到服务端应用程序处理完发送完响应数据的时间,包括请求数据时间、程序响应时间、输出响应时间;
upstream_response_time:指nginx向后端如php,tomcat等建立连接开始到到处理完数据关闭连接为止的时间;
上面说过,原因可能是服务端处理时间太长了, 那么应该upstream_resopnse_time和request_time时间很长才对。结果打脸了,upstream_response_time没有记录,request_time也非常短,也就是说nginx根本没有将请求转发到php处理,而是直接返回了499状态码,所以没有upstream_response_time,并且request_time时间很短,甚至为零。
这么说我这里出现499不是服务端处理时间太长了,而是另有他因。
3、结果
在google上搜索到一英文论坛上有关于此错误的解决方法:
客户端请求速度过快,触发了nginx保护机制,直接返回499状态码;
第二种情况就是客户端主动关闭了连接;
证书错误;
优化方法:
proxy_ignore_client_abort on; # 表示代理服务端不要主要主动关闭客户端连接。
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
proxy_buffer_size 32k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 512k;