refactor/nginx: 修改 Nginx 配置并添加错误页面
- 新增 50x.html 错误页面文件
- 更新 nginx.conf 配置:
- 修改服务器名称为 team12.10813352.xyz
- 优化前端静态文件配置,添加缓存和 Gzip 压缩策略
- 更新后端 API 代理地址
- 添加长连接支持和超时设置
- 配置错误页面
- 增加安全头设置
Change-Id: Ib765f7ed370de721bfab23e441e51ea77fa6740f
diff --git a/50x.html b/50x.html
new file mode 100644
index 0000000..c009f2c
--- /dev/null
+++ b/50x.html
@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>服务器错误</title>
+ <meta charset="utf-8">
+ <style>
+ body {
+ width: 35em;
+ margin: 0 auto;
+ font-family: Tahoma, Verdana, Arial, sans-serif;
+ }
+ </style>
+</head>
+<body>
+ <h1>六百六十六,服务器内部错误</h1>
+ <p>抱歉,服务器遇到了内部错误,无法完成您的请求。</p>
+ <p>请稍后再试。</p>
+</body>
+</html>
\ No newline at end of file
diff --git a/nginx.conf b/nginx.conf
index 78e9b9e..ab42dba 100644
--- a/nginx.conf
+++ b/nginx.conf
@@ -1,29 +1,52 @@
server {
listen 80;
- server_name localhost;
-
+ server_name team12.10813352.xyz;
+
+ # 前端静态文件配置
root /usr/share/nginx/html;
index index.html;
-
- # 开启 gzip 压缩
- gzip on;
- gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
-
- # 所有静态资源
+
+ # 解决 React Router 单页应用路由问题
location / {
try_files $uri $uri/ /index.html;
+
+ # 缓存策略
+ expires 7d; # 静态资源缓存7天
+ add_header Cache-Control "public";
+
+ # Gzip 压缩
+ gzip on;
+ gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
+ gzip_min_length 1024;
+ gzip_comp_level 6;
}
-
- # 后端 API 代理 - 修改为宿主机地址
+
+ # API 请求代理到后端
location /api/ {
- proxy_pass http://host.docker.internal:8080/api/;
- proxy_http_version 1.1;
- proxy_set_header Upgrade $http_upgrade;
- proxy_set_header Connection 'upgrade';
+ proxy_pass http://202.205.102.121:5001/; # 后端服务地址
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
- proxy_cache_bypass $http_upgrade;
+
+ # 长连接支持
+ proxy_http_version 1.1;
+ proxy_set_header Connection "";
+
+ # 超时设置
+ proxy_connect_timeout 60s;
+ proxy_send_timeout 60s;
+ proxy_read_timeout 60s;
}
-}
\ No newline at end of file
+
+ # 错误页面配置
+ error_page 404 /index.html;
+ error_page 500 502 503 504 /50x.html;
+
+ # 安全头设置
+ add_header X-Frame-Options "SAMEORIGIN" always;
+ add_header X-XSS-Protection "1; mode=block" always;
+ add_header X-Content-Type-Options "nosniff" always;
+ add_header Referrer-Policy "no-referrer-when-downgrade" always;
+ add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:" always;
+}
\ No newline at end of file