使用wget命令行程序调试网页错误的方法

60 阅读2分钟

Digital creative of a browser on the internet

有时在管理一个网站时,事情会被搞得一团糟。你可能会删除一些陈旧的内容,用重定向到其他网页来取代它。后来,在做了其他改动后,你发现一些网页变得完全无法访问了。你可能会在浏览器中看到一个错误:"该页面没有正确重定向",并建议你检查你的cookies。

Redirect loop example in Firefox

图片由 屏幕截图:吉姆-霍尔,CC-BY SA 4.0

调试这种情况的一种方法是使用wget 命令行程序,使用-S 选项来显示所有的服务器响应。当使用wget 进行调试时,我还喜欢使用-O 选项将输出保存到某个临时文件中,以备以后需要查看其内容。

$ wget -O /tmp/test.html -S http://10.0.0.11/announce/--2021-08-24 17:09:49--  http://10.0.0.11/announce/
Connecting to 10.0.0.11:80... connected.
HTTP request sent, awaiting response... 
 
HTTP/1.1 302 Found
 
Date: Tue, 24 Aug 2021 22:09:49 GMT
 
Server: Apache/2.4.48 (Fedora)
 
X-Powered-By: PHP/7.4.21
 
Location: http://10.0.0.11/assets/
 
Content-Length: 0
 
Keep-Alive: timeout=5, max=100
 
Connection: Keep-Alive
 
Content-Type: text/html; charset=UTF-8
Location: http://10.0.0.11/assets/ [following]--2021-08-24 17:09:49--  http://10.0.0.11/assets/
Reusing existing connection to 10.0.0.11:80.
HTTP request sent, awaiting response... 
 
HTTP/1.1 302 Found
 
Date: Tue, 24 Aug 2021 22:09:49 GMT
 
Server: Apache/2.4.48 (Fedora)
 
X-Powered-By: PHP/7.4.21
 
Location: http://10.0.0.11/announce/
 
Content-Length: 0
 
Keep-Alive: timeout=5, max=99
 
Connection: Keep-Alive
 
Content-Type: text/html; charset=UTF-8
Location: http://10.0.0.11/announce/ [following]--2021-08-24 17:09:49--  http://10.0.0.11/announce/
Reusing existing connection to 10.0.0.11:80.
.
.
.20 redirections exceeded.

在这个输出中,我省略了很多重复的内容。通过阅读服务器的响应,你可以看到http ://10.0.0.11/announce/ ,它立即重定向到http ://10.0.0.11/assets/ ,然后又重定向到http ://10.0.0.11/announce/ 。以此类推。这是一个无休止的循环,wget 将在20次重定向后退出。但有了这些调试信息,你可以修复重定向并避免循环。