summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/reverseproxy
diff options
context:
space:
mode:
authorMatt Holt <mholt@users.noreply.github.com>2019-09-17 15:16:17 -0600
committerGitHub <noreply@github.com>2019-09-17 15:16:17 -0600
commit484cee1ac12ee590128d85da22b6b9df2efe02d4 (patch)
treeddb5f20da081f8f3994d0dbe6fc7988d11a9620f /modules/caddyhttp/reverseproxy
parentd030bfdae065c8776e8ac14c0a28f725ec05145c (diff)
fastcgi: Implement / redirect for index.php with php_fastcgi directive (#2754)
* fastcgi: Implement / redirect for index.php with php_fastcgi directive See #2752 and https://caddy.community/t/v2-redirect-path-to-path-index-php-with-assets/6196?u=matt * caddyhttp: MatchNegate implements json.Marshaler * fastcgi: Add /index.php element to try_files matcher * fastcgi: Make /index.php redirect permanent
Diffstat (limited to 'modules/caddyhttp/reverseproxy')
-rw-r--r--modules/caddyhttp/reverseproxy/fastcgi/caddyfile.go29
1 files changed, 27 insertions, 2 deletions
diff --git a/modules/caddyhttp/reverseproxy/fastcgi/caddyfile.go b/modules/caddyhttp/reverseproxy/fastcgi/caddyfile.go
index fd82c5a..b7326af 100644
--- a/modules/caddyhttp/reverseproxy/fastcgi/caddyfile.go
+++ b/modules/caddyhttp/reverseproxy/fastcgi/caddyfile.go
@@ -16,6 +16,7 @@ package fastcgi
import (
"encoding/json"
+ "net/http"
"github.com/caddyserver/caddy/v2/caddyconfig"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
@@ -114,10 +115,30 @@ func parsePHPFastCGI(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error
return nil, h.ArgErr()
}
+ // route to redirect to canonical path if index PHP file
+ redirMatcherSet := map[string]json.RawMessage{
+ "file": h.JSON(fileserver.MatchFile{
+ TryFiles: []string{"{http.request.uri.path}/index.php"},
+ }, nil),
+ "not": h.JSON(caddyhttp.MatchNegate{
+ MatchersRaw: map[string]json.RawMessage{
+ "path": h.JSON(caddyhttp.MatchPath{"*/"}, nil),
+ },
+ }, nil),
+ }
+ redirHandler := caddyhttp.StaticResponse{
+ StatusCode: caddyhttp.WeakString("308"),
+ Headers: http.Header{"Location": []string{"{http.request.uri.path}/"}},
+ }
+ redirRoute := caddyhttp.Route{
+ MatcherSetsRaw: []map[string]json.RawMessage{redirMatcherSet},
+ HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(redirHandler, "handler", "static_response", nil)},
+ }
+
// route to rewrite to PHP index file
rewriteMatcherSet := map[string]json.RawMessage{
"file": h.JSON(fileserver.MatchFile{
- TryFiles: []string{"{http.request.uri.path}", "index.php"},
+ TryFiles: []string{"{http.request.uri.path}", "{http.request.uri.path}/index.php", "index.php"},
}, nil),
}
rewriteHandler := rewrite.Rewrite{
@@ -175,7 +196,7 @@ func parsePHPFastCGI(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error
// wrap ours in a subroute and return that
if hasUserMatcher {
subroute := caddyhttp.Subroute{
- Routes: caddyhttp.RouteList{rewriteRoute, rpRoute},
+ Routes: caddyhttp.RouteList{redirRoute, rewriteRoute, rpRoute},
}
return []httpcaddyfile.ConfigValue{
{
@@ -193,6 +214,10 @@ func parsePHPFastCGI(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error
return []httpcaddyfile.ConfigValue{
{
Class: "route",
+ Value: redirRoute,
+ },
+ {
+ Class: "route",
Value: rewriteRoute,
},
{