HUGO
Menu
GitHub 87548 stars Mastodon

Hugo 配置服務器

Hugo 配置開發服務器。

這些設置專用於 Hugo 的開發服務器,因此推薦的方法是使用專用的開發 配置目錄,在其中相應地配置服務器。

project/
└── config/
    ├── _default/
    │   └── hugo.toml
    └── development/
        └── server.toml

默認設置

開發服務器默認將對任何不存在的 URL 的請求重定向到 /404.html。詳見下面的 404 錯誤 部分。

server:
  headers: null
  redirects:
  - force: false
    from: /**
    fromHeaders: null
    fromRe: ''
    status: 404
    to: /404.html
[server]
  [[server.redirects]]
    force = false
    from = '/**'
    fromRe = ''
    status = 404
    to = '/404.html'
{
   "server": {
      "headers": null,
      "redirects": [
         {
            "force": false,
            "from": "/**",
            "fromHeaders": null,
            "fromRe": "",
            "status": 404,
            "to": "/404.html"
         }
      ]
   }
}
force
(bool) 是否強制重定向,即使路徑中已存在內容。
from
(string) 匹配請求 URL 的 glob 模式。必須設置 fromfromRE。如果同時指定了 fromfromRe,則 URL 必須匹配兩種模式。
fromHeaders
New in v0.144.0
(map[string][string]) 用於重定向的匹配頭。這將 HTTP 頭名稱映射到要匹配的值的 glob 模式。如果映射為空,則將始終觸發重定向。
fromRe
New in v0.144.0
(string) 用於匹配請求 URL 的 正則表達式。必須設置 fromfromRE。如果同時指定了 fromfromRe,則 URL 必須匹配兩種模式。正則表達式中的捕獲組可在 to 字段中作為 $1$2 等訪問。
status
(string) 用於重定向的 HTTP 狀態碼。200 的狀態碼將觸發 URL 重寫。
to
(string) 將請求轉發到的 URL。

在每個服務器響應中包含頭以促進測試,特別是對於 內容安全策略 等功能。

headers:
- for: /**
  values:
    Content-Security-Policy: script-src localhost:1313
    Referrer-Policy: strict-origin-when-cross-origin
    X-Content-Type-Options: nosniff
    X-Frame-Options: DENY
    X-XSS-Protection: 1; mode=block
[[headers]]
  for = '/**'
  [headers.values]
    Content-Security-Policy = 'script-src localhost:1313'
    Referrer-Policy = 'strict-origin-when-cross-origin'
    X-Content-Type-Options = 'nosniff'
    X-Frame-Options = 'DENY'
    X-XSS-Protection = '1; mode=block'
{
   "headers": [
      {
         "for": "/**",
         "values": {
            "Content-Security-Policy": "script-src localhost:1313",
            "Referrer-Policy": "strict-origin-when-cross-origin",
            "X-Content-Type-Options": "nosniff",
            "X-Frame-Options": "DENY",
            "X-XSS-Protection": "1; mode=block"
         }
      }
   ]
}

重定向

您可以定義簡單的重定向規則。

redirects:
- force: false
  from: /myspa/**
  status: 200
  to: /myspa/
[[redirects]]
  force = false
  from = '/myspa/**'
  status = 200
  to = '/myspa/'
{
   "redirects": [
      {
         "force": false,
         "from": "/myspa/**",
         "status": 200,
         "to": "/myspa/"
      }
   ]
}

此示例中的 200 狀態碼會觸發 URL 重寫,這通常是 單頁應用程序 所需的行為。

404 錯誤

開發服務器默認將對任何不存在的 URL 的請求重定向到 /404.html。

server:
  headers: null
  redirects:
  - force: false
    from: /**
    fromHeaders: null
    fromRe: ''
    status: 404
    to: /404.html
[server]
  [[server.redirects]]
    force = false
    from = '/**'
    fromRe = ''
    status = 404
    to = '/404.html'
{
   "server": {
      "headers": null,
      "redirects": [
         {
            "force": false,
            "from": "/**",
            "fromHeaders": null,
            "fromRe": "",
            "status": 404,
            "to": "/404.html"
         }
      ]
   }
}

如果您已經定義了其他重定向,則必須顯式添加 404 重定向。

redirects:
- force: false
  from: /**
  status: 404
  to: /404.html
[[redirects]]
  force = false
  from = '/**'
  status = 404
  to = '/404.html'
{
   "redirects": [
      {
         "force": false,
         "from": "/**",
         "status": 404,
         "to": "/404.html"
      }
   ]
}

對於多語言站點,確保默認語言的 404 重定向最後定義:

defaultContentLanguage: en
defaultContentLanguageInSubdir: false
redirects:
- from: /fr/**
  status: 404
  to: /fr/404.html
- from: /**
  status: 404
  to: /404.html
defaultContentLanguage = 'en'
defaultContentLanguageInSubdir = false
[[redirects]]
  from = '/fr/**'
  status = 404
  to = '/fr/404.html'
[[redirects]]
  from = '/**'
  status = 404
  to = '/404.html'
{
   "defaultContentLanguage": "en",
   "defaultContentLanguageInSubdir": false,
   "redirects": [
      {
         "from": "/fr/**",
         "status": 404,
         "to": "/fr/404.html"
      },
      {
         "from": "/**",
         "status": 404,
         "to": "/404.html"
      }
   ]
}

當默認語言從子目錄提供服務時:

defaultContentLanguage: en
defaultContentLanguageInSubdir: true
redirects:
- from: /fr/**
  status: 404
  to: /fr/404.html
- from: /**
  status: 404
  to: /en/404.html
defaultContentLanguage = 'en'
defaultContentLanguageInSubdir = true
[[redirects]]
  from = '/fr/**'
  status = 404
  to = '/fr/404.html'
[[redirects]]
  from = '/**'
  status = 404
  to = '/en/404.html'
{
   "defaultContentLanguage": "en",
   "defaultContentLanguageInSubdir": true,
   "redirects": [
      {
         "from": "/fr/**",
         "status": 404,
         "to": "/fr/404.html"
      },
      {
         "from": "/**",
         "status": 404,
         "to": "/en/404.html"
      }
   ]
}

Last updated: January 1, 0001
Improve this page