jingrow/Caddyfile

85 lines
2.5 KiB
Caddyfile
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Caddy 配置文件 - 前端静态文件 + 后端 API 反向代理
# 最小化配置,符合 Caddy 2.x 最佳实践
#
# 地址配置说明:
# - 前端地址从环境变量 FRONTEND_ADDRESS 读取,可以是:
# * 域名(如 example.com自动启用 HTTPSCaddy 会从 Let's Encrypt 获取 SSL 证书
# * 端口(如 :8080使用 HTTP
# - 如果未设置 FRONTEND_ADDRESS默认使用 :8080
# - 后端端口从环境变量 BACKEND_PORT 读取,默认 9001使用 {$BACKEND_PORT:9001} 语法)
# - 可在 .env 文件中设置:
# FRONTEND_ADDRESS=example.com # 使用域名(推荐生产环境,自动 SSL
# 或
# FRONTEND_ADDRESS=:8080 # 使用端口(开发环境)
# BACKEND_PORT=9001
# - 注意:使用域名时,确保 DNS 已解析Caddy 会自动获取 SSL 证书
#
# 使用说明:
# 1. 生产环境:设置 FRONTEND_ADDRESS=your-domain.comCaddy 会自动获取 SSL 证书
# 2. 开发环境:设置 FRONTEND_ADDRESS=:8080 或使用默认值
# 3. 确保域名 DNS 已解析到服务器 IP使用域名时
# 4. 确保防火墙开放相应端口80、443 用于域名,或自定义端口)
# 5. 可选:取消注释 email 行,用于证书到期通知
{
# 自动 HTTPS 配置Let's Encrypt
# email your-email@example.com # 可选:用于证书到期通知
# acme_ca https://acme-v02.api.letsencrypt.org/directory # 默认使用 Let's Encrypt
log {
output stdout
format console
}
}
{$FRONTEND_ADDRESS::8080} {
# 静态资源缓存
@static {
file
path *.js *.css *.png *.jpg *.jpeg *.gif *.svg *.ico *.woff *.woff2 *.ttf *.eot
}
header @static Cache-Control "public, max-age=31536000, immutable"
# HTML 不缓存
@html {
file
path *.html
}
header @html Cache-Control "no-cache"
# 安全头
header {
X-Content-Type-Options "nosniff"
X-Frame-Options "SAMEORIGIN"
Referrer-Policy "strict-origin-when-cross-origin"
}
# API 反向代理
handle /api/* {
reverse_proxy localhost:{$BACKEND_PORT:9001} {
header_up X-Real-IP {remote_host}
}
}
# 后端 API 反向代理
handle /jingrow* {
reverse_proxy localhost:{$BACKEND_PORT:9001} {
header_up X-Real-IP {remote_host}
}
}
# 文件服务(直接服务文件系统,无需 FastAPI
handle /files/* {
root * apps/jingrow/jingrow/public
encode gzip zstd
file_server
}
# 前端静态文件 + SPA 路由支持
handle {
root * apps/jingrow/frontend/dist
encode gzip zstd
try_files {path} /index.html
file_server
}
}