第 3 章 網站與路由

Last Updated: 2026-12-04 09:44 Views: 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 與管理後臺儲存會驗證形態語法(未知前綴、..、控制字元直接報錯)。舊寫法(/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