場景:
為了給用戶較好的交互和感官,我們通常需要對錯誤頁面進行友好提示。
環境介紹:
LNMP(linux(centos7.4)Nginx Mysql5.6 php7.0)
實現:
這里,我直接對nginx的子配置文件進行了相應配置,給出代碼
server { listen 80; server_name www.xiaobudiu.top; charset utf-8; access_log /etc/nginx/logs/access/www.xiaobudiu.top.access.log main; error_log /etc/nginx/logs/error/www.xiaobudiu.top.error.log debug; root /data/www; index index.html index.htm index.php; location /favicon.ico { log_not_found off; access_log off; }location ~ \.php$ { fastcgi_pass unix:/dev/shm/php-cgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }error_page 404 403 500 502 503 504 /404.html; location = /404.html { root /data/errorPage; }location ~ /\.ht { deny all; } }
從上面可以看出,如果訪問我定義的server(www.xiaobudiu.top)出現404,403,500,502,503,504 錯誤時,直接nginx重寫到 location = /404.html ,在這個location中,我定義root,也就是我們自己定義的錯誤頁面所在的位置,這里是/data/errorPage,然后我們在這個路徑下vim 404.html就可以了 。
文件結構是這樣:
效果示例:
假設我在我的網站找一個不存在的頁面,就會直接返回我剛才自己定義的404.html,如圖。
注:當然,還有對nginx反向代理錯誤頁面的定義,以及nginx解析php出錯的錯誤頁面的定義,如果有這方面需求,可以參考這篇文章。https://www.cnblogs.com/paul8339/p/7389422.html