What is a 404 page
如果碰巧網(wǎng)站出了問題,或者用戶試圖訪問一個(gè)并不存在的頁面時(shí),此時(shí)服務(wù)器會(huì)返回代碼為404的錯(cuò)誤信息,此時(shí)對(duì)應(yīng)頁面就是404頁面。404頁面的默認(rèn)內(nèi)容和具體的服務(wù)器有關(guān)。如果后臺(tái)用的是NGINX服務(wù)器,那么404頁面的內(nèi)容則為:404 Not Found
Why you need to customize the 404 page
在訪問時(shí)遇到上面這樣的404錯(cuò)誤頁面,我想99%(未經(jīng)調(diào)查,估計(jì)數(shù)據(jù))的用戶會(huì)把頁面關(guān)掉,用戶就這樣悄悄的流失了。如果此時(shí)能有一個(gè)漂亮的頁面能夠引導(dǎo)用戶去他想去的地方必然可以留住用戶。因此,每一個(gè)網(wǎng)站都應(yīng)該自定義自己的404頁面。
How to customize the 404 page under NGINX
1.創(chuàng)建自己的404.html頁面
2.更改nginx.conf在http定義區(qū)域加入: fastcgi_intercept_errors on;
3.更改nginx.conf(或單獨(dú)網(wǎng)站配置文件,例如在nginx -> sites-enabled下的站點(diǎn)配置文件 )中在server 區(qū)域加入: error_page 404 /404.html 或者 error_page 404 =http://www.xxx.com/404.html
4.更改后重啟nginx,,測(cè)試nginx.conf正確性: /opt/nginx/sbin/nginx –t
#502 等錯(cuò)誤可以用同樣的方法來配置:error_page 500 502 503 504 /50x.html;
Precautions:
1. Must add: fastcgi_intercept_errors on
如果這個(gè)選項(xiàng)沒有設(shè)置,即使創(chuàng)建了404.html和配置了error_page也沒有效果。
fastcgi_intercept_errors 語法: fastcgi_intercept_errors on|off 默認(rèn): fastcgi_intercept_errors off
添加位置: http, server, location 默認(rèn)情況下,nginx不支持自定義404錯(cuò)誤頁面,只有這個(gè)指令被設(shè)置為on,nginx才支持將404錯(cuò)誤重定向。
這里需要注意的是,并不是說設(shè)置了fastcgi_intercept_errors on,nginx就會(huì)將404錯(cuò)誤重定向。在nginx中404錯(cuò)誤重定向生效的前提是設(shè)置了fastcgi_intercept_errors on,并且正確的設(shè)置了error_page這個(gè)選項(xiàng)(包括語法和對(duì)應(yīng)的404頁面)
2. The custom 404 page must be larger than 512 bytes, otherwise the IE default 404 page may appear. For example, assume that 404.html is customized and the size is only 11 bytes (content: 404 error)