Simplest Caddyfile for local files not working but works with caddy CLI options

1. The problem I’m having:

I have a local directory with an index.html file, and I want to serve it locally, so the Caddyfile is very simple. I have a lot of other Caddyfiles, much more complicated, and everything works flawlessly.

My first test was with the caddy CLI : caddy file-server --listen :2019, executed in the directory, and everything works.

But then I tried this Caddyfile, thinking it was identical with the cli options :

http://localhost:2019 {
    log
    file_server
}

and it doesn’t work. The logs even in debug mode are not helpful to me :

$ caddy run
2024/11/08 10:44:59.258	INFO	using adjacent Caddyfile
2024/11/08 10:44:59.259	INFO	adapted config to JSON	{"adapter": "caddyfile"}
2024/11/08 10:44:59.259	WARN	Caddyfile input is not formatted; run 'caddy fmt --overwrite' to fix inconsistencies	{"adapter": "caddyfile", "file": "Caddyfile", "line": 2}
2024/11/08 10:44:59.260	INFO	admin	admin endpoint started	{"address": "localhost:2019", "enforce_origin": false, "origins": ["//localhost:2019", "//[::1]:2019", "//127.0.0.1:2019"]}
2024/11/08 10:44:59.260	DEBUG	http.auto_https	adjusted config	{"tls": {"automation":{"policies":[{}]}}, "http": {"servers":{"srv0":{"listen":[":2019"],"routes":[{"handle":[{"handler":"subroute","routes":[{"handle":[{"browse":{},"handler":"file_server","hide":["./Caddyfile"]}]}]}],"terminal":true}],"automatic_https":{"skip":["localhost"]},"logs":{"logger_names":{"localhost":[""]}}}}}}
2024/11/08 10:44:59.260	INFO	tls.cache.maintenance	started background certificate maintenance	{"cache": "0xc00065f100"}
2024/11/08 10:44:59.260	DEBUG	http	starting server loop	{"address": "[::]:2019", "tls": false, "http3": false}
2024/11/08 10:44:59.260	INFO	http.log	server running	{"name": "srv0", "protocols": ["h1", "h2", "h3"]}
2024/11/08 10:44:59.261	INFO	autosaved config (load with --resume flag)	{"file": "/home/marrco/.config/caddy/autosave.json"}
2024/11/08 10:44:59.261	INFO	serving initial configuration
2024/11/08 10:44:59.266	INFO	tls	storage cleaning happened too recently; skipping for now	{"storage": "FileStorage:/home/marrco/.local/share/caddy", "instance": "f02a28c7-4ce7-475e-8608-eb5358e4c9dd", "try_again": "2024/11/09 10:44:59.266", "try_again_in": 86399.999999449}
2024/11/08 10:44:59.267	INFO	tls	finished cleaning storage units
2024/11/08 10:45:04.992	INFO	admin.api	received request	{"method": "GET", "host": "127.0.0.1:2019", "uri": "/index.html", "remote_ip": "127.0.0.1", "remote_port": "51224", "headers": {"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"],"Accept-Encoding":["gzip, deflate, br, zstd"],"Accept-Language":["fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3"],"Connection":["keep-alive"],"Cookie":["CSRF-Token-4ZEIO=XSH4znbRXSnGTN7LmbXAFYCqTjzUU9Ri; CSRF-Token-V5VIE=pdHLtPVKetNamMnD46LdSAkqq6rX72Zq; CSRF-Token-4ZEIOVS=vi7SMHrbFmCMNtNver59U7wtGeWjXQi93CKdLRgXX25rKuHzXd6aqbPTkkVy9MoP; CSRF-Token-V5VIEAQ=VxuXkqACW7Zfw9JAVGqEu7ApWY6hfZzdP2dxDZHYWKWmdewCY5hx6C33nepu3wRH"],"Priority":["u=0, i"],"Sec-Fetch-Dest":["document"],"Sec-Fetch-Mode":["navigate"],"Sec-Fetch-Site":["none"],"Sec-Fetch-User":["?1"],"Upgrade-Insecure-Requests":["1"],"User-Agent":["Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:132.0) Gecko/20100101 Firefox/132.0"]}}

I don’t want to use https, even locally with self certificate because my port 80 is already in use.

What do I do wrong here ? I guess it’s a simple mistake on my end but I cannot find it.

Thank you

3. Caddy version:

caddy v2.8.4 on ubuntu 24.04

In what way does it “not work”? You didn’t show any evidence of a problem. Not sure how you expect help without showing the problem.

1 Like

you’re right sorry, I was so much expecting an answer like “of course it’s obvious you made this rookie mistake …” that I forgot to add this.

When I navigate to http://localhost:2019, and caddy is using the Caddyfile above, I get a 404 error.

When I run caddy with the cli options above without caddyfile, it works well.

Use a different port. Caddy’s admin API runs on port 2019. Use 8080 or something.

1 Like

Ah thank you.

Did this, and then now I get an empty blank page, so I’ll keep investigating. It still working ok with the cli.

Show exactly what you tried. Show your Caddyfile and an curl -v command output.

1 Like

I already posted the caddyfile, I just changed the port like you said.
And the blank page is juste a blank page, the request returns nothing, with content-length: 0.

But you were right in the beginning, changing the port fixed the issue, I was requesting 127.0.0.1 instead of localhost. When using localhost, I works ! I guess that when setting localhost in the caddyfile it allows only localhost, but running caddy from cli also allows by default 127.0.0.1.

thanks

Okay, then show that.

Okay, and show what you’re doing with curl -v.

Seriously, we need to see exactly what you’re doing, not a retelling of what you think you did.

Correct, the site address is a Host matcher, so it only matches requests with the same host. See Caddyfile Concepts — Caddy Documentation

1 Like

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