Skip to content

Commit eddbccd

Browse files
authored
fastcgi: remove dir redirection when useless in php_fastcgi (#6698)
* perf: remove dir redirection when useless in php_fastcgi * fix test * review * fix * fix * simplify * simplify again * restore test * add test
1 parent 197c564 commit eddbccd

File tree

2 files changed

+134
-24
lines changed

2 files changed

+134
-24
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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} index.php
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}",
33+
"index.php"
34+
],
35+
"split_path": [
36+
".php",
37+
".php5"
38+
]
39+
}
40+
}
41+
],
42+
"handle": [
43+
{
44+
"handler": "rewrite",
45+
"uri": "{http.matchers.file.relative}"
46+
}
47+
]
48+
},
49+
{
50+
"match": [
51+
{
52+
"path": [
53+
"*.php",
54+
"*.php5"
55+
]
56+
}
57+
],
58+
"handle": [
59+
{
60+
"handler": "reverse_proxy",
61+
"load_balancing": {
62+
"selection_policy": {
63+
"policy": "random"
64+
}
65+
},
66+
"transport": {
67+
"dial_timeout": 3000000000,
68+
"env": {
69+
"VAR1": "value1",
70+
"VAR2": "value2"
71+
},
72+
"protocol": "fastcgi",
73+
"read_timeout": 10000000000,
74+
"root": "/var/www",
75+
"split_path": [
76+
".php",
77+
".php5"
78+
],
79+
"write_timeout": 20000000000
80+
},
81+
"upstreams": [
82+
{
83+
"dial": "localhost:9000"
84+
}
85+
]
86+
}
87+
]
88+
}
89+
]
90+
}
91+
}
92+
}
93+
}
94+
}

modules/caddyhttp/reverseproxy/fastcgi/caddyfile.go

+40-24
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func parsePHPFastCGI(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error
179179
indexFile := "index.php"
180180

181181
// set up for explicitly overriding try_files
182-
tryFiles := []string{}
182+
var tryFiles []string
183183

184184
// if the user specified a matcher token, use that
185185
// matcher in a route that wraps both of our routes;
@@ -310,31 +310,47 @@ func parsePHPFastCGI(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error
310310

311311
// if the index is turned off, we skip the redirect and try_files
312312
if indexFile != "off" {
313-
// route to redirect to canonical path if index PHP file
314-
redirMatcherSet := caddy.ModuleMap{
315-
"file": h.JSON(fileserver.MatchFile{
316-
TryFiles: []string{"{http.request.uri.path}/" + indexFile},
317-
}),
318-
"not": h.JSON(caddyhttp.MatchNot{
319-
MatcherSetsRaw: []caddy.ModuleMap{
320-
{
321-
"path": h.JSON(caddyhttp.MatchPath{"*/"}),
322-
},
323-
},
324-
}),
325-
}
326-
redirHandler := caddyhttp.StaticResponse{
327-
StatusCode: caddyhttp.WeakString(strconv.Itoa(http.StatusPermanentRedirect)),
328-
Headers: http.Header{"Location": []string{"{http.request.orig_uri.path}/"}},
329-
}
330-
redirRoute := caddyhttp.Route{
331-
MatcherSetsRaw: []caddy.ModuleMap{redirMatcherSet},
332-
HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(redirHandler, "handler", "static_response", nil)},
333-
}
313+
dirRedir := false
314+
dirIndex := "{http.request.uri.path}/" + indexFile
334315

335316
// if tryFiles wasn't overridden, use a reasonable default
336317
if len(tryFiles) == 0 {
337-
tryFiles = []string{"{http.request.uri.path}", "{http.request.uri.path}/" + indexFile, indexFile}
318+
tryFiles = []string{"{http.request.uri.path}", dirIndex, indexFile}
319+
dirRedir = true
320+
} else {
321+
for _, tf := range tryFiles {
322+
if tf == dirIndex {
323+
dirRedir = true
324+
325+
break
326+
}
327+
}
328+
}
329+
330+
if dirRedir {
331+
// route to redirect to canonical path if index PHP file
332+
redirMatcherSet := caddy.ModuleMap{
333+
"file": h.JSON(fileserver.MatchFile{
334+
TryFiles: []string{dirIndex},
335+
}),
336+
"not": h.JSON(caddyhttp.MatchNot{
337+
MatcherSetsRaw: []caddy.ModuleMap{
338+
{
339+
"path": h.JSON(caddyhttp.MatchPath{"*/"}),
340+
},
341+
},
342+
}),
343+
}
344+
redirHandler := caddyhttp.StaticResponse{
345+
StatusCode: caddyhttp.WeakString(strconv.Itoa(http.StatusPermanentRedirect)),
346+
Headers: http.Header{"Location": []string{"{http.request.orig_uri.path}/"}},
347+
}
348+
redirRoute := caddyhttp.Route{
349+
MatcherSetsRaw: []caddy.ModuleMap{redirMatcherSet},
350+
HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(redirHandler, "handler", "static_response", nil)},
351+
}
352+
353+
routes = append(routes, redirRoute)
338354
}
339355

340356
// route to rewrite to PHP index file
@@ -352,7 +368,7 @@ func parsePHPFastCGI(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error
352368
HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(rewriteHandler, "handler", "rewrite", nil)},
353369
}
354370

355-
routes = append(routes, redirRoute, rewriteRoute)
371+
routes = append(routes, rewriteRoute)
356372
}
357373

358374
// route to actually reverse proxy requests to PHP files;

0 commit comments

Comments
 (0)