最近在查看nginx日志的的时候,发现了一些状态码为499的记录。所以Google查了下大致内容如下:
499 CLIENT CLOSED REQUEST A non-standard status code introduced by nginx for the case when a client closes the connection while nginx is processing the request. 链接:https://httpstatuses.com/499
大概意思就是nginx还在处理请求的时候客户端已经关闭了连接。
下面来简单模拟下这种情况
- 新建test.php放在web根目录下,代码如下
<?php sleep(20);//sleep20秒 echo 'ok';
- 进行curl请求,超时时间和数据传输时间都设置为10秒
curl --connect-timeout 10 -m 10 http://localhost/test.php
- nginx日志如下
127.0.0.1 - - [24/Jun/2017:23:11:23 +0800] "GET /test.php HTTP/1.1" 499 0 - "-" "curl/7.51.0" "-"
以上可以看见http code 为499,并且客户端已经连接关闭了,没有接收到任何数据。
如果curl不设置超时时间进行请求的话就正常了。
curl http://localhost/test.php 会返回ok 同时nginx日志为 127.0.0.1 - - [24/Jun/2017:23:13:01 +0800] "GET /test.php HTTP/1.1" 200 12 - "-" "curl/7.51.0" "-"
解决方案
- 客户端超时时间设置大一点,不要等服务端还没处理完就关闭连接。
- 服务端代码及逻辑优化,减少执行时间。如果是比较耗时的操作业务容许的情况下可以改为异步去处理(具体如何调整还需看业务流程及具体场景)