首页
> 计算机技术
> Web服务器
> Nginx
配置nginx服务器的默认错误提示页
原创 lihf8515于2025年01月14日 14:58发表
来源:本站 阅读:168
nginx服务器的默认错误提示页面很简陋,显示出来不够友好,有必要重新配置nginx服务器的默认错误提示页面。主要有以下几个步骤:
打开 /etc/nginx/sites-enabled/vhost.conf 文件,这个文件在安装好nginx后是没有的。需要手动创建,方便配置多个网站。
找到server段,添加以下配置代码:
# 错误处理
fastcgi_intercept_errors on; # 捕获来自 FastCGI 应用程序的错误
proxy_intercept_errors on; # 开启 反向代理 错误页使用自定义的页面
error_page 500 502 503 504 /error/50x.html;
location /error/50x.html {
root /error;
internal;
}
error_page 404 /error/404.html;
location /error/404.html {
root /error;
internal;
}
这里,/error/50x.html和/error/404.html是您自己网站根目录中的自己创建的提示页面,其中需要的style.css代码如下:
body{
margin: 0;
}
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.btn {
border-width: 1px;
border-style: solid;
border-color: #cccccc;
border-radius: 5px;
cursor: pointer;
padding: 0.5em 1.5em;
display: inline-block;
font-weight: bold;
background-color: #f44336;
color: white;
}
错误提示页面代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=Edeg,chrome=1">
<link href="/error/style.css" rel="Stylesheet" />
<title>404 - Page Not Found</title>
</head>
<body>
<div class="container">
<div class="center">
<h3>404 - 哎呀!看来我们的小精灵迷路了,让我们一起帮他回家吧~</h3>
</div>
<div class="center">
<a href="/" class="btn">返回首页</a>
</div>
</div>
</body>
</html>
下一篇:nginx服务器的伪静态配置方法
阅读排行榜