在 CentOS 系统中,要自定义 Nginx 的错误页面,你需要按照以下步骤操作:
/usr/share/nginx/html
目录下创建一个名为 custom_error_pages
的目录:sudo mkdir /usr/share/nginx/html/custom_error_pages
404.html
)上传到该目录中。你可以使用 scp
或其他文件传输工具将文件从本地计算机传输到服务器。确保文件具有正确的权限和所有权:sudo cp /path/to/your/local/404.html /usr/share/nginx/html/custom_error_pages/
sudo chown nginx:nginx /usr/share/nginx/html/custom_error_pages/404.html
/etc/nginx/nginx.conf
或 /etc/nginx/conf.d/default.conf
:sudo vi /etc/nginx/nginx.conf
或者
sudo vi /etc/nginx/conf.d/default.conf
server
块中,添加以下配置以指定自定义错误页面:error_page 404 /custom_error_pages/404.html;
location = /custom_error_pages/404.html {
root /usr/share/nginx/html;
}
这将把 404 错误重定向到你创建的自定义错误页面。你可以根据需要更改错误代码和文件路径。
保存并关闭配置文件。
检查 Nginx 配置文件的语法是否正确:
sudo nginx -t
如果一切正常,你应该会看到以下输出:
nginx: configuration file /etc/nginx/nginx.conf test is successful
sudo systemctl reload nginx
现在,当用户访问不存在的页面时,Nginx 将显示你的自定义错误页面。你可以为其他错误代码(如 500、403 等)创建类似的配置。