Help migrating from v.1 to v. 2

Hi all,

after years not touching anything, I said that with your help I could finally jump on v2!

1. Caddy version (caddy version):

Version 1

2. How I run Caddy:

qpkg installed and runs on QNAP at start.

Ports 443 and 80 are forwarded by the router to the server.

Everything works fine under v. 1, but I think it would be best practice and more secure to migrate everything to v. 2.

Honestly, I do not need all the log function if it could create problems.

a. System environment:

QNAP

b. Command:

…/.qpkg/Caddy/caddy.sh

#!/bin/sh
CONF=/etc/config/qpkg.conf
QPKG_NAME="Caddy"
QPKG_ROOT=`/sbin/getcfg $QPKG_NAME Install_Path -f ${CONF}`
APACHE_ROOT=`/sbin/getcfg SHARE_DEF defWeb -d Qweb -f /etc/config/def_share.info`
export QNAP_QPKG=$QPKG_NAME

export QPKG_ROOT
export QPKG_NAME
export APACHE_ROOT

export SHELL=/bin/sh
export LC_ALL=en_US.UTF-8
export USER=admin
export LANG=en_US.UTF-8
export LC_CTYPE=en_US.UTF-8

export CADDYPATH=/share/homes/user/caddy/Caddypath

ulimit -n 8192

export PIDF=/var/run/caddy.pid

case "$1" in
  start)
    ENABLED=$(/sbin/getcfg $QPKG_NAME Enable -u -d FALSE -f $CONF)
    if [ "$ENABLED" != "TRUE" ]; then
        echo "$QPKG_NAME is disabled."
        exit 1
    fi
/bin/ln -sf $QPKG_ROOT /opt/$QPKG_NAME
/bin/ln -sf $QPKG_ROOT/caddy /usr/bin/caddy

caddy -conf /share/homes/user/caddy/caddy.conf -agree -email nas@server.com -root /home/Qhttpd -log /share/homes/user/caddy/caddy.log -pidfile /var/run/caddy.pid &

    ;;

  stop)

ID=$(more /var/run/caddy.pid)

        if [ -e $PIDF ]; then
            kill -9 $ID
            rm -f $PIDF
        fi


rm -rf /opt/$QPKG_NAME
rm -rf /usr/bin/caddy

    ;;

  restart)
    $0 stop
    $0 start
    ;;

  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
esac

exit 0

d. My complete Caddyfile or JSON config:

caddy.conf

(gzipconf) {
	gzip {
		ext *
		level 7
		min_length 1
	}
}

(addheader) {
	header / {
		Strict-Transport-Security "max-age=31536000;"
		X-XSS-Protection "1; mode=block"
		X-Content-Type-Options "nosniff"
		X-Frame-Options "SAMEORIGIN"
		Referrer-Policy "strict-origin-when-cross-origin"
		-Server
	}
}

### server NAS

nas.server.com {
	import gzipconf
	import addheader
	log / /share/homes/user/caddy/accesslogs/server-NAS.log "{hostonly} {combined} {latency_ms}"
	proxy / http://192.168.10.5:8080 {
		keepalive 32
		transparent
		websocket
	}
}

3. The problem I’m having:

Help moving to version 2 (for the config file, as the sh would be upgraded by the new qpkg, and I think it would need simple customization.

4. Error messages and/or full log output:

Not tried yet as really afraid to break 20 and counting proxies…

5. What I already tried:

Reading the doc[1], but I am really bad at understanding JSON and necessary steps.

[1] Upgrading to Caddy 2 — Caddy Documentation

6. Links to relevant resources:

https://forum.qnap.com/viewtopic.php?f=320&t=155758

Hmm, well I have no experience with QNAP, so I’m not sure I can help with that part.

We have a systemd service file you can find here, if it uses systemd :man_shrugging:

Well, give a shot at converting the Caddyfile to Caddy v2 format, and if you run into snags, we can help.

You don’t need to JSON at all. In Caddy v2, the Caddyfile is an adapter which outputs JSON config, which Caddy actually runs with. But that’s just an implementation detail most of the time.

What JSON are you trying to understand exactly? There’s no JSON on that page :thinking:

Ok, so there is no straight forward way to convert the code below?

That would be already a good start for me…

From the guide should it be something like the following?

nas.server.com {
reverse_proxy 192.168.10.5:8080
  }
}

But I have no idea how would be the entire section…

Yep, just reverse_proxy 192.168.10.5:8080.

Caddy v2’s proxy has “transparent” and “websocket” enabled by default, essentially. And you likely don’t need to modify keepalives, rarely any benefit to do so. The defaults are sufficient.

Thank you, so the whole file could be reduced to:

nas.server.com {
reverse_proxy 192.168.10.5:8080
}

Correct?

And if I want to put back logging, can I use exactly the same syntax?

nas.server.com {
reverse_proxy 192.168.10.5:8080
log / /share/homes/user/caddy/accesslogs/server-NAS.log "{hostonly} {combined} {latency_ms}" 
}

Thanks!

For logging, please see the upgrade guide, and v2 docs: Upgrading to Caddy 2 — Caddy Documentation