Webtrees caddyfile, converting from v1 to v2

I’m running Caddy 2.2.0 on Arch Linux with systemd. I am happy to have successfully converted my config for Processwire CMS and KiwiBNC IRC bouncer. I have a teensy bit of trouble with my Webtrees config.

I need to serve assets residing in the /public directory, of the form /public/css/vendor.min.css?v=2.0.4
It is obvious my rewrite for /public is not working. Help appreciated!

Caddyfile v1:

webtrees.mysite.fi {
    gzip
    root /var/www/mysite/webtrees

    fastcgi /index.php unix:/var/run/php-fpm/php-fpm.sock php {
        connect_timeout 10000s
        read_timeout 10000s
        send_timeout 10000s
    }

    internal /forbidden

    rewrite {
        r ^/data
        to /forbidden
    }
    rewrite {
        r ^/.git
        to /forbidden
    }
    rewrite {
        r ^/app
        to /forbidden
    }
    rewrite {
        r ^/modules
        to /forbidden
    }
    rewrite {
        r ^/resources
        to /forbidden
    }
    rewrite {
        r ^/vendor
        to /forbidden
    }
    rewrite {
        r ^/public
        to {path}
    }   
    #pretty urls
    rewrite {
       r ^
       to /index.php
    }
}

Caddyfile v2:

webtrees.mysite.fi {
    encode gzip
    root * /var/www/mysite/webtrees
    file_server

    php_fastcgi /index.php unix//var/run/php-fpm/php-fpm.sock {
        health_timeout 10000s
    }

    @deny path_regexp ^/(.git|data|app|modules|resources|vendor)
    @prettyurls path_regexp ^
    @public path_regexp ^/public

    rewrite @deny /denyaccess
    rewrite @prettyurls /index.php
    rewrite @public {path}
}

You don’t need to do anything extra for “pretty URLs” to work in v2. You can remove the @prettyurls and @public matchers and rewrites (they don’t do anything), and remove the /index.php matcher from your php_fastcgi directive. The php_fastcgi directive has its own try_files logic built-in that checks if a file exists on disk for the path requested, and if not, falls back to index.php.

webtrees.mysite.fi {
    encode gzip
    root * /var/www/mysite/webtrees
    file_server

    php_fastcgi unix//var/run/php-fpm/php-fpm.sock

    @deny path_regexp ^/(.git|data|app|modules|resources|vendor)
    rewrite @deny /denyaccess
}
2 Likes

Sweeeeet! Works perfectly. Thanks a lot!

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.