404是一個相應代碼,表示"頁面無法找到"(Page Not Found),Google關于"軟404"給出的說法是:
Instead of returning a 404 response code for a non-existent URL, websites that serve "soft 404s" return a 200 response code.
就是說對于那些不存在的URL,服務器并沒有返回404(Page Not Found)代碼,而是返回了200(OK)代碼,而這是不正常的。
之后在其它的搜索結果里我又看到了這樣一段話
Soft 404s can occur as a result of configuration errors when an Error Document 404 is specified as an absolute path rather than a relative path.
看完之后恍然大悟,因為我的404自定義頁面是有圖片和CSS的,而圖片跟CSS都是以相對路徑(eg. /xxx/xxx)寫在頁面里的,所以為了能讓整站都能看到404頁面里的圖片,我就把404頁面在Nginx里定義成了絕對路徑(eg. http://www.slyar.com/xxx/xxx),由于頁面被當成了外部頁面,所以會返回200代碼,由此產生了"軟404"錯誤。
知道了錯誤,那就好辦了。將404頁面的路徑定義為相對路徑,至于圖片和CSS,只要在頁面里使用絕對路徑即可。
Nginx下正確的404頁面定義方法:
1、VIM編輯Nginx配置文件,用了vhosts的就單獨改,沒用的直接改nginx.conf
vim /usr/local/nginx/conf/nginx.conf
or
vim /usr/local/nginx/conf/vhosts/slyar.com.nginx.conf
2、以相對路徑指定404頁面
server {#error_page 404 http://www.slyar.com/404.htmlerror_page 404 /404.html;}
3、:wq保存退出,重新加載Nginx
/usr/local/nginx/sbin/nginx -s reload
4、重新檢查一下不存在的頁面,看是否返回404
curl -I http://www.slyar.com/slyar
HTTP/1.1 404 Not Found
Server: nginx/1.0.15
Date: Mon, 27 Aug 2012 08:13:56 GMT
Content-Type: text/html
Content-Length: 2110
Connection: keep-alive