第 3 章 站点与路由

Last Updated: 2026-12-04 09:44 Views: 7

3.1 站点模型

一个站点对应一个 servers 节点,保存在 conf/vhost/{name}.toml。主配置通过 http 中的 include = ["vhost/*.toml"] 聚合,管理后台保存时自动按站点拆分文件;站点 name 仅允许字母、数字、.、-、_,留空时按域名推导。

键 作用
name 仅 Manager/转换器:选择 conf/vhost/{name}.toml;不在站点文件保存
domain 域名数组,* 表示兜底站点
listen 监听地址、TLS、HTTP/2、HTTP/3
root / index 站点根目录与默认首页,默认 ["index.html"]
locations 内联数组,正则按数组顺序匹配
ssl 证书库名称或证书/私钥路径
error_page 状态码到 URI 或文件的映射
access_log / waf 覆盖全局日志;站点级 WAF 开关与规则

站点文件名即站点名:站点文件里写 name 会被忽略且不会写回;一个文件可包含多个站点(同名站点保存在同一文件)。Manager 的 name 仅用于选择保存文件,留空按域名推导。

3.2 域名与监听

站点匹配顺序为:精确域名 → *.example.com / example.* 通配(按站点文件顺序)→ domain = ["*"] 兜底 → 第一个站点。同一 addr 可由多个站点声明,TLS 握手按 SNI 选择证书,HTTP 请求按 Host 选择路由。

listen 支持 ":80"、"8080"、"1.2.3.4:8080"、"[::]:443"。常用组合:

listen = [{ addr = ":80" }]
listen = [{ addr = ":443", ssl = true, http2 = true, http3 = true }]

http3 = true 必须同时 ssl = true,服务器会在同一端口额外监听 UDP,并在 TCP 响应中发送 Alt-Svc: h3=":端口"; ma=86400。

3.3 location 匹配

写法 类型
= /exact 精确匹配,优先级最高
/prefix/ 前缀匹配
^~ /static/ 前缀匹配且命中后跳过正则
~ \.php$ 正则,区分大小写
~* \.jpg$ 正则,不区分大小写

优先级为:精确 → 最长前缀(^~ 命中则不再尝试正则)→ 正则按配置顺序 → 最长前缀。正则 location 与通配域名都按配置顺序生效,调整时不要随意换序。

3.4 内容处理器

location.type 决定由哪个模块处理请求:

type 说明
static 静态文件,支持 root/alias/index/autoindex/try_files/WebDAV
proxy 反向代理,支持负载均衡、重试、WebSocket、缓存与分片
fastcgi PHP 等 FastCGI 后端,支持 split_path_info 与连接复用
uwsgi / scgi uwsgi / SCGI 协议后端
grpc HTTP/2 h2c 或 TLS 的 gRPC 透传
memcached 以 memcached 为内容源,未命中返回 404
return / redirect 直接返回状态码、文本或跳转
image / mp4 缩略图加水印、MP4 伪流媒体
empty_gif / stub_status 1x1 透明 GIF、状态页

3.5 错误页与站点级覆盖

error_page 支持三种目标形态(可写在 http / 站点 / location,继承链为 location > 站点 > http):

形态 示例 行为
file: file:4xx.html、file:/var/wxserver/html/4xx.html 相对路径相对 error_page_root(默认内置页面目录 /var/wxserver/html 或 <exe>/../html);绝对路径按完整路径;读取并渲染
root: root:errors/404.html 相对站点 root;读取并渲染
link: link:/404.html、link:@fallback、link:https://example.com/404 内部跳转(完整 URL 为 302),不渲染

未配置时默认 4xx → file:4xx.html、5xx → file:5xx.html(本地生成的错误默认生效;后端返回的错误需 *_intercept_errors)。文件上限 1MB,使用 html/template 渲染,变量自动转义;-t 与 Manager 保存会校验形态语法(未知前缀、..、控制字符直接报错)。旧写法(/uri、相对路径、@name、http(s)://)继续兼容。

站点内可单独关闭访问日志(access_log = { enabled = false })或 WAF(waf = { enabled = false }),也可用 client_max_body_size、limit_rate 等在 location 覆盖默认值。

3.6 最小示例

http = {
  servers = [
    {
      domain = ["example.com", "www.example.com"],
      listen = [{ addr = ":443", ssl = true, http2 = true }],
      ssl = { cert = "example.com" },
      root = "html",
      index = ["index.html"],
      error_page = { "404" = "file:4xx.html", "5xx" = "file:5xx.html" },
      waf = { enabled = true },
      locations = [
        { path = "/", type = "static" },
        { path = "/api/", type = "proxy", proxy_pass = "http://backend" },
      ],
    },
  ],
}

3.7 站点级协议与模块覆盖

HTTP/2、HTTP/3 的开关写在对应监听行上(ssl = true, http2 = true / http3 = true);站点可用 disabled_modules = ["http_zstd"] 单独禁用模块。站点级 http2 = false、http3 = false 是兼容的硬关闭,全局 http2/http3 = false 不能被站点反开。

sendfile 也可在站点/location 级覆盖,见第 9 章。

← Previous Article 第 2 章 Web 管理后台 Next Article → 第 4 章 TLS 与证书

Comments

No comments yet