Managing a lot of rewrites

1. The problem I’m having:

I want to run Ampache, and in their installation instructions, they showcase a Caddyfile config - however, this seems outdated, as caddy validate immediately complains about the r directive.

The config is here: Installation · ampache/ampache Wiki · GitHub

2. Error messages and/or full log output:

# caddy validate --config /srv/Caddyfile
2023/04/26 14:02:01.419 INFO    using provided configuration    {"config_file": "/srv/Caddyfile", "config_adapter": ""}
Error: adapting config using caddyfile: parsing caddyfile tokens for 'rewrite': /srv/Caddyfile:211 - Error during parsing: Wrong argument count or unexpected line ending after 'rewrite'

3. Caddy version:

# caddy version
v2.6.2 h1:wKoFIxpmOJLGl3QXoo6PNbYvGW4xLEgo32GPBEjWL8o=

4. How I installed and ran Caddy:

I installed via the Go package manager and update via xcaddy. It’s ran using dinit on OpenWrt - I haven’t gotten around to implementing a Caddy service for procd yet…

a. System environment:

OpenWrt 22.03 on FriendlyElec NanoPi R6s

b. Command:

# dinit unit file for Caddy
type = process
command = /usr/bin/caddy run --config /srv/Caddyfile
stop-command = /usr/bin/caddy stop
restart = true
smooth-recovery = true
restart-delay = 1
restart-limit-interval = 5

c. Service/unit/compose file:

See above.

d. My complete Caddy config:

I copied the example config and adjusted it a little to fit into my config.

# Global:
{
        debug true
        log {
                level info
                format console
                output file /var/log/caddy.log {
                        roll_size 10mb
                        roll_keep 5
                        roll_keep_for 168h
                }
        }
        storage redis {
                host 100.64.0.4
                key_prefix caddy_
        }
}

(php) {
        php_fastcgi unix//var/run/php8-fpm.sock {
                capture_stderr true
                trusted_proxies 100.64.0.0/24
        }
}

# The block I copied/edited:
music.birb.it {
        root * /sdcard/srv/ampache/public/
        import php
        encode gzip

        # Rewrite rules for Subsonic backend
        rewrite /rest {
                r ^/rest/(.*).view$
                to {path}/ /rest/index.php?action={1}
        }
        rewrite /rest/fake {
                r ^/rest/fake/(.+)$
                to {path}/ /play/{1}
        }

        # Rewrite rule for Channels
        rewrite /channel {
                r ^/channel/([0-9]+)/(.*)$
                to /channel/index.php?channel={1&}target={2} last;
        }

        # Beautiful URL Rewriting
        rewrite /play/ssid {
                r ^/play/ssid/(\w+)/type/(\w+)/oid/([0-9]+)/uid/([0-9]+)/name/(.*)$
                to /play/index.php?ssid={1}&type={2}&oid={3}&uid={4}&name={5}

                r ^/play/ssid/(\w+)/type/(\w+)/oid/([0-9]+)/uid/([0-9]+)/client/(.*)/noscrobble/([0-1])/name/(.*)$
                to /play/index.php?ssid={1}&type={2}&oid={3}&uid={4}&client={5}&noscrobble={6}&name={7}

                r ^/play/ssid/(.*)/type/(.*)/oid/([0-9]+)/uid/([0-9]+)/client/(.*)/noscrobble/([0-1])/player/(.*)/name/(.*)$
                to /play/index.php?ssid={1}&type={2}&oid={3}&uid={4}&client={5}&noscrobble={6}&player={7}&name={8}

                r ^/play/ssid/(.*)/type/(.*)/oid/([0-9]+)/uid/([0-9]+)/client/(.*)/noscrobble/([0-1])/transcode_to/(w+)/bitrate/([0-9]+)/player/(.*)/name/(.*)$
                to /play/index.php?ssid={1}&type={2}&oid={3}&uid={4}&client={5&}noscrobble={6}&transcode_to={7}&bitrate={8}&player={9}&name={10}

                r ^/play/ssid/(.*)/type/(.*)/oid/([0-9]+)/uid/([0-9]+)/action/(.*)/name/(.*)$
                to /play/index.php?ssid={1}&type={2}&oid={3}&uid={4}action={5}&name={6}
        }

        # the following line was needed for me to get downloads of single songs to work
        rewrite /play {
                r ^/play/art/([^/]+)/([^/]+)/([0-9]+)/thumb([0-9]*)\.([a-z]+)$
                to /image.php?object_type={2}&object_id={3}&auth={1}

                r ^/([^/]+)/([^/]+)(/.*)?$
                to /play/{3}?{1}={2}

                r ^/(/[^/]+|[^/]+/|/?)$
                to /play/index.php
        }

        proxy /ws 127.0.0.1:8100 {
                transparent
                websocket
                without /ws
        }
}

5. Links to relevant resources:

See above, the only reference is the Ampache installation guide.

You’re using a Caddy v1 config. Caddy v2 was a complete rewrite, that syntax doesn’t exist anymore. Please review the docs and the upgrade guide.

Make sure to use the latest version, v2.6.4.

I had a hunch that this might be the case.

I looked at the v2 rewrite directives but aside from named handlers, I couldn’t figure out a good way to migrate the r/to pattern. Do you have any idea?

And thanks, will update now!

You need to use named matchers with the path_regexp matcher, and apply that to the rewrite directive.

So, a named matcher for each path rewrite? Oh boy… That’ll take some time. x)

Will give it a try though! :slight_smile:

There are what, about 10? That should be manageable. When you finish, you should submit the updated config to Ampache so they can update their docs. :100:

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