官网示例 check interval=3000 rise=2 fall=5 timeout=1000 type=http; check_keepalive_requests 100; check_http_send "HEAD / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n"; check_http_expect_alive http_2xx http_3xx;
始终返回错误信息,后来通过Wireshark抓包发现tomcat返回400 Bad Request信息。 通过分析http的请求头发现没有host头信息。
在http1.1协议定义上找到如下说明 https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.23 A client MUST include a Host header field in all HTTP/1.1 request messages . If the requested URI does not include an Internet host name for the service being requested, then the Host header field MUST be given with an empty value. An HTTP/1.1 proxy MUST ensure that any request message it forwards does contain an appropriate Host header field that identifies the service being requested by the proxy. All Internet-based HTTP/1.1 servers MUST respond with a 400 (Bad Request) status code to any HTTP/1.1 request message which lacks a Host header field.
http1.1请求头当中必须包含host头(值可以为空),否则服务器要返回400错误。
我修改后的配置 check interval=3000 rise=2 fall=5 timeout=1000 type=http; check_keepalive_requests 100; check_http_send "GET / HTTP/1.1\r\nHost:\r\nProxy-Connection: keep-alive\r\n\r\n"; check_http_expect_alive http_2xx http_3xx;
其中加了host头,并且http将Connection头改为Proxy-Connection 再次通过抓包测试,返回正确200,并且nginx与tomcat之间使用长连接进行健康检查