聊聊nginx報錯499問題

codecraft發表於2017-11-04

本文主要來聊一下nginx的access log當中出現的499問題。

問題描述

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.

原因

伺服器返回http頭之前,客戶端就提前關閉了http連線,常見於後臺介面處理時間比較長,而前端請求又自帶有超時時間。

復現

請求例項

<!DOCTYPE html>
<html>
<head>
<script src="http://www.w3school.com.cn/jquery/jquery-1.11.1.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
      $.ajax({
    url : `/demo/test`,
    timeout : 10000,
    type : `get`,
    dataType : `json`,
    success : function(data){
        alert(`success`);
    }
   });
  });
});
</script>
</head>
<body>

<button>ajax帶超時時間請求</button>

</body>
</html>

後臺介面

    @GetMapping("/test")
    public String test(HttpServletResponse response) throws InterruptedException {
        Thread.sleep(100*1000);
        return "hello";
    }

nginx

        location /demo/ {
            access_log  /usr/local/var/log/nginx/host.access.log  main;
            proxy_pass http://localhost:8080/demo/ ;
        }

關於log format如下

log_format  main  `$remote_addr - $remote_user [$time_local] "$request" `
                      `$status $body_bytes_sent "$http_referer" `
                      `"$http_user_agent" "$http_x_forwarded_for"`;

log例項

127.0.0.1 - - [04/Nov/2017:01:11:29 +0800] "GET /demo/test HTTP/1.1" 499 0 "http://localhost:8888/demo.html" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36" "-"
127.0.0.1 - - [04/Nov/2017:01:11:42 +0800] "GET /demo/test HTTP/1.1" 499 0 "http://localhost:8888/demo.html" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36" "-"
127.0.0.1 - - [04/Nov/2017:01:11:58 +0800] "GET /demo/test HTTP/1.1" 499 0 "http://localhost:8888/demo.html" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36" "-"

doc

相關文章