在Debian系統中,要設置Nginx的錯誤頁面,請按照以下步驟操作:
sudo apt update
sudo apt install nginx
custom_error.html
。使用文本編輯器打開它,并添加你想要顯示的自定義錯誤信息。sudo nano /var/www/html/custom_error.html
custom_error.html
文件中添加以下內容,根據需要修改錯誤代碼和消息:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Error</title>
</head>
<body>
<h1>Oops! Something went wrong.</h1>
<p>We're sorry, but the page you are looking for could not be found.</p>
</body>
</html>
保存并關閉文件。
接下來,需要配置Nginx以使用自定義錯誤頁面。打開Nginx的默認站點配置文件:
sudo nano /etc/nginx/sites-available/default
server
塊中找到error_page
指令。如果沒有這個指令,可以在http
塊或server
塊中添加它。將以下代碼添加到適當的位置,根據需要修改錯誤代碼和自定義錯誤頁面的路徑:error_page 404 /custom_error.html;
location = /custom_error.html {
root /var/www/html;
}
這里,我們將404錯誤代碼映射到自定義錯誤頁面custom_error.html
。你可以根據需要更改錯誤代碼和文件路徑。
保存并關閉文件。
最后,重新加載Nginx以應用更改:
sudo nginx -t
sudo systemctl reload nginx
現在,當用戶遇到指定的錯誤代碼時,Nginx將顯示你設置的自定義錯誤頁面。