Unable to set {http.vars.root}

1. Caddy version (caddy version):

v2.2.2

2. How I run Caddy:

a. System environment:

Linux Mint 20

b. Command:

caddy run --config ~/test.json

d. My complete Caddyfile or JSON config:

{
    "apps": {
        "http": {
            "servers": {
                "server1": {
                    "listen": [":8080"],
                    "automatic_https": {
                        "disable": true
                    },
                    "routes": [
                        {
                            "match": [
                                {"host": ["localhost"]}
                            ],
                            "handle": [
                                {
                                    "handler": "vars",
                                    "http.vars.root": "/var/www/localhost/asset_root/"
                                },
                                {
                                    "handler": "file_server",
                                    "index_names": ["index.html"]
                                }
                            ]
                        }
                    ]
                }
            }
        }
    }
}

3. The problem I’m having:

According to the docs, the default root path of the file server is {http.vars.root}. Therefore I’m trying to set this variable so that the fileserver points to the correct location. However, my configuration above doesn’t seem to set the value.

5. What I already tried:

I’ve tried setting the root property of file_server manually and everything works as expected. The problem only occurs when I try to set the {http.vars.root} variable, which leads me to believe it’s not being set.

So, this is not actually a valid version of Caddy, it was a mistaken release. See this comment:

Please run apt install caddy=2.2.1 to re-install the latest stable version.

As for your vars handler, it should just be this:

{
	"handler": "vars",
	"root": "/var/www/localhost/asset_root"
}

You can see in the root handler’s docs that the rest of the keys are the variable names to set, with the appropriate value.

If it’s your first time using Caddy, I recommend starting from a Caddyfile instead, then using the caddy adapt command to get the underlying JSON config.

Your JSON config is basically just this in Caddyfile:

:8080 {
	root * /var/www/localhost/asset_root
	file_server
}

Then you can run caddy adapt --pretty to get the JSON config.

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