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, 04:00 UTC
fastcgi: Implement `try_files` override in Caddyfile directive (#4347)
Francis Lavoie committed
Sep 17, 2021 at 10:23 UTC
3f2c3ecf85b590473030b21fd2b5fc9c8ef88744
2 files changed
+155
-11
caddytest/integration/caddyfile_adapt/php_fastcgi_try_files_override.txt
new
+124
@@ -0,0 +1,124 @@
1
+:8884
2
+
3
+php_fastcgi localhost:9000 {
4
+ # some php_fastcgi-specific subdirectives
5
+ split .php .php5
6
+ env VAR1 value1
7
+ env VAR2 value2
8
+ root /var/www
9
+ try_files {path} {path}/index.php =404
10
+ dial_timeout 3s
11
+ read_timeout 10s
12
+ write_timeout 20s
13
+
14
+ # passed through to reverse_proxy (directive order doesn't matter!)
15
+ lb_policy random
16
+}
17
+----------
18
+{
19
+ "apps": {
20
+ "http": {
21
+ "servers": {
22
+ "srv0": {
23
+ "listen": [
24
+ ":8884"
25
+ ],
26
+ "routes": [
27
+ {
28
+ "match": [
29
+ {
30
+ "file": {
31
+ "try_files": [
32
+ "{http.request.uri.path}/index.php"
33
+ ]
34
+ },
35
+ "not": [
36
+ {
37
+ "path": [
38
+ "*/"
39
+ ]
40
+ }
41
+ ]
42
+ }
43
+ ],
44
+ "handle": [
45
+ {
46
+ "handler": "static_response",
47
+ "headers": {
48
+ "Location": [
49
+ "{http.request.uri.path}/"
50
+ ]
51
+ },
52
+ "status_code": 308
53
+ }
54
+ ]
55
+ },
56
+ {
57
+ "match": [
58
+ {
59
+ "file": {
60
+ "try_files": [
61
+ "{http.request.uri.path}",
62
+ "{http.request.uri.path}/index.php",
63
+ "=404"
64
+ ],
65
+ "split_path": [
66
+ ".php",
67
+ ".php5"
68
+ ]
69
+ }
70
+ }
71
+ ],
72
+ "handle": [
73
+ {
74
+ "handler": "rewrite",
75
+ "uri": "{http.matchers.file.relative}"
76
+ }
77
+ ]
78
+ },
79
+ {
80
+ "match": [
81
+ {
82
+ "path": [
83
+ "*.php",
84
+ "*.php5"
85
+ ]
86
+ }
87
+ ],
88
+ "handle": [
89
+ {
90
+ "handler": "reverse_proxy",
91
+ "load_balancing": {
92
+ "selection_policy": {
93
+ "policy": "random"
94
+ }
95
+ },
96
+ "transport": {
97
+ "dial_timeout": 3000000000,
98
+ "env": {
99
+ "VAR1": "value1",
100
+ "VAR2": "value2"
101
+ },
102
+ "protocol": "fastcgi",
103
+ "read_timeout": 10000000000,
104
+ "root": "/var/www",
105
+ "split_path": [
106
+ ".php",
107
+ ".php5"
108
+ ],
109
+ "write_timeout": 20000000000
110
+ },
111
+ "upstreams": [
112
+ {
113
+ "dial": "localhost:9000"
114
+ }
115
+ ]
116
+ }
117
+ ]
118
+ }
119
+ ]
120
+ }
121
+ }
122
+ }
123
+ }
124
+}
modules/caddyhttp/reverseproxy/fastcgi/caddyfile.go
+31
-11
@@ -124,21 +124,22 @@ func (t *Transport) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
124
//
125
// is equivalent to a route consisting of:
126
//
127
+// # Add trailing slash for directory requests
128
// @canonicalPath {
128
-// file {
129
-// try_files {path}/index.php
130
-// }
131
-// not {
132
-// path */
133
-// }
129
+// file {path}/index.php
130
+// not path */
131
// }
132
// redir @canonicalPath {path}/ 308
133
//
137
-// try_files {path} {path}/index.php index.php
138
-//
139
-// @phpFiles {
140
-// path *.php
134
+// # If the requested file does not exist, try index files
135
+// @indexFiles file {
136
+// try_files {path} {path}/index.php index.php
137
+// split_path .php
138
// }
139
+// rewrite @indexFiles {http.matchers.file.relative}
140
+//
141
+// # Proxy PHP files to the FastCGI responder
142
+// @phpFiles path *.php
143
// reverse_proxy @phpFiles localhost:7777 {
144
// transport fastcgi {
145
// split .php
@@ -172,6 +173,9 @@ func parsePHPFastCGI(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error
173
// set the default index file for the try_files rewrites
174
indexFile := "index.php"
175
176
+ // set up for explicitly overriding try_files
177
+ tryFiles := []string{}
178
+
179
// if the user specified a matcher token, use that
180
// matcher in a route that wraps both of our routes;
181
// either way, strip the matcher token and pass
@@ -237,6 +241,17 @@ func parsePHPFastCGI(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error
241
}
242
indexFile = args[0]
243
244
+ case "try_files":
245
+ args := dispenser.RemainingArgs()
246
+ dispenser.Delete()
247
+ for range args {
248
+ dispenser.Delete()
249
+ }
250
+ if len(args) < 1 {
251
+ return nil, dispenser.ArgErr()
252
+ }
253
+ tryFiles = args
254
+
255
case "resolve_root_symlink":
256
args := dispenser.RemainingArgs()
257
dispenser.Delete()
@@ -318,10 +333,15 @@ func parsePHPFastCGI(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error
333
HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(redirHandler, "handler", "static_response", nil)},
334
}
335
336
+ // if tryFiles wasn't overridden, use a reasonable default
337
+ if len(tryFiles) == 0 {
338
+ tryFiles = []string{"{http.request.uri.path}", "{http.request.uri.path}/" + indexFile, indexFile}
339
+ }
340
+
341
// route to rewrite to PHP index file
342
rewriteMatcherSet := caddy.ModuleMap{
343
"file": h.JSON(fileserver.MatchFile{
324
- TryFiles: []string{"{http.request.uri.path}", "{http.request.uri.path}/" + indexFile, indexFile},
344
+ TryFiles: tryFiles,
345
SplitPath: extensions,
346
}),
347
}