從命令行使用 wget 調(diào)試網(wǎng)頁(yè)錯(cuò)誤
調(diào)試 Web 服務(wù)器的一種方法是使用 wget 命令行程序。
有時(shí)在管理一個(gè)網(wǎng)站時(shí),事情會(huì)被搞得一團(tuán)糟。你可能會(huì)刪除一些陳舊的內(nèi)容,用重定向到其他頁(yè)面來(lái)代替。后來(lái),在做了其他改動(dòng)后,你發(fā)現(xiàn)一些網(wǎng)頁(yè)變得完全無(wú)法訪問(wèn)了。你可能會(huì)在瀏覽器中看到一個(gè)錯(cuò)誤:“該頁(yè)面沒(méi)有正確重定向”,并建議你檢查你的 cookie。
Redirect loop example in Firefox
調(diào)試這種情況的一個(gè)方法是使用 wget
命令行程序,使用 -S
選項(xiàng)來(lái)顯示所有的服務(wù)器響應(yīng)。當(dāng)使用 wget
進(jìn)行調(diào)試時(shí),我也喜歡使用 -O
選項(xiàng)將輸出保存到一些臨時(shí)文件中,以備以后需要查看其內(nèi)容。
$ 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.
我在這個(gè)輸出中省略了很多重復(fù)的內(nèi)容。通過(guò)閱讀服務(wù)器的響應(yīng),你可以看到 http://10.0.0.11/announce/
立即重定向到 http://10.0.0.11/assets/
,然后又重定向到 http://10.0.0.11/announce/
。以此類推。這是一個(gè)無(wú)休止的循環(huán),wget
將在 20 次重定向后退出。但有了這些調(diào)試信息,你可以修復(fù)重定向,避免循環(huán)。