我正在尝试测试服务器以查看其行为是否符合以下定义,为此,我需要构建无效响应并将其从上游服务器发送到网关服务器。 HTTP 规范说明了以下关于状态代码 502 - Bad Gateway 的内容。
The 502 (Bad Gateway) status code indicates that the server, while acting as a gateway or proxy, received an invalid response from an inbound server it accessed while attempting to fulfill the request.
但究竟什么构成无效响应?非标准响应代码(例如,一些随机数,如 6789)是否算作无效响应?还是带有无效 HTTP 版本的响应?
遇到这个:502 HTTP Status Code ,它表示不完整的 header 和响应主体可能会导致 502 错误。服务器如何确定 header 或正文是否不完整?更重要的是,是否可以通过编程方式创建这样的响应(我使用的是 Java)?
最佳答案
我设法在 HTTP 规范(RFC7230 第 3.3.3 节)中找到以下内容。
If a message is received without Transfer-Encoding and with either multiple Content-Length header fields having differing field-values or a single Content-Length header field having an invalid value, then the message framing is invalid and the recipient MUST treat it as an unrecoverable error. If this is a request message, the server MUST respond with a 400 (Bad Request) status code and then close the connection. If this is a response message received by a proxy, the proxy MUST close the connection to the server, discard the received response, and send a 502 (Bad Gateway) response to the client. If this is a response message received by a user agent, the user agent MUST close the connection to the server and discard the received response.
除此之外,我还注意到当上游服务器关闭时,Nginx 在充当反向代理/负载平衡器时返回 502。
关于http - 在 HTTP 502 中,无效响应是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35660464/