1. The problem I’m having:
Hello.
I tried to solve my issue for hours without success and I searched the web a lot not finding any working solution for me.
I use devenv for development and devenv uses Caddy as a webserver.
My project has the following folder structure:
my-app/
├─ admin/
│ ├─ index.php
├─ index.php
I need all frontend requests that are no static resources like CSS, JS or images to be rewritten to index.php
Example: https://localhost:8000/account/orders
hits my-app/index.php
All requests to the administration should be rewritten to my-app/admin/index.php
Example: https://localhost:8000/admin/configuration
hits my-app/admin/index.php
I tried a couple of configurations and while requests to http://localhost:8000/admin
do hit admin/index.php requests like http://localhost:8000/admin/configuration
hit /index.php and cause a 404 error in the application frontend router.
2. Error messages and/or full log output:
currently, the setup does not log anything. I will add logging if you show me how.
3. Caddy version:
2.7.6
4. How I installed and ran Caddy:
a. System environment:
Ubuntu 24.04 with Nix and devenv
b. Command:
#!/nix/store/a1s263pmsci9zykm5xcdf7x9rv26w6d5-bash-5.2p26/bin/bash
XDG_DATA_HOME=/media/juergen/ssd-ext/devenv/my-app/.devenv/state/caddy/data XDG_CONFIG_HOME=/media/juergen/ssd-ext/devenv/my-app/.devenv/state/caddy/config /nix/store/7ad29pl6dsmpjkphkv4q7n3pp63g3v7w-caddy-2.7.6/bin/caddy run --config /nix/store/p5jk90mkvbm3ykh7f7gai7l6wnlxmkcm-caddy-config.json
c. Service/unit/compose file:
sorry devenv starts caddy for me, not sure what info you need here.
d. My complete Caddy config:
my config section of caddy in my devenv.nix
:8000 {
encode zstd gzip
root * shop
file_server
handle /admin/* {
php_fastcgi unix//media/juergen/ssd-ext/devenv/my-app/.devenv/state/php-fpm/web.sock {
trusted_proxies private_ranges
}
}
@default {
not path /admin/* /theme/* /media/* /thumbnail/* /bundles/* /css/* /fonts/* /js/* /sitemap/*
}
php_fastcgi @default unix//media/juergen/ssd-ext/devenv/my-app/.devenv/state/php-fpm/web.sock {
trusted_proxies private_ranges
}
}
devenv generated config file:
{
"apps": {
"http": {
"servers": {
"srv0": {
"listen": [
":8000"
],
"routes": [
{
"handle": [
{
"handler": "vars",
"root": "shop"
},
{
"encodings": {
"gzip": {},
"zstd": {}
},
"handler": "encode",
"prefer": [
"zstd",
"gzip"
]
}
]
},
{
"match": [
{
"path": [
"/admin/*"
]
}
],
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"handler": "static_response",
"headers": {
"Location": [
"{http.request.orig_uri.path}/"
]
},
"status_code": 308
}
],
"match": [
{
"file": {
"try_files": [
"{http.request.uri.path}/index.php"
]
},
"not": [
{
"path": [
"*/"
]
}
]
}
]
},
{
"handle": [
{
"handler": "rewrite",
"uri": "{http.matchers.file.relative}"
}
],
"match": [
{
"file": {
"split_path": [
".php"
],
"try_files": [
"{http.request.uri.path}",
"{http.request.uri.path}/index.php",
"index.php"
]
}
}
]
},
{
"handle": [
{
"handler": "reverse_proxy",
"transport": {
"protocol": "fastcgi",
"split_path": [
".php"
]
},
"trusted_proxies": [
"192.168.0.0/16",
"172.16.0.0/12",
"10.0.0.0/8",
"127.0.0.1/8",
"fd00::/8",
"::1"
],
"upstreams": [
{
"dial": "unix//media/juergen/ssd-ext/devenv/my-app/.devenv/state/php-fpm/web.sock"
}
]
}
],
"match": [
{
"path": [
"*.php"
]
}
]
}
]
}
]
},
{
"match": [
{
"not": [
{
"path": [
"/admin/*",
"/theme/*",
"/media/*",
"/thumbnail/*",
"/bundles/*",
"/css/*",
"/fonts/*",
"/js/*",
"/sitemap/*"
]
}
]
}
],
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"handler": "static_response",
"headers": {
"Location": [
"{http.request.orig_uri.path}/"
]
},
"status_code": 308
}
],
"match": [
{
"file": {
"try_files": [
"{http.request.uri.path}/index.php"
]
},
"not": [
{
"path": [
"*/"
]
}
]
}
]
},
{
"handle": [
{
"handler": "rewrite",
"uri": "{http.matchers.file.relative}"
}
],
"match": [
{
"file": {
"split_path": [
".php"
],
"try_files": [
"{http.request.uri.path}",
"{http.request.uri.path}/index.php",
"index.php"
]
}
}
]
},
{
"handle": [
{
"handler": "reverse_proxy",
"transport": {
"protocol": "fastcgi",
"split_path": [
".php"
]
},
"trusted_proxies": [
"192.168.0.0/16",
"172.16.0.0/12",
"10.0.0.0/8",
"127.0.0.1/8",
"fd00::/8",
"::1"
],
"upstreams": [
{
"dial": "unix//media/juergen/ssd-ext/devenv/my-app/.devenv/state/php-fpm/web.sock"
}
]
}
],
"match": [
{
"path": [
"*.php"
]
}
]
}
]
}
]
},
{
"handle": [
{
"handler": "file_server",
"hide": [
"/nix/store/c40lz32kx8v9wnf75r0wyl6hpcxjpkjv-formattedCaddyFile"
]
}
]
}
]
}
}
},
"tls": {
"automation": {
"policies": [
{
"issuers": [
{
"ca": "https://acme-v02.api.letsencrypt.org/directory",
"email": "",
"module": "acme"
}
]
}
]
}
}
}
}