1. 自定義錯誤頁面
-
error_page code ... [=[response]] uri;
定義錯誤頁,以指定的響應狀態碼進行響應,此指令由ngx_http_index_module模塊提供
支持環境:http, server, location, if in location
#1.修改nginx的配置文件如下,定義錯誤頁,把500 502 503 504 404這些狀態碼轉換為200狀態碼,並定義錯誤頁面。 [root@nginx01 ~]# vim /etc/nginx/conf.d/virtualhost.conf server { listen 80; server_name www.nginx01.com; location / { root /data/nginx/html/web1; index index.html; } error_page 500 502 503 504 404 =200 /errorpage.html; location = /errorpage.html { root /data/nginx/html/web1/errorpage/; } } #2.重啟nginx服務 [root@nginx01 web1]# systemctl reload nginx.service #3.在web1下新建目錄errorpage和文件errorpage/errorpage.html [root@nginx01 web1]# ll errorpage/ total 4 -rw-r--r-- 1 root root 31 Jun 15 23:30 errorpage.html [root@nginx01 web1]# cat errorpage/errorpage.html our system is being maintained #4.使用客戶端訪問一個不存在的頁面,能正常返回自定義的錯誤頁面,狀態碼變為200 [root@xuzhichao ~]# curl http://www.nginx01.com/abc our system is being maintained [root@xuzhichao ~]# curl -I http://www.nginx01.com/abc HTTP/1.1 200 OK Server: nginx/1.20.1 Date: Tue, 15 Jun 2021 15:36:27 GMT Content-Type: text/html Content-Length: 31 Last-Modified: Tue, 15 Jun 2021 15:30:38 GMT Connection: keep-alive ETag: "60c8c79e-1f" Accept-Ranges: bytes