我正在尝试在 docker 容器中进行健康检查。我发现这个命令:
\n\nwget --quiet --tries=1 --spider http://localhost:6077 || exit 1\n
Run Code Online (Sandbox Code Playgroud)\n\n问题是,当容器运行时,如果我在不带 --spider 的情况下运行 wget,我会得到 HTTP 200 代码,但如果使用 --spider,则会返回 404。
\n\n为什么会发生这种情况?
\n\n$ wget --tries=1 http://localhost:6077\n--2019-04-22 04:20:12-- http://localhost:6077/\nResolving localhost (localhost)... 127.0.0.1, ::1\nConnecting to localhost (localhost)|127.0.0.1|:6077... connected.\nHTTP request sent, awaiting response... 200 OK\nLength: 436 [application/xml]\nSaving to: \xe2\x80\x98index.html.1\xe2\x80\x99\n\n\n$ wget --tries=1 --spider http://localhost:6077\nSpider mode enabled. Check if remote file exists.\n--2019-04-22 04:21:46-- http://localhost:6077/\nResolving localhost (localhost)... 127.0.0.1, ::1\nConnecting to localhost (localhost)|127.0.0.1|:6077... connected.\nHTTP request sent, awaiting response... 404 Not Found\nRemote file does not exist -- broken link!!!\n
Run Code Online (Sandbox Code Playgroud)\n\n这种奇怪的行为正在破坏我的健康检查,如果我不使用 --spider 我认为 wget 会尝试在某个地方下载index.html,对吗?
\n接受的答案似乎是不正确的,实际上可以帮助您隐藏 Docker 容器中的错误。向 Wget添加该--spider
选项将导致 Wget 发送请求HEAD
而不是GET
. 特别是在这种特殊情况下,您不使用调用 Wget --recursive
。
根据 RFC 7231 第 4.3.2 节,HEAD
请求与请求相同GET
,只是它不包含消息正文。但是,在您的情况下,服务器似乎对 aHEAD
和 aGET
请求返回不同的响应。我将其称为您服务器中的错误。请不要简单地在没有蜘蛛的情况下调用 Wget 并将问题隐藏起来。此行为违反了 HTTP 规范,并且将来可能会导致其他问题,因为连接到它的客户端会看到错误的响应。