從命令行使用 wget 調試網頁錯誤
調試 Web 服務器的一種方法是使用 wget 命令行程序。
有時在管理一個網站時,事情會被搞得一團糟。你可能會刪除一些陳舊的內容,用重定向到其他頁面來代替。后來,在做了其他改動后,你發現一些網頁變得完全無法訪問了。你可能會在瀏覽器中看到一個錯誤:“該頁面沒有正確重定向”,并建議你檢查你的 cookie。
Redirect loop example in Firefox
調試這種情況的一個方法是使用 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 FoundDate: Tue, 24 Aug 2021 22:09:49 GMTServer: Apache/2.4.48 (Fedora)X-Powered-By: PHP/7.4.21Location: http://10.0.0.11/assets/Content-Length: 0Keep-Alive: timeout=5, max=100Connection: Keep-AliveContent-Type: text/html; charset=UTF-8Location: 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 FoundDate: Tue, 24 Aug 2021 22:09:49 GMTServer: Apache/2.4.48 (Fedora)X-Powered-By: PHP/7.4.21Location: http://10.0.0.11/announce/Content-Length: 0Keep-Alive: timeout=5, max=99Connection: Keep-AliveContent-Type: text/html; charset=UTF-8Location: 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 次重定向后退出。但有了這些調試信息,你可以修復重定向,避免循環。


























