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