feat: update modern nginx conf for backend and frontend
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
|
||||
# Setup this path to coconut-legacy frontend.
|
||||
# Setup this path to coconut-leaf legacy frontend.
|
||||
set $frontend_root /var/www/coconut-leaf/frontend-legacy;
|
||||
root $frontend_root;
|
||||
index home.html;
|
||||
|
||||
@@ -1,61 +1,64 @@
|
||||
|
||||
# Setup this path to coconut-leaf frontend.
|
||||
set $frontend_root /var/www/coconut-leaf/frontend/dist;
|
||||
|
||||
# ============================
|
||||
# 路由 1: /web -> 静态文件
|
||||
# Route 1: /web -> Vite-built SPA
|
||||
# ============================
|
||||
location /web {
|
||||
# 使用 alias 精确映射
|
||||
# 请求 /web/index.html -> /var/www/static/index.html
|
||||
alias /var/www/static;
|
||||
|
||||
# 静态文件优化
|
||||
alias $frontend_root;
|
||||
index index.html;
|
||||
|
||||
# Optimize for static file
|
||||
expires 7d;
|
||||
add_header Cache-Control "public, max-age=604800";
|
||||
|
||||
# 尝试返回文件,不存在则返回404(避免落入其他location)
|
||||
try_files $uri $uri/ =404;
|
||||
|
||||
# 可选:启用 gzip 压缩
|
||||
gzip_static on;
|
||||
|
||||
# SPA fallback: for non-file routes, serve index.html
|
||||
try_files $uri $uri/ /web/index.html;
|
||||
}
|
||||
|
||||
# ============================
|
||||
# 路由 2: /api -> Go 程序 (8848端口)
|
||||
# Route 3: /api -> reverse proxy to backend
|
||||
# ============================
|
||||
location /api {
|
||||
# 反向代理到本地 Go 服务
|
||||
# Rewrite to remove "api" prefix
|
||||
rewrite ^/api(/.*)$ $1 break;
|
||||
|
||||
# Reverse proxy to localhost backend
|
||||
proxy_pass http://127.0.0.1:8848;
|
||||
|
||||
# 重要:保留原始请求头
|
||||
|
||||
# Keep original request headers
|
||||
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;
|
||||
|
||||
# WebSocket 支持(如果 Go 程序需要)
|
||||
|
||||
# Optional: WebSocket support
|
||||
# proxy_http_version 1.1;
|
||||
# proxy_set_header Upgrade $http_upgrade;
|
||||
# proxy_set_header Connection "upgrade";
|
||||
|
||||
# 超时设置(根据业务调整)
|
||||
# Time configuration
|
||||
proxy_connect_timeout 60s;
|
||||
proxy_send_timeout 60s;
|
||||
proxy_read_timeout 60s;
|
||||
|
||||
# 缓冲设置(可选,大文件上传时注意调整)
|
||||
|
||||
# Buffer setup (changed when meeting large file uploading)
|
||||
proxy_buffering on;
|
||||
proxy_buffer_size 4k;
|
||||
proxy_buffers 8 4k;
|
||||
}
|
||||
|
||||
# ============================
|
||||
# 可选:根路径处理
|
||||
# Root path redirect: Root -> /web/home
|
||||
# ============================
|
||||
location = / {
|
||||
# 重定向到 /web
|
||||
return 302 /web/;
|
||||
return 302 /web/home;
|
||||
}
|
||||
|
||||
# 禁止访问隐藏文件
|
||||
# Deny hidden files at root level
|
||||
location ~ /\. {
|
||||
deny all;
|
||||
return 404;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user