配置参考

Avalon 使用 TOML 格式的配置文件。本文档详细说明所有可用的配置选项。

配置结构

[global]      # 全局设置
[tls]         # TLS/HTTPS 设置
[[servers]]   # 服务器配置 (数组)

[global] 全局设置

选项类型默认值说明
log_levelstring"info"日志级别: trace, debug, info, warn, error
admin_listenstring"localhost:2019"Admin API 监听地址
access_logstring-访问日志文件路径
access_log_formatstring"common"日志格式: common, json, combined

[global.compression] 压缩设置

选项类型默认值说明
enabledbooltrue启用响应压缩
gzipbooltrue启用 gzip
brotlibooltrue启用 brotli
min_sizeint1024最小压缩大小 (字节)
levelint6压缩级别 (gzip: 1-9, brotli: 0-11)

[global.cache] 缓存设置

选项类型默认值说明
enabledboolfalse启用响应缓存
default_ttlint300默认缓存时间 (秒)
max_entry_sizeint10485760单条缓存最大大小 (10MB)
max_cache_sizeint104857600缓存总大小上限 (100MB)

[tls] TLS 设置

选项类型默认值说明
emailstring-ACME 账户邮箱 (必填,如果启用 ACME)
acme_enabledbooltrue启用 ACME 自动证书
acme_castringLet's EncryptACME CA URL 或提供商名称
storage_pathstring"./certs"证书存储目录
cert_pathstring-手动指定证书文件路径
key_pathstring-手动指定私钥文件路径

ACME CA 可选值:

  • letsencrypt - Let's Encrypt (默认)
  • le-staging - Let's Encrypt 测试环境
  • zerossl - ZeroSSL
  • buypass - Buypass
  • google - Google Trust Services

[[servers]] 服务器配置

选项类型默认值说明
namestring"default"服务器名称 (用于日志)
listenarray-监听地址列表 (必填)
https_redirectboolfalse自动重定向 HTTP 到 HTTPS

Handler 类型

reverse_proxy - 反向代理

[servers.routes.handle]
type = "reverse_proxy"
upstreams = ["127.0.0.1:3000", "127.0.0.1:3001"]
load_balancing = "round_robin"
timeout = 30

负载均衡策略:

  • round_robin - 轮询
  • random - 随机
  • least_conn - 最少连接
  • ip_hash - IP 哈希
  • first - 始终使用第一个

file_server - 静态文件服务

[servers.routes.handle]
type = "file_server"
root = "/var/www/html"
browse = false
index = ["index.html", "index.htm"]
compress = true

static_response - 静态响应

[servers.routes.handle]
type = "static_response"
status = 200
body = "Hello, World!"

[servers.routes.handle.headers]
Content-Type = "text/plain"

redirect - 重定向

[servers.routes.handle]
type = "redirect"
to = "https://example.com{uri}"
code = 301

认证

Basic 认证

[servers.routes.handle.auth]
realm = "Protected Area"

[[servers.routes.handle.auth.basic]]
username = "admin"
password = "password123"

API Key 认证

[[servers.routes.handle.auth.api_keys]]
key = "sk-xxxx"
name = "production"
source = "header"
param_name = "X-API-Key"

JWT 认证

[servers.routes.handle.auth.jwt]
secret = "your-secret-key"
algorithm = "HS256"
header = "Authorization"

CORS 跨域

[servers.routes.handle.cors]
allowed_origins = ["https://example.com"]
allowed_methods = ["GET", "POST", "PUT", "DELETE"]
allowed_headers = ["Content-Type", "Authorization"]
allow_credentials = true
max_age = 3600