Skip to content

Commit 2d6a299

Browse files
authored
perf: improve php_server directive (#1180)
1 parent b4748ee commit 2d6a299

3 files changed

Lines changed: 46 additions & 26 deletions

File tree

caddy/caddy.go

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ func parsePhpServer(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error)
448448
indexFile := "index.php"
449449

450450
// set up for explicitly overriding try_files
451-
tryFiles := []string{}
451+
var tryFiles []string
452452

453453
// if the user specified a matcher token, use that
454454
// matcher in a route that wraps both of our routes;
@@ -546,31 +546,52 @@ func parsePhpServer(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error)
546546

547547
// if the index is turned off, we skip the redirect and try_files
548548
if indexFile != "off" {
549-
// route to redirect to canonical path if index PHP file
550-
redirMatcherSet := caddy.ModuleMap{
551-
"file": h.JSON(fileserver.MatchFile{
552-
TryFiles: []string{"{http.request.uri.path}/" + indexFile},
553-
}),
554-
"not": h.JSON(caddyhttp.MatchNot{
555-
MatcherSetsRaw: []caddy.ModuleMap{
556-
{
557-
"path": h.JSON(caddyhttp.MatchPath{"*/"}),
558-
},
559-
},
560-
}),
561-
}
562-
redirHandler := caddyhttp.StaticResponse{
563-
StatusCode: caddyhttp.WeakString(strconv.Itoa(http.StatusPermanentRedirect)),
564-
Headers: http.Header{"Location": []string{"{http.request.orig_uri.path}/"}},
565-
}
566-
redirRoute := caddyhttp.Route{
567-
MatcherSetsRaw: []caddy.ModuleMap{redirMatcherSet},
568-
HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(redirHandler, "handler", "static_response", nil)},
569-
}
549+
dirRedir := false
550+
dirIndex := "{http.request.uri.path}/" + indexFile
570551

571552
// if tryFiles wasn't overridden, use a reasonable default
572553
if len(tryFiles) == 0 {
573-
tryFiles = []string{"{http.request.uri.path}", "{http.request.uri.path}/" + indexFile, indexFile}
554+
if disableFsrv {
555+
tryFiles = []string{dirIndex, indexFile}
556+
} else {
557+
tryFiles = []string{"{http.request.uri.path}", dirIndex, indexFile}
558+
}
559+
560+
dirRedir = true
561+
} else {
562+
for _, tf := range tryFiles {
563+
if tf == dirIndex {
564+
dirRedir = true
565+
566+
break
567+
}
568+
}
569+
}
570+
571+
// route to redirect to canonical path if index PHP file
572+
if dirRedir {
573+
redirMatcherSet := caddy.ModuleMap{
574+
"file": h.JSON(fileserver.MatchFile{
575+
TryFiles: []string{dirIndex},
576+
}),
577+
"not": h.JSON(caddyhttp.MatchNot{
578+
MatcherSetsRaw: []caddy.ModuleMap{
579+
{
580+
"path": h.JSON(caddyhttp.MatchPath{"*/"}),
581+
},
582+
},
583+
}),
584+
}
585+
redirHandler := caddyhttp.StaticResponse{
586+
StatusCode: caddyhttp.WeakString(strconv.Itoa(http.StatusPermanentRedirect)),
587+
Headers: http.Header{"Location": []string{"{http.request.orig_uri.path}/"}},
588+
}
589+
redirRoute := caddyhttp.Route{
590+
MatcherSetsRaw: []caddy.ModuleMap{redirMatcherSet},
591+
HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(redirHandler, "handler", "static_response", nil)},
592+
}
593+
594+
routes = append(routes, redirRoute)
574595
}
575596

576597
// route to rewrite to PHP index file
@@ -588,7 +609,7 @@ func parsePhpServer(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error)
588609
HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(rewriteHandler, "handler", "rewrite", nil)},
589610
}
590611

591-
routes = append(routes, redirRoute, rewriteRoute)
612+
routes = append(routes, rewriteRoute)
592613
}
593614

594615
// route to actually pass requests to PHP files;

caddy/caddy_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ func TestPHPServerDirectiveDisableFileServer(t *testing.T) {
312312
`, "caddyfile")
313313

314314
tester.AssertGetResponse("http://localhost:"+testPort, http.StatusOK, "I am by birth a Genevese (i not set)")
315-
tester.AssertGetResponse("http://localhost:"+testPort+"/hello.txt", http.StatusNotFound, "Not found")
315+
tester.AssertGetResponse("http://localhost:"+testPort+"/not-found.txt", http.StatusOK, "I am by birth a Genevese (i not set)")
316316
}
317317

318318
func TestMetrics(t *testing.T) {

worker.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ func (worker *worker) startNewWorkerThread() {
9595
backingOffLock := sync.RWMutex{}
9696

9797
for {
98-
9998
// if the worker can stay up longer than backoff*2, it is probably an application error
10099
upFunc := sync.Once{}
101100
go func() {

0 commit comments

Comments
 (0)