1. Caddy version (caddy version
): 2.0.0
2. How I run Caddy: Dockerfile
with the Caddyfile
added with a docker-compose.yml
FROM caddy:2.0.0-builder AS builder
RUN caddy-builder \
github.com/hairyhenderson/caddy-teapot-module \
github.com/caddy-dns/cloudflare
FROM caddy:2.0.0
COPY --from=builder /usr/bin/caddy /usr/bin/caddy
a. System environment:
Linux Ubuntu 20.04.1 LTS, Dockerb. Command: Not really
c. Service/unit/compose file:
caddy:
build:
context: ./caddy
dockerfile: Dockerfile
restart: always
volumes:
- site:/srv/site
- ./.caddy_data:/data
- ./.caddy_config:/config
- ./caddy/etc/Caddyfile:/etc/caddy/Caddyfile
environment:
- ACME_AGREE=true
ports:
- "2015:2015"
- "80:80"
- "443:443"
env_file:
- ./conf/caddy.env
- ./conf/caddy_sensitive.env
d. My complete Caddyfile or JSON config:
{
default_sni {$SERVER_NAME}
}
{$SERVER_NAME} {
import /etc/{$TLS_MODE}
import /etc/basic_auth.conf
root * /srv/site/pub
encode zstd gzip
@blocked {
path /media/customer/* /media/downloadable/* /media/import/* /media/custom_options/* /errors/*
}
respond @blocked 403
@notfound {
path_regexp reg_notfound \/\..*$|\/errors\/.*\.xml$|theme_customization\/.*\.xml
}
respond @notfound 404
@static {
path_regexp reg_static ^/static/(version\d*/)?(.*)$
}
rewrite @static /static/{http.regexp.reg_static.2}
file_server
php_fastcgi fpm:9000
log {
output file /var/log/caddy.log
}
header /media X-Frame-Options "SAMEORIGIN"
header /static X-Frame-Options "SAMEORIGIN"
header / X-Content-Type-Options "nosniff"
}
3. The problem I’m having:
There is a @static
matcher that works perfectly in a production environment. In prod all files are compiled statically and if they are served statically that will be caught in this matcher and the following redirect.
So, I want to run the same server in developer mode, then all files are not compiled and instead these files will be server via a php file instead. When this application is hosted under Apache the .htaccess file for the static dir has following:
<IfModule mod_rewrite.c>
RewriteEngine On
# Remove signature of the static files that is used to overcome the browser cache
RewriteRule ^version.+?/(.+)$ $1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* ../static.php?resource=$0 [L]
# Detects if moxieplayer request with uri params and redirects to uri without params
<Files moxieplayer.swf>
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ %{REQUEST_URI}? [R=301,L]
</Files>
</IfModule>
I’ve been trying to figure out how to make the same logic.
- Static file is requested, filter out “version1234443” and grab the last group
- If regex group is a path that exists on disk, serve it
- Otherwise redirect to
static.php
with the file as a parameter
I’ve tried quite a few different options similar to:
@static {
path_regexp reg_static ^/static/(version\d*/)?(.*)$
file /static/{re.reg_static.2}
}
rewrite @static /static/{re.reg_static.2}
@dynamic {
path_regexp reg_dynamic ^/static/(version\d*/)?(.*)$
not file /static/{re.reg_dynamic.2}
}
rewrite @dynamic /static.php?resource={re.reg_dynamic.2}
4. Error messages and/or full log output:
The redirect comes back to the browser but WITHOUT the resource variable added. The redirect ends with ?resource=
and the redirect request ends up in a 404.
5. What I already tried:
I’ve tried several different options based on this subject with no luck so far.