更新 Nginx 配置,使用自定义 404 页面并优化未命中路径处理逻辑

This commit is contained in:
2026-06-09 13:20:30 +08:00
parent 28d81d86bd
commit ffaf04982a
2 changed files with 223 additions and 7 deletions

View File

@@ -1,4 +1,4 @@
# Nginx 静态文件服务 + 前端入口兜底
# Nginx 静态文件服务 + 自定义 404 页面
# HTTP 重定向到 HTTPS
server {
@@ -19,8 +19,12 @@ server {
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
# 域名下任意未命中路径都不要落到 nginx 默认 404
error_page 404 = /web/index.html;
# 使用项目自定义静态 404 页面,保持 nginx 返回 404 状态码
error_page 404 /404.html;
location = /404.html {
internal;
}
# 根路径默认进入网页端
location = / {
@@ -39,17 +43,17 @@ server {
# 网页端public/web/index.html -> dist/web/index.html
location /web/ {
alias /usr/share/nginx/html/web/;
try_files $uri $uri/ /web/index.html;
try_files $uri $uri/ =404;
}
# 后台管理端dist/index.html前缀 /sys/
location /sys/ {
alias /usr/share/nginx/html/;
try_files $uri $uri/ /sys/index.html;
try_files $uri $uri/ =404;
}
# 其他任意路径统一兜底到网页端入口,避免出现 nginx 404 页面
# 其他静态资源或任意路径,未命中时统一交给 nginx 404再由自定义 404 页面展示
location / {
try_files $uri $uri/ /web/index.html;
try_files $uri $uri/ =404;
}
}

212
public/404.html Normal file
View File

@@ -0,0 +1,212 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>404 - 页面不存在</title>
<link rel="icon" href="/favicon.ico" />
<style>
:root {
color-scheme: light;
--bg-start: #f5f7fa;
--bg-end: #e8ecf3;
--card-bg: rgba(255, 255, 255, 0.92);
--text-primary: #1f2d3d;
--text-secondary: #6b7280;
--accent: #409eff;
--accent-hover: #66b1ff;
--border: rgba(64, 158, 255, 0.12);
--shadow: 0 20px 50px rgba(31, 45, 61, 0.12);
}
* {
box-sizing: border-box;
}
body {
margin: 0;
min-height: 100vh;
font-family: Inter, -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
background: linear-gradient(135deg, var(--bg-start), var(--bg-end));
color: var(--text-primary);
}
.error-page {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 32px;
}
.error-card {
width: min(960px, 100%);
background: var(--card-bg);
border: 1px solid var(--border);
border-radius: 24px;
box-shadow: var(--shadow);
backdrop-filter: blur(10px);
display: flex;
gap: 48px;
align-items: center;
padding: 56px;
}
.error-content {
flex: 1;
}
.error-code {
margin: 0;
font-size: clamp(64px, 12vw, 110px);
line-height: 1;
color: var(--accent);
letter-spacing: 4px;
}
.error-title {
margin: 20px 0 12px;
font-size: 30px;
line-height: 1.3;
}
.error-message {
margin: 0;
color: var(--text-secondary);
font-size: 16px;
line-height: 1.8;
}
.error-actions {
margin-top: 32px;
display: flex;
flex-wrap: wrap;
gap: 14px;
}
.action-button {
appearance: none;
border: none;
border-radius: 999px;
padding: 12px 22px;
font-size: 15px;
cursor: pointer;
text-decoration: none;
transition: transform 0.2s ease, box-shadow 0.2s ease, background-color 0.2s ease;
display: inline-flex;
align-items: center;
justify-content: center;
}
.action-button:hover {
transform: translateY(-1px);
}
.action-button.primary {
background: var(--accent);
color: #fff;
box-shadow: 0 12px 24px rgba(64, 158, 255, 0.24);
}
.action-button.primary:hover {
background: var(--accent-hover);
}
.action-button.secondary {
background: #fff;
color: var(--text-primary);
border: 1px solid rgba(31, 45, 61, 0.12);
}
.error-illustration {
flex: 0 0 320px;
display: flex;
align-items: center;
justify-content: center;
}
.illustration-shell {
width: 100%;
aspect-ratio: 1 / 1;
border-radius: 32px;
background: linear-gradient(160deg, rgba(64, 158, 255, 0.12), rgba(64, 158, 255, 0.02));
border: 1px solid rgba(64, 158, 255, 0.14);
position: relative;
overflow: hidden;
}
.illustration-shell::before,
.illustration-shell::after {
content: '';
position: absolute;
border-radius: 50%;
background: rgba(64, 158, 255, 0.12);
}
.illustration-shell::before {
width: 180px;
height: 180px;
top: -30px;
right: -30px;
}
.illustration-shell::after {
width: 120px;
height: 120px;
left: -20px;
bottom: -20px;
}
.illustration-center {
position: absolute;
inset: 0;
display: flex;
align-items: center;
justify-content: center;
font-size: clamp(72px, 10vw, 120px);
font-weight: 800;
color: rgba(64, 158, 255, 0.85);
letter-spacing: 6px;
}
@media (max-width: 900px) {
.error-card {
flex-direction: column;
padding: 36px 24px;
gap: 28px;
}
.error-illustration {
flex-basis: auto;
width: min(320px, 100%);
}
.error-title {
font-size: 24px;
}
}
</style>
</head>
<body>
<main class="error-page">
<section class="error-card">
<div class="error-content">
<h1 class="error-code">404</h1>
<h2 class="error-title">抱歉,你访问的页面不存在</h2>
<p class="error-message">
当前地址可能已失效、输入有误,或者对应内容已被移动。你可以返回首页,或回到后台入口继续操作。
</p>
<div class="error-actions">
<a class="action-button primary" href="/">返回首页</a>
<a class="action-button secondary" href="/sys/">进入后台</a>
</div>
</div>
<div class="error-illustration" aria-hidden="true">
<div class="illustration-shell">
<div class="illustration-center">404</div>
</div>
</div>
</section>
</main>
</body>
</html>