You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 37107fb
Browse filesBrowse the repository at this point in the historyBrowse files
docs: document the optional status filter of custom http headers
Describe the optional `status` key of `[[advanced.headers]]` entries, the
responses custom headers never reach, the fallback page caveat and how a
filtered entry overrides an earlier unfiltered one.
Copy file name to clipboardExpand all lines: src/v3/features/custom-http-headers.md
+48-3Lines changed: 48 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,12 +10,13 @@ outline: deep
10
10
11
11
The Server HTTP response headers should be defined mainly as an [Array of Tables](https://toml.io/en/v1.0.0#array-of-tables).
12
12
13
-
Each table entry should have two key/value pairs:
13
+
Each table entry should have the following key/value pairs:
14
14
15
15
- One `source` key containing a string _glob pattern_.
16
16
- One `headers` key containing a [set or hash table](https://toml.io/en/v1.0.0#table) describing plain HTTP headers to apply.
17
+
- An optional `status` key containing an array of HTTP response status codes.
17
18
18
-
A particular set of HTTP headers can only be applied when a `source` matches against the request URI.
19
+
A particular set of HTTP headers can only be applied when a `source` matches against the request URI and, if `status` is defined, the response status code is one of its values.
19
20
20
21
> [!INFO] Custom HTTP headers take precedence over existing ones
21
22
>
@@ -33,9 +34,21 @@ The source is a [Glob pattern](<https://en.wikipedia.org/wiki/Glob_(programming)
33
34
34
35
A set of valid plain [HTTP headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers) to be applied.
35
36
37
+
### Status
38
+
39
+
An optional array of [HTTP response status codes](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) (numbers) that limits the entry to responses with one of those codes, for example `status = [200, 206, 304]`.
40
+
41
+
Without `status`, the headers apply regardless of the response status, including error responses like `404 Not Found`. This fits headers such as `Strict-Transport-Security`, but not caching headers: an `immutable``Cache-Control` for `/assets/**` would also be sent with a `404` for a missing asset.
42
+
43
+
Custom headers are not added to responses that SWS returns before serving a file, such as [URL redirects](url-redirects.md), `405 Method Not Allowed` or maintenance mode responses, so a `status` of `301` or `503` never matches those.
44
+
45
+
To limit caching headers to successful file responses, include `304` next to `200` and `206`: a `304 Not Modified` updates the headers of the response stored by a cache ([RFC 9111, section 4.3.4](https://www.rfc-editor.org/rfc/rfc9111#section-4.3.4)).
46
+
47
+
SWS validates the codes at startup and fails to start on an empty array or a code outside `100` to `999`.
48
+
36
49
## Examples
37
50
38
-
Below are some examples of how to customize server HTTP headers in three variants.
51
+
Below are some examples of how to customize server HTTP headers.
`GET /assets/app.css` returns `200 OK` with `cache-control: public, max-age=31536000, immutable`, while `GET /assets/missing.css` returns `404 Not Found` without the `immutable` directive.
98
+
99
+
> [!INFO] Fallback page
100
+
>
101
+
> With a [fallback page](error-pages.md#fallback-page-for-use-with-client-routers) configured, a `GET` for a missing file is answered with `200 OK` and the fallback page, so status filtering does not exclude it.
102
+
103
+
Entries apply in order and a later entry replaces a header set by an earlier one. Below, every `/assets/**` response gets `no-cache`, and the second entry replaces it with `immutable` for `200`, `206` and `304` responses only.
0 commit comments