第 3 章 站點與路由
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 章。