@marcel91200 / caddy Mirror · read-only / commits / 403732c4
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

httpcaddyfile: Reorder some directives (#4311)

We realized we made some mistakes with the directive ordering, so we're making some minor adjustments. `abort` and `error` don't really make sense to be after other handler directives, because you would expect to be able to "fail-fast" and throw an error before falling through to some `file_server` or `respond` typically. So we're moving them up to just before `respond`, i.e. before the common handler directives. This is also more consistent with our existing examples in the docs, which actually didn't work due to the directive ordering. See https://caddyserver.com/docs/caddyfile/directives/error#examples Also, `push` doesn't quite make sense to be after `handle`/`route`, since its job is to read from response headers to push additional resources if necessary, and `handle`/`route` may be terminal so push would not be reached if it was declared outside those. And also, it would make sense to be _before_ `templates` because a template _could_ add a `Link` header to the response dynamically.

Francis Lavoie committed Aug 26, 2021 at 16:31 UTC 403732c433b816409e3525a9f49b4cc90abc2486
2 files changed +142 -4
caddyconfig/httpcaddyfile/directives.go
+4 -4
@@ -44,9 +44,9 @@ var directiveOrder = []string{
44 "request_body",
45
46 "redir",
47 - "rewrite",
47
48 // URI manipulation
49 + "rewrite",
50 "uri",
51 "try_files",
52
@@ -54,23 +54,23 @@ var directiveOrder = []string{
54 "basicauth",
55 "request_header",
56 "encode",
57 + "push",
58 "templates",
59
60 // special routing & dispatching directives
61 "handle",
62 "handle_path",
63 "route",
63 - "push",
64
65 // handlers that typically respond to requests
66 + "abort",
67 + "error",
68 "respond",
69 "metrics",
70 "reverse_proxy",
71 "php_fastcgi",
72 "file_server",
73 "acme_server",
72 - "abort",
73 - "error",
74 }
75
76 // directiveIsOrdered returns true if dir is
caddytest/integration/caddyfile_adapt/error_example.txt new
+138
@@ -0,0 +1,138 @@
1 +example.com {
2 + root * /srv
3 +
4 + # Trigger errors for certain paths
5 + error /private* "Unauthorized" 403
6 + error /hidden* "Not found" 404
7 +
8 + # Handle the error by serving an HTML page
9 + handle_errors {
10 + rewrite * /{http.error.status_code}.html
11 + file_server
12 + }
13 +
14 + file_server
15 +}
16 +----------
17 +{
18 + "apps": {
19 + "http": {
20 + "servers": {
21 + "srv0": {
22 + "listen": [
23 + ":443"
24 + ],
25 + "routes": [
26 + {
27 + "match": [
28 + {
29 + "host": [
30 + "example.com"
31 + ]
32 + }
33 + ],
34 + "handle": [
35 + {
36 + "handler": "subroute",
37 + "routes": [
38 + {
39 + "handle": [
40 + {
41 + "handler": "vars",
42 + "root": "/srv"
43 + }
44 + ]
45 + },
46 + {
47 + "handle": [
48 + {
49 + "error": "Unauthorized",
50 + "handler": "error",
51 + "status_code": 403
52 + }
53 + ],
54 + "match": [
55 + {
56 + "path": [
57 + "/private*"
58 + ]
59 + }
60 + ]
61 + },
62 + {
63 + "handle": [
64 + {
65 + "error": "Not found",
66 + "handler": "error",
67 + "status_code": 404
68 + }
69 + ],
70 + "match": [
71 + {
72 + "path": [
73 + "/hidden*"
74 + ]
75 + }
76 + ]
77 + },
78 + {
79 + "handle": [
80 + {
81 + "handler": "file_server",
82 + "hide": [
83 + "./Caddyfile"
84 + ]
85 + }
86 + ]
87 + }
88 + ]
89 + }
90 + ],
91 + "terminal": true
92 + }
93 + ],
94 + "errors": {
95 + "routes": [
96 + {
97 + "match": [
98 + {
99 + "host": [
100 + "example.com"
101 + ]
102 + }
103 + ],
104 + "handle": [
105 + {
106 + "handler": "subroute",
107 + "routes": [
108 + {
109 + "group": "group0",
110 + "handle": [
111 + {
112 + "handler": "rewrite",
113 + "uri": "/{http.error.status_code}.html"
114 + }
115 + ]
116 + },
117 + {
118 + "handle": [
119 + {
120 + "handler": "file_server",
121 + "hide": [
122 + "./Caddyfile"
123 + ]
124 + }
125 + ]
126 + }
127 + ]
128 + }
129 + ],
130 + "terminal": true
131 + }
132 + ]
133 + }
134 + }
135 + }
136 + }
137 + }
138 +}
\ No newline at end of file