Ampache on Caddy (nginx config convert)


(Chris Koster) #1

Hi,

I’m trying to run Ampache (https://github.com/ampache/ampache/) on Caddy but there’s quite an extensive config file with a lot of rewrite rules to be set.

Somebody willing to help me get this converted to Caddy?

i’ve got some stuff which I know how to do. I’m stuck at the rewrite rules, the proxy rules (alhough i’ll mighty manage to do that myself) and the fastcgi parameters. The installation and nginx instructions can be found here: https://github.com/ampache/ampache/wiki/Installation

This is what I have so far:

ampache.myserver.nl.nl {
root /home/user/web/ampache.myserver.nl

log /home/user/web/log/ampache.myserver.nl.access.log {
    rotate_size 100  # Rotate after 100 MB
    rotate_age  14   # Keep log files for 14 days
    rotate_keep 10   # keep maximum of 10 lof files
}

errors /home/user/web/log/ampache.myserver.nl.error.log

index index.php

gzip

fastcgi / unix:/var/run/php/php7.0-fpm.sock php
}

Any help is much appreciated and I will open a PR on github to get the config in their wiki for other users.


(Matthew Fay) #2

Wow - this app needs a lot of support from its web server in order to function properly, it seems.

You don’t need to set index index.php, since fastcgi with the php preset will handle those index requests.


The rewrites should copy across mostly as-is, except they have to take the format:

rewrite [basepath] {
  r [regex]
  to [dest]
}

Use the base path to help reduce unnecessary regex processing. Here it acts as a quick substring check telling Caddy: If the path doesn’t even start with [basepath], don’t bother running the regex. Otherwise we’d be running every regex we have on every request, which is a lot of unnecessary overhead.

In [dest], you must use {1} instead of $1, {2} instead of $2, etc.

To demonstrate, this rewrite from the nginx example looks complicated, because it’s two separate rewrites - both under a non-directory check:

    if ( !-d $request_filename ) {
        rewrite ^/rest/(.*).view$ /rest/index.php?action=$1 last;
        rewrite ^/rest/fake/(.+)$ /play/$1 last;
    }

In Caddy, we’d write that like this:

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

The {path}/ part means we first check if it’s a directory, and if it is, we don’t rewrite it to the next option - effectively a !-d check.

For one of the Beautiful URL Rewriting examples:

rewrite ^/play/ssid/(\w+)/type/(\w+)/oid/([0-9]+)/uid/([0-9]+)/name/(.*)$ /play/index.php?ssid=$1&type=$2&oid=$3&uid=$4&name=$5 last;

In Caddy:

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

Rinse and repeat for the others.


For PHP, your current fastcgi string looks great.

I’m inclined to believe that none of the other params from the nginx config are necessary. Give that a shot on its own, if that doesn’t work, we can take a closer look at what parameters might be required.


For that websocket, use a proxy.

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

(Chris Koster) #3

Wow thanks @Whitestrake! Much appreciated and just what I needed. I will work on the config this weekend and report back here if I got everything up and running (or not :p).


(Matthew Fay) #4

No worries! It’s going to be a bit of effort to translate it all - but once you know the formats, all the regex is standard fare, so it’s just a matter of slogging through it. Post your end result Caddyfile here and I’d be happy to look over it.

Oh, and some relevant Caddy docs, for reference:

https://caddyserver.com/docs/rewrite
https://caddyserver.com/docs/fastcgi
https://caddyserver.com/docs/proxy


(Chris Koster) #5

Well, thanks again! Got the application running. I’ve skipped the limits on certain paths (might look into them later to secure the application a bit more) and also skipped the cgi params as the seem not te be necessary.

This is the config i’m using now:

ampache.mysite.nl {
root /home/user/web/ampache.mysite.nl

log /home/user/web/log/ampache.mysite.nl.access.log {
    rotate_size 100  # Rotate after 100 MB
    rotate_age  14   # Keep log files for 14 days
    rotate_keep 10   # keep maximum of 10 lof files
}

errors /home/user/web/log/ampache.mysite.nl.error.log

index index.php

gzip

fastcgi / unix:/var/run/php/php7.0-fpm.sock php

# 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
}

}

Will start using the application and configuration for a week or so, i’f I don’t ecnounter any problems i’ll add the config to the Ampache Wiki.


(Matt Holt) #6

Would you also add it to the Caddy examples repo? :smiley:


(Chris Koster) #7

Yup, will add it later this week if i know for sure the whole config is working as supposed to.


(system) #8

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