URL rewriting for Craft CMS 3

1. My Caddy version:

v2.0.0-beta9 h1:oILdAOfunJ4ijBN9kOWjFIeH8EufBX/N1pC9HbnwjzU=

2. How I run Caddy:

Run binary downloaded from Github at the terminal for now.

a. System environment:

Linux, Ubuntu 18.04

b. Command:

caddy2 run --config Caddyfile2 --adapter caddyfile

d. My complete Caddyfile:

http://sitename.localhost
tls off
root * /path/to/project/web
encode gzip
php_fastcgi unix//run/php/php7.3-fpm.sock
try_files index.php?p={path}&{query}
file_server

3. The problem I’m having:

URLs that don’t lead to actual files result in the home page being served rather than whatever page was requested.

I’m running Craft CMS 3 (PHP), and the rewrite directive that worked for Caddy 1 is:

rewrite {
	to {path} {path}/ /index.php?p={path}&{query} /index.php?{query}
}

These are the Apache rewrite rules:

    # Send would-be 404 requests to Craft
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteRule (.+) index.php?p=$1 [QSA,L]

5. What I already tried:

A

Instead of my try_files directive, I’ve tried:

rewrite * /index.php?p={path}&{query}

This results in http.log.error too many rehandles in Craft’s output.


B

php_fastcgi unix//run/php/php7.3-fpm.sock
matcher craftPages {
	path /blog
}
rewrite match:craftPages /index.php?p={path}&{query}
file_server

This enables the blog page /blog to load properly (yay!), but I need to achieve the same for dynamic paths.


C

I’ve also tried using reverse_proxy with a modified version of what the documentation states php_fastcgi does, but this only gives me 404s for what should be PHP-served pages.

# canonicalize URI trailing slash when index file present
matcher indexFiles {
	not {
		path */
	}
	file {
		try_files {path}/index.php
	}
}
redir match:indexFiles {path}/

# internally rewrite directory URIs to index.php files
# try_files {path} {path}/index.php index.php
try_files {path} /index.php?p={path}&{query} /index.php?{query}

# proxy any requests for PHP files to backend via FastCGI
matcher phpFiles {
	path *.php
}
reverse_proxy match:phpFiles unix//run/php/php7.3-fpm.sock {
	transport fastcgi {
		split .php
	}
}

6. Links to relevant resources:

https://caddyserver.com/v1/docs/rewrite

Home · caddyserver/caddy Wiki · GitHub

Home · caddyserver/caddy Wiki · GitHub


I’ll continue to use Caddy 1 for now. Thanks in advance for any guidance you’re able to provide!

Hi @BrightEyesD,

I’d have thought that C would be correct here.

Perhaps @matt or @francislavoie might cast an eye over this one and venture a possible cause for this issue.

I’ve tried to create a simple test case.

v2.0 beta 9

/m/projects/site/project/web/test.php (shows PHP Variables):

<?php phpinfo(32); ?>

The Caddyfile is below, with comments listing results of requesting this URL:

http://www.example.localhost/blog

http://www.example.localhost
tls off
root * /m/projects/site/project/web

# Only one of the three try_files uncommented at a time
try_files /test.php?p={path}&{query}  # 404 status: 'File not found.'
try_files /test.php?                  # 404 status: 'File not found.'
try_files /test.php                   # shows PHP Variables page

reverse_proxy {
	to unix//run/php/php7.3-fpm.sock
	transport fastcgi {
		split .php
	}
}

Am I right in thinking that all three try_files should show the test.php PHP Variables page, as processed and output by PHP? Does adding the ? to the filename result in Caddy looking for a file literally named test.php??

As an aside, I came across this while experimenting: when requesting http://www.example.localhost/?p=1 with try_files /test.php? or try_files /test.php?p={path}&{query} (but not with try_files /test.php) uncommented I get the following:

Error
2019/11/18 10:34:34.672 INFO    http.log.access received request        {"request": {"method": "GET", "uri": "/?p=1", "proto": "HTTP/1.1", "remote_addr": "[::1]:42920", "host": "www.example.localhost", "headers": {"Pragma": ["no-cache"], "Cache-Control": ["no-cache"], "User-Agent": ["Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:70.0) Gecko/20100101 Firefox/70.0"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"], "Accept-Encoding": ["gzip, deflate"], "Connection": ["keep-alive"], "Upgrade-Insecure-Requests": ["1"], "Accept-Language": ["en-GB,en;q=0.5"], "Dnt": ["1"]}}, "common_log": "::1 - - [18/Nov/2019:11:34:34 +0100] \"GET /?p=1 HTTP/1.1\" 0 0", "latency": 0.000039485, "size": 0, "status": 0}
2019/11/18 11:34:34 http: panic serving [::1]:42920: runtime error: slice bounds out of range [:3] with length 1
goroutine 113 [running]:
net/http.(*conn).serve.func1(0xc000131220)
        /usr/local/go/src/net/http/server.go:1767 +0x139
panic(0xd5a140, 0xc000274b00)
        /usr/local/go/src/runtime/panic.go:679 +0x1b2
github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy/fastcgi.Transport.buildEnv(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xdd180b,
0x10, ...)
        /Users/matt/Dev/pkg/mod/github.com/caddyserver/caddy/v2@v2.0.0-beta9/modules/caddyhttp/reverseproxy/fastcgi/fastcgi.go:176 +0x196b
github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy/fastcgi.Transport.RoundTrip(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xdd180b, 0x10, ...)
        /Users/matt/Dev/pkg/mod/github.com/caddyserver/caddy/v2@v2.0.0-beta9/modules/caddyhttp/reverseproxy/fastcgi/fastcgi.go:91 +0x7c
github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy.(*Handler).reverseProxy(0xc000276990, 0x7efbfd2300e8, 0xc0002ced80, 0xc0000f1c00, 0xc0001dfdc0, 0xc000274ae0, 0x4, 0xc000274ae5, 0x18, 0x0, ...)
        /Users/matt/Dev/pkg/mod/github.com/caddyserver/caddy/v2@v2.0.0-beta9/modules/caddyhttp/reverseproxy/reverseproxy.go:391 +0x1f0
github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy.(*Handler).ServeHTTP(0xc000276990, 0x7efbfd2300e8, 0xc0002ced80, 0xc0000f1b00, 0xf06260, 0xe05a48, 0x0, 0x0)
        /Users/matt/Dev/pkg/mod/github.com/caddyserver/caddy/v2@v2.0.0-beta9/modules/caddyhttp/reverseproxy/reverseproxy.go:288 +0xaf4
github.com/caddyserver/caddy/v2/modules/caddyhttp.wrapMiddleware.func1.1(0x7efbfd2300e8, 0xc0002ced80, 0xc0000f1b00, 0x0, 0x0)
        /Users/matt/Dev/pkg/mod/github.com/caddyserver/caddy/v2@v2.0.0-beta9/modules/caddyhttp/routes.go:151 +0x62
github.com/caddyserver/caddy/v2/modules/caddyhttp.HandlerFunc.ServeHTTP(0xc00023fd80, 0x7efbfd2300e8, 0xc0002ced80, 0xc0000f1b00, 0xf06260,
0xc00023fd80)
        /Users/matt/Dev/pkg/mod/github.com/caddyserver/caddy/v2@v2.0.0-beta9/modules/caddyhttp/caddyhttp.go:532 +0x44
github.com/caddyserver/caddy/v2/modules/caddyhttp.(*Subroute).ServeHTTP(0xc0002813a0, 0x7efbfd2300e8, 0xc0002ced80, 0xc0000f1b00, 0xf06260,
0xe05a48, 0xffffffffffffffff, 0x0)
        /Users/matt/Dev/pkg/mod/github.com/caddyserver/caddy/v2@v2.0.0-beta9/modules/caddyhttp/subroute.go:71 +0x80
github.com/caddyserver/caddy/v2/modules/caddyhttp.wrapMiddleware.func1.1(0x7efbfd2300e8, 0xc0002ced80, 0xc0000f1b00, 0x152c4f0, 0xc0002da120)
        /Users/matt/Dev/pkg/mod/github.com/caddyserver/caddy/v2@v2.0.0-beta9/modules/caddyhttp/routes.go:151 +0x62
github.com/caddyserver/caddy/v2/modules/caddyhttp.HandlerFunc.ServeHTTP(0xc00023fd20, 0x7efbfd2300e8, 0xc0002ced80, 0xc0000f1b00, 0xc00026ace8, 0xc000134ba0)
        /Users/matt/Dev/pkg/mod/github.com/caddyserver/caddy/v2@v2.0.0-beta9/modules/caddyhttp/caddyhttp.go:532 +0x44
github.com/caddyserver/caddy/v2/modules/caddyhttp.VarsMiddleware.ServeHTTP(0xc0002b0b40, 0x7efbfd2300e8, 0xc0002ced80, 0xc0000f1b00, 0xf06260, 0xc00023fd20, 0x2, 0x0)
        /Users/matt/Dev/pkg/mod/github.com/caddyserver/caddy/v2@v2.0.0-beta9/modules/caddyhttp/vars.go:48 +0x2c0
github.com/caddyserver/caddy/v2/modules/caddyhttp.wrapMiddleware.func1.1(0x7efbfd2300e8, 0xc0002ced80, 0xc0000f1b00, 0x0, 0x0)
        /Users/matt/Dev/pkg/mod/github.com/caddyserver/caddy/v2@v2.0.0-beta9/modules/caddyhttp/routes.go:151 +0x62
github.com/caddyserver/caddy/v2/modules/caddyhttp.HandlerFunc.ServeHTTP(0xc00023fd40, 0x7efbfd2300e8, 0xc0002ced80, 0xc0000f1b00, 0xf06260,
0xc00023fd40)
        /Users/matt/Dev/pkg/mod/github.com/caddyserver/caddy/v2@v2.0.0-beta9/modules/caddyhttp/caddyhttp.go:532 +0x44
github.com/caddyserver/caddy/v2/modules/caddyhttp.(*Subroute).ServeHTTP(0xc000280fa0, 0x7efbfd2300e8, 0xc0002ced80, 0xc0000f1b00, 0xf06260,
0xe05a48, 0xc000375420, 0x0)
        /Users/matt/Dev/pkg/mod/github.com/caddyserver/caddy/v2@v2.0.0-beta9/modules/caddyhttp/subroute.go:71 +0x80
github.com/caddyserver/caddy/v2/modules/caddyhttp.wrapMiddleware.func1.1(0x7efbfd2300e8, 0xc0002ced80, 0xc0000f1b00, 0x0, 0x8)
        /Users/matt/Dev/pkg/mod/github.com/caddyserver/caddy/v2@v2.0.0-beta9/modules/caddyhttp/routes.go:151 +0x62
github.com/caddyserver/caddy/v2/modules/caddyhttp.HandlerFunc.ServeHTTP(0xc00023fc80, 0x7efbfd2300e8, 0xc0002ced80, 0xc0000f1b00, 0x203000,
0x203000)
        /Users/matt/Dev/pkg/mod/github.com/caddyserver/caddy/v2@v2.0.0-beta9/modules/caddyhttp/caddyhttp.go:532 +0x44
github.com/caddyserver/caddy/v2/modules/caddyhttp.(*Server).enforcementHandler(0xc00029e4e0, 0x7efbfd2300e8, 0xc0002ced80, 0xc0000f1b00, 0xf06260, 0xc00023fc80, 0x40caf8, 0x20)
        /Users/matt/Dev/pkg/mod/github.com/caddyserver/caddy/v2@v2.0.0-beta9/modules/caddyhttp/server.go:237 +0x9b
github.com/caddyserver/caddy/v2/modules/caddyhttp.(*Server).wrapPrimaryRoute.func1(0x7efbfd2300e8, 0xc0002ced80, 0xc0000f1b00, 0xd12fa0, 0xc00023fca0)
        /Users/matt/Dev/pkg/mod/github.com/caddyserver/caddy/v2@v2.0.0-beta9/modules/caddyhttp/server.go:213 +0x5a
github.com/caddyserver/caddy/v2/modules/caddyhttp.HandlerFunc.ServeHTTP(0xc00023fca0, 0x7efbfd2300e8, 0xc0002ced80, 0xc0000f1b00, 0xc00023fca0, 0xc00023fc80)
        /Users/matt/Dev/pkg/mod/github.com/caddyserver/caddy/v2@v2.0.0-beta9/modules/caddyhttp/caddyhttp.go:532 +0x44
github.com/caddyserver/caddy/v2/modules/caddyhttp.(*Server).executeCompositeRoute(0xc00029e4e0, 0x7efbfd2300e8, 0xc0002ced80, 0xc0000f1b00,
0xc0002d8000, 0x1, 0x4, 0xc00027e690, 0x0)
        /Users/matt/Dev/pkg/mod/github.com/caddyserver/caddy/v2@v2.0.0-beta9/modules/caddyhttp/server.go:198 +0xff
github.com/caddyserver/caddy/v2/modules/caddyhttp.(*Server).ServeHTTP(0xc00029e4e0, 0x7efbfd2300e8, 0xc0002ced80, 0xc0000f1b00)
        /Users/matt/Dev/pkg/mod/github.com/caddyserver/caddy/v2@v2.0.0-beta9/modules/caddyhttp/server.go:135 +0x73f
net/http.serverHandler.ServeHTTP(0xc00016a380, 0xf159e0, 0xc00001c460, 0xc0000f1900)
        /usr/local/go/src/net/http/server.go:2802 +0xa4
net/http.(*conn).serve(0xc000131220, 0xf186a0, 0xc0002cec40)
        /usr/local/go/src/net/http/server.go:1890 +0x875
created by net/http.(*Server).Serve
        /usr/local/go/src/net/http/server.go:2927 +0x38e

Git commit 9fe54e1c6025a45cdd531c093bb94d193a8b91a0

When compiling from source and trying again, I found that Caddy can’t connect to the PHP socket (it’s still there, and has permissions of 666).

http://www.example.localhost/blog:

2019/11/18 10:45:13.535 ERROR http.log.error dialing backend: dial unix /run/php/php7.3-fpm.sock:0: connect: no such file or directory {“request”: {“method”: “GET”, “uri”: “/blog”, “proto”: “HTTP/1.1”, “remote_addr”: “[::1]:43150”, “host”: “www.example.localhost”, “headers”: {“Accept-Language”: [“en-GB,en;q=0.5”], “Accept-Encoding”: [“gzip, deflate”], “Upgrade-Insecure-Requests”: [“1”], “Pragma”: [“no-cache”], “Cache-Control”: [“no-cache”], “User-Agent”: [“Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:70.0) Gecko/20100101 Firefox/70.0”], “Accept”: [“text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8”], “Dnt”: [“1”], “Connection”: [“keep-alive”]}}, “status”: 502, “err_id”: “5hg5xcs0c”, “err_trace”: “reverseproxy.(*Handler).ServeHTTP (reverseproxy.go:330)”}

A different error when requesting /?p=1, which repeated several times for one request:

http://www.example.localhost/?p=1:

Error
2019/11/18 11:40:58 http: panic serving [::1]:43056: runtime error: slice bounds out of range [:3] with length 1                                                                                                                                                                          
goroutine 22 [running]:                                                                                                                                                                                                                                                                   
net/http.(*conn).serve.func1(0xc0000f30e0)                                                                                                                                                                                                                                                
        /home/david/.asdf/installs/golang/1.13.4/go/src/net/http/server.go:1767 +0x139                                                                                                                                                                                                    
panic(0xda8ca0, 0xc0002dc0c0)                                                                                                                                                                                                                                                             
        /home/david/.asdf/installs/golang/1.13.4/go/src/runtime/panic.go:679 +0x1b2                                                                                                                                                                                                       
github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy/fastcgi.Transport.buildEnv(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xe24ecf, 0x10, ...)                                                                                                                                    
        /home/david/go/caddy/modules/caddyhttp/reverseproxy/fastcgi/fastcgi.go:176 +0x196b                                                                                                                                                                                                
github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy/fastcgi.Transport.RoundTrip(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xe24ecf, 0x10, ...)                                                                                                                                   
        /home/david/go/caddy/modules/caddyhttp/reverseproxy/fastcgi/fastcgi.go:91 +0x7c                                                                                                                                                                                                   
github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy.(*Handler).reverseProxy(0xc00020aaa0, 0xf74260, 0xc0002ec000, 0xc0002da300, 0xc000262600, 0xc0002210a0, 0x4, 0xc0002dc060, 0x1a, 0xc0002210a5, ...)                                                                        
        /home/david/go/caddy/modules/caddyhttp/reverseproxy/reverseproxy.go:409 +0x1f0                                                                                                                                                                                                    
github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy.(*Handler).ServeHTTP(0xc00020aaa0, 0xf74260, 0xc0002ec000, 0xc0002da200, 0xf640c0, 0xe5ae98, 0x0, 0x0)                                                                                                                     
        /home/david/go/caddy/modules/caddyhttp/reverseproxy/reverseproxy.go:314 +0xb30                                                                                                                                                                                                    
github.com/caddyserver/caddy/v2/modules/caddyhttp.wrapMiddleware.func1.1(0xf74260, 0xc0002ec000, 0xc0002da200, 0x0, 0x0)                                                                                                                                                                  
        /home/david/go/caddy/modules/caddyhttp/routes.go:151 +0x62                                                                                                                                                                                                                        
github.com/caddyserver/caddy/v2/modules/caddyhttp.HandlerFunc.ServeHTTP(0xc0002f42c0, 0xf74260, 0xc0002ec000, 0xc0002da200, 0xf640c0, 0xc0002f42c0)                                                                                                                                       
        /home/david/go/caddy/modules/caddyhttp/caddyhttp.go:539 +0x44                                                                                                                                                                                                                     
github.com/caddyserver/caddy/v2/modules/caddyhttp.(*Subroute).ServeHTTP(0xc0002593e0, 0xf74260, 0xc0002ec000, 0xc0002da200, 0xf640c0, 0xe5ae98, 0xffffffffffffffff, 0x0)                                                                                                                  
        /home/david/go/caddy/modules/caddyhttp/subroute.go:71 +0x80                                                                                                                                                                                                                       
github.com/caddyserver/caddy/v2/modules/caddyhttp.wrapMiddleware.func1.1(0xf74260, 0xc0002ec000, 0xc0002da200, 0x15c2cb0, 0xc00025fb00)                                                                                                                                                   
        /home/david/go/caddy/modules/caddyhttp/routes.go:151 +0x62                                                                                                                                                                                                                        
github.com/caddyserver/caddy/v2/modules/caddyhttp.HandlerFunc.ServeHTTP(0xc0002f4260, 0xf74260, 0xc0002ec000, 0xc0002da200, 0xc0002c81a8, 0xc00003ac60)                                                                                                                                   
        /home/david/go/caddy/modules/caddyhttp/caddyhttp.go:539 +0x44                                                                                                                                                                                                                     
github.com/caddyserver/caddy/v2/modules/caddyhttp.VarsMiddleware.ServeHTTP(0xc0002650b0, 0xf74260, 0xc0002ec000, 0xc0002da200, 0xf640c0, 0xc0002f4260, 0x2, 0x0)                                                                                                                          
        /home/david/go/caddy/modules/caddyhttp/vars.go:48 +0x2c0                                                                                                                                                                                                                          
github.com/caddyserver/caddy/v2/modules/caddyhttp.wrapMiddleware.func1.1(0xf74260, 0xc0002ec000, 0xc0002da200, 0x0, 0x0)                                                                                                                                                                  
        /home/david/go/caddy/modules/caddyhttp/routes.go:151 +0x62                                                                                                                                                                                                                        
github.com/caddyserver/caddy/v2/modules/caddyhttp.HandlerFunc.ServeHTTP(0xc0002f4280, 0xf74260, 0xc0002ec000, 0xc0002da200, 0xf640c0, 0xc0002f4280)                                                                                                                                       
        /home/david/go/caddy/modules/caddyhttp/caddyhttp.go:539 +0x44                                                                                                                                                                                                                     
github.com/caddyserver/caddy/v2/modules/caddyhttp.(*Subroute).ServeHTTP(0xc000258fe0, 0xf74260, 0xc0002ec000, 0xc0002da200, 0xf640c0, 0xe5ae98, 0xc0002d1420, 0x0)                                                                                                                        
        /home/david/go/caddy/modules/caddyhttp/subroute.go:71 +0x80                                                                                                                                                                                                                       
github.com/caddyserver/caddy/v2/modules/caddyhttp.wrapMiddleware.func1.1(0xf74260, 0xc0002ec000, 0xc0002da200, 0x0, 0x8)                                                                                                                                                                  
        /home/david/go/caddy/modules/caddyhttp/routes.go:151 +0x62                                                                                                                                                                                                                        
github.com/caddyserver/caddy/v2/modules/caddyhttp.HandlerFunc.ServeHTTP(0xc0002f41e0, 0xf74260, 0xc0002ec000, 0xc0002da200, 0x203000, 0x203000)                                                                                                                                           
        /home/david/go/caddy/modules/caddyhttp/caddyhttp.go:539 +0x44                                                                                                                                                                                                                     
github.com/caddyserver/caddy/v2/modules/caddyhttp.(*Server).enforcementHandler(0xc0002665b0, 0xf74260, 0xc0002ec000, 0xc0002da200, 0xf640c0, 0xc0002f41e0, 0x40e568, 0x20)                                                                                                                
        /home/david/go/caddy/modules/caddyhttp/server.go:238 +0x9b                                                                                                                                                                                                                        
github.com/caddyserver/caddy/v2/modules/caddyhttp.(*Server).wrapPrimaryRoute.func1(0xf74260, 0xc0002ec000, 0xc0002da200, 0xd5f0e0, 0xc0002f4200)                                                                                                                                          
        /home/david/go/caddy/modules/caddyhttp/server.go:214 +0x5a                                                                                                                                                                                                                        
github.com/caddyserver/caddy/v2/modules/caddyhttp.HandlerFunc.ServeHTTP(0xc0002f4200, 0xf74260, 0xc0002ec000, 0xc0002da200, 0xc0002f4200, 0xc0002f41e0)                                                                                                                                   
        /home/david/go/caddy/modules/caddyhttp/caddyhttp.go:539 +0x44                                                                                                                                                                                                                     
github.com/caddyserver/caddy/v2/modules/caddyhttp.(*Server).executeCompositeRoute(0xc0002665b0, 0xf74260, 0xc0002ec000, 0xc0002da200, 0xc00000c1e0, 0x1, 0x4, 0xc0002ba240, 0xc0002d42e8)                                                                                                 
        /home/david/go/caddy/modules/caddyhttp/server.go:199 +0xff                                                                                                                                                                                                                        
github.com/caddyserver/caddy/v2/modules/caddyhttp.(*Server).ServeHTTP(0xc0002665b0, 0xf74260, 0xc0002ec000, 0xc0002da200)                                                                                                                                                                 
        /home/david/go/caddy/modules/caddyhttp/server.go:136 +0x73d                                                                                                                                                                                                                       
net/http.serverHandler.ServeHTTP(0xc000178380, 0xf74260, 0xc0002ec000, 0xc0002da000)
        /home/david/.asdf/installs/golang/1.13.4/go/src/net/http/server.go:2802 +0xa4
net/http.(*conn).serve(0xc0000f30e0, 0xf77120, 0xc0002bc000)
        /home/david/.asdf/installs/golang/1.13.4/go/src/net/http/server.go:1890 +0x875
created by net/http.(*Server).Serve
        /home/david/.asdf/installs/golang/1.13.4/go/src/net/http/server.go:2927 +0x38e

Trying the approach Matt shows here:

matcher notStatic {
	not {
		file {
			try_files {path}
		}
	}
}
try_files /index.php?p={path}&{query}
reverse_proxy match:notStatic {
	to unix//run/php/php7.3-fpm.sock
	transport fastcgi {
		split .php
	}
}
file_server

/blog is 404.

Trying a rewrite:

matcher notStatic {
	not {
		file {
			try_files {path}
		}
	}
}

rewrite match:notStatic /index.php?p={path}&{query}
reverse_proxy match:notStatic {
	to unix//run/php/php7.3-fpm.sock
	transport fastcgi {
		split .php
	}
}
file_server

This sends unprocessed PHP code with a status response of 200:

<?php
/**
 * Craft web bootstrap file
 */

// Set path constants
define('CRAFT_BASE_PATH', dirname(__DIR__));
define('CRAFT_VENDOR_PATH', CRAFT_BASE_PATH.'/vendor');

// Load Composer's autoloader
require_once CRAFT_VENDOR_PATH.'/autoload.php';

// Load dotenv?
if (class_exists('Dotenv\Dotenv') && file_exists(CRAFT_BASE_PATH.'/.env')) {
    (new Dotenv\Dotenv(CRAFT_BASE_PATH))->load();
}

// Load and run Craft
define('CRAFT_ENVIRONMENT', getenv('ENVIRONMENT') ?: 'production');
$app = require CRAFT_VENDOR_PATH.'/craftcms/cms/bootstrap/web.php';
$app->run();

Just curious, how does this work:

Because to the file system, the last two options (/index.php?p={path}&{query} and /index.php?{query}) are indistinguishable - they both go to index.php.

Yes, I since gathered that and so removed the last option.

Ah, okay - thanks for clarifying.

I just realized the v2 try_files doesn’t split a path’s query string off before checking for file existence. In other words, right now, it doesn’t support having query strings (it assumes the entire token text is a filename), but I guess we need those, huh.

2 Likes

@BrightEyesD Before we forget, would you mind filing an issue to request that try_files be smart enough to elide the query string?

Done. :slight_smile:

FYI, this has been implemented in try_files, rewrite: allow query string in try_files (fix #2891) · caddyserver/caddy@5e9d81b · GitHub

Just to update here in addition to the Github issue, Matt has worked out some other aspects of URL rewriting in relation to serving serving static assets and reverse proxying (e.g., to PHP).

In v2 now, the following alone may be enough to have *.php files processed by PHP and other, non- *.php files served directly:

https://example.com
root * /path/to/public_html
php_fastcgi unix//run/php/php7.3-fpm.sock
file_server

If the PHP software requires particular URL rewrites, something like the following (which is for Craft CMS) can be added:

try_files {path} /index.php?{query}&p={path}
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.