Site will not render templates (on Apline Linux edge)

Problem: Caddy will not render my site on Alpine Linux (both v3.16 and edge) because of problems with templates / includes. Adding a response "Hello world" in the Caddyfile produces the expected results, but my templates does not work.

Context: I had the same problem with Caddy version 2.4.6 shipping with Fedora 36. Downloading and using the latest binary version (2.5.2) on my Fedora workstation fixed the problem (…might be related to this Github issue/PR?).

Alpine edge has Caddy version 2.5.2 so I expected it to work, but it does not. Could it be that Caddy is built without support for templates on Alpine? How can I check this?

Steps to reproduce: I just pushed out the code to sourcehut, so if anyone wants to test this on different versions of Caddy to see if they experience the same blank page, you can clone the code (git clone https://git.sr.ht/~rsolvang/vigilantes) and execute caddy run -c Caddyfile.dev (which start at port 2012).

A blank page with a 200 response code usually means your request didn’t match any routes.

What is the exact output of curl -v?

Next time, please follow the help template. That’s crucial for our being able to help you and is a forum rule.

Also add this to the beginning of your Caddyfile for more details:

{
    debug
}

What is in your server logs?

Sorry for the lack of information! After some further testing on the Alpine box, I found that the site works perfectly fine when running v2.5.2 downloaded directly from caddyserver.com/downloads using the same Caddyfile.

Output from curl when using the Caddy from the Alpines repos:

curl -v localhost
*   Trying 127.0.0.1:80...
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> Host: localhost
> User-Agent: curl/7.84.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 404 Not Found
< Server: Caddy
< Date: Tue, 23 Aug 2022 19:53:03 GMT
< Content-Length: 0
< 
* Connection #0 to host localhost left intact

…nothing.

Adding the debug option to my Caddyfile did not produce anything helpful. I tried enabling logging (see my Caddyfile below) but nothing got written to the logs. However, the binary I downloaded from caddyserver.com works without problems and produces normal logs.

Caddyfile:

{
	debug
}
:80

file_server
templates

try_files {path}.html

log {
	output file /var/log/caddy.log {
		roll_uncompressed
	}

}

I’m not sure what’s going on, but I guess something is wrong with the caddy binary that is built and packaged with Alpine?

Thanks for the additional details.

404 just means the file was not found, which has nothing to do with templates. There probably was no file loaded to evaluate templates on in the first place (i.e. no index file).

That means you’re not even reaching your Caddy instance.

Keep in mind that your access logs will be written to caddy.log while all other process logs will still be written to stderr unless you reconfigure that too.

Just going to guess here without the logs (which will tell us for certain) but your Alpine system doesn’t have a complete or properly-configured MIME database.

Basically, I think the problem has nothing to do with Caddy or your config and everything to do with the system you’re running it on, or your system configuration.

When I’m adding a response "hello world" to the Caddyfile, it is returned, so “normal” things do work – but it refuses to render my template-related code. But I suspect this is a Alpine problem somehow, I skimmed through the buildlogs and found lines like this:

github.com/caddyserver/caddy/v2/modules/caddyhttp/templates.test
loadinternal: cannot find runtime/cgo

I guess this could be related to the fact that Alpine uses musl?

Caddy builds without cgo; that’s fine, those messages are red herrings.

I think what you’ll want to check is that your debug logs indicate the file was actually found and that the Content-Type is set properly.

1 Like

Ok, I think I figured out what went wrong, and it’s really basic. I hadn’t defined a root directive in the Caddyfile, as I assumed caddy was looking in the same directory as the Caddyfile was placed by default in Alpine: /etc/caddy/. As soon as I added a root directive specifying the path, it worked as expected.

This explains why it worked when I ran the site directly with the downloaded binary; it was executed from the same directory as the HTML-files.

The Alpine system does not have a /var/www or public_html folder anywhere either, so I guess that makes it a requirement to define a path to the sites root when using Alpine :man_shrugging:

Hope this thread can help someone searching the web if they make the same erroneous assumptions as I did!

EDIT: I guess this could explain why it did not work on my Fedora Workstation either! I’ll have to test that tomorrow.

1 Like

It looks in Caddy’s working directory, which is where the command to run caddy was run from. That entirely depends on how Caddy was run, how the service manager is configured etc.

Remember that the Caddyfile is not the actual config Caddy runs with; it’s adapted into a JSON config, then that’s what runs. So as soon as the config is adapted, Caddy “forgets” where the Caddyfile was from.

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