Motioneye behind Caddy v2

1. Caddy version (caddy version):

v2.4.5

2. How I run Caddy:

I run Caddy v2 as a jail in truenas 12.

a. System environment:

I run Caddy as a jail in truenas.
Jail version: 12.2-release-p6

b. Command:

service caddy start

c. Service/unit/compose file:

#!/bin/sh

# PROVIDE: caddy
# REQUIRE: LOGIN DAEMON NETWORKING
# KEYWORD: shutdown

# To enable caddy, add 'caddy_enable="YES"' to /etc/rc.conf or
# /etc/rc.conf.local

# Optional settings:
# caddy_config (string):      Full path to caddy config file
#                             (/usr/local/etc/caddy/Caddyfile)
# caddy_adapter (string):     Config adapter type (caddyfile)
# caddy_directory (string):   Root for caddy storage (ACME certs, etc.)
#                             (/var/db/caddy)
# caddy_extra_flags (string): Extra flags passed to caddy start
# caddy_logdir (string):      Where caddy logs are stored
#                             (/var/log/caddy)
# caddy_logfile (string):     Location of process log (${caddy_logdir}/caddy.log)
#                             This is for startup/shutdown/error messages.
#                             To create an access log, see:
#                             https://caddyserver.com/docs/caddyfile/directives/log
# caddy_user (user):          User to run caddy (root)
# caddy_group (group):        Group to run caddy (wheel)
#
# This script will honor XDG_CONFIG_HOME/XDG_DATA_HOME. Caddy will create a
# .../caddy subdir in each of those. By default, they are subdirs of /var/db/caddy.
# See https://caddyserver.com/docs/conventions#data-directory

. /etc/rc.subr

name=caddy
rcvar=caddy_enable
desc="Powerful, enterprise-ready, open source web server with automatic HTTPS written in Go"

load_rc_config $name

# Defaults
: ${caddy_enable:=NO}
: ${caddy_adapter:=caddyfile}
: ${caddy_config:=/usr/local/etc/caddy/Caddyfile}
: ${caddy_directory:=/var/db/caddy}
: ${caddy_extra_flags:=""}
: ${caddy_logdir:="/var/log/${name}"}
: ${caddy_logfile:="${caddy_logdir}/${name}.log"}
: ${caddy_user:="root"}
: ${caddy_group:="wheel"}

# Config and base directories
: ${XDG_CONFIG_HOME:="${caddy_directory}/config"}
: ${XDG_DATA_HOME:="${caddy_directory}/data"}
export XDG_CONFIG_HOME XDG_DATA_HOME

command="/usr/local/bin/${name}"
caddy_flags="--config ${caddy_config} --adapter ${caddy_adapter}"
pidfile="/var/run/${name}/${name}.pid"

required_files="${caddy_config} ${command}"

start_precmd="caddy_precmd"
start_cmd="caddy_start"
stop_cmd="caddy_stop"

# Extra Commands
extra_commands="configtest reload"
configtest_cmd="caddy_command validate ${caddy_flags}"
reload_cmd="caddy_command reload ${caddy_flags}"

caddy_command()
{
        /usr/bin/su -m "${caddy_user}" -c "${command} $*"
}

caddy_precmd()
{
        # Create required directories and set permissions
        /usr/bin/install -d -m 755 -o "${caddy_user}" -g "${caddy_group}" ${caddy_directory}
        /usr/bin/install -d -m 700 -o "${caddy_user}" -g "${caddy_group}" ${caddy_directory}/config
        /usr/bin/install -d -m 700 -o "${caddy_user}" -g "${caddy_group}" ${caddy_directory}/data
        /usr/bin/install -d -m 755 -o "${caddy_user}" -g "${caddy_group}" ${caddy_logdir}
        /usr/bin/install -d -m 700 -o "${caddy_user}" -g "${caddy_group}" /var/run/caddy
}

caddy_start()
{
        echo -n "Starting caddy... "
        /usr/bin/su -m ${caddy_user} -c "${command} start ${caddy_flags} \
                ${caddy_extra_flags} --pidfile ${pidfile}" >> ${caddy_logfile} 2>&1
        if [ $? -eq 0 ] && ps -ax -o pid | grep -q "$(cat ${pidfile})"; then
                echo "done"
                echo "Log: ${caddy_logfile}"
        else
                echo "Error: Caddy failed to start"
                echo "Check the caddy log: ${caddy_logfile}"
        fi
}

caddy_stop()
{
        echo -n "Stopping caddy... "
        if caddy_command stop; then
                echo "done"
        else
                echo "Error: Unable to stop caddy"
                echo "Check the caddy log: ${caddy_logfile}"
        fi
}

run_rc_command "$1"

d. My complete Caddyfile or JSON config:

mydomain.duckdns.org {

tls {
        dns duckdns <APIKEY>
    }

   encode gzip
   reverse_proxy  http://10.10.10.189:8765
}

3. The problem I’m having:

I can get Caddy v2 to work with Motioneye with a normal url. The solution found in topic #11845 works flawlessly.
But what I can’t get to work, is to change the base url. I would like to have my reverse proxy point to mydomain/cams for example.

4. Error messages and/or full log output:

caddy starts without errors.

5. What I already tried:

I’ve tried the config below, but all I get is a white page.

mydomain.duckdns.org {

tls {
dns duckdns
}
encode gzip
reverse_proxy /cams* http://10.10.10.189:8765
}

Before caddy, motioneye was running behind nginx. So I have a working nginx config:
http {
server {
listen 80;
server_name motioneye.home;
location /cams/ {
proxy_pass http://localhost:8765/;
proxy_read_timeout 120s;
}
}
}

6. Links to relevant resources:

You probably want to use handle_path, which is the closest equivalent to location in nginx – it involves stripping a path prefix from the request before proxying it:

handle_path /cams* {
	reverse_proxy http://10.10.10.189:8765
}
1 Like

Dear Francis,

Thank you for your reply :slight_smile:
This works! However there is a slight catch…
For some reason I have to type a “/” at the end of my url otherwise the webinterface only partially loads.
So I have to type: mydomain.duckdns.org/cams/
mydomain.duckdns.org/cams only partially works…

I’ve tried changing the config like so: handle_path /cams/* but the result is still the same
I’ve tried changing the config like so as well: redir /cams* /cams/* but the result is still the same

Is there a catch to not having to type a “/” at the end of your url?

Try this:

redir /cams /cams/
handle_path /cams/* {
	reverse_proxy http://10.10.10.189:8765
}

Path matching is exact, so you can make redir only happen for exactly /cams to redirect to /cams/.

2 Likes

Dear Francis,

Yes, it works :slight_smile:
Problem solved.

Thank you very much.

Kind regards

1 Like

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