天天看點

Nginx傳回400 Bad Request故障排查

系統環境

服務端:K8s叢集使用Nginx Ingress Controller,并且支援http2

用戶端:golang version 1.9.2,且使用http2

故障現象

Nginx偶發400 Bad Request,絕大部分請求正常傳回200

故障排除步驟

(1)故障複現

使用golang http2 client,每個請求post相同的資料,直到response 400;查詢nginx日志,response為400時,對應的request_length明顯小

(2)開啟nginx debug

本文使用的是Nginx Ingress Controller,開啟debug的方式如圖所示,修改deployment,增加參數“--v=5”,不過要注意,修改deployment後容器會重新開機,本文複現故障是在測試環境中。

Nginx傳回400 Bad Request故障排查

根據response為400的時間,發現這樣的info級别日志:client prematurely closed stream: only 0 out of 1125 bytes of request body received

即nginx收到的http header content-length是1125位元組,但是http body裡卻是0位元組

(3)檢視用戶端問題

打開用戶端debug,env GODEBUG=http2debug=2 go test --timeout 30m -v test.go >> test.log 2>&1 

debug日志中正常request資訊如下圖,而異常的request沒有紅線辨別那行資訊,即data并沒有發出

Nginx傳回400 Bad Request故障排查

(4)問題解決

按照https://github.com/golang/go/issues/25009的讨論,問題在于GOROOT目錄下src/net/http/transport.go;修改之後GOROOT重新build,問題解決

繼續閱讀