Could my config be simplified?

1. The problem I’m having:

Not a problem per se, I get the feeling my config could be simplified and I’d like input from an extra pair of eyes that does actually know what they are doing :slight_smile:

Mostly asking if my config could be simplified, as I get the idea that what I’m doing is more complex than needed
What I need Caddy to do other than just serving static files:

  • Set the content-encoding header for a few .gz files
  • Check for .png files and return 204 instead of 404 when they do not exist (if they do exist obviously serve the file)

2. Error messages and/or full log output:

N.A.

3. Caddy version:

v2.8.4

4. How I installed and ran Caddy:

a. System environment:

Ubuntu 22.04.4, arm64

b. Command:

caddy start

and subsequently after editing the caddyfile

caddy reload

c. Service/unit/compose file:

N.A.

d. Caddy config for this page:

handle_path /BlueMap/* {
		root ../bluemap/web

		@gz {
			path /maps/*/textures.json /maps/*/tiles/*.prbm
			file {
				try_files {path}.gz
			}
		}
		route @gz {
			rewrite {http.matchers.file.relative}
			header Content-Encoding gzip
		}

		@png path *.png
		route @png {
			try_files {path} =204
		}

		file_server
	}

Slightly shorter, rewrite {file_match.relative}, see Request matchers (Caddyfile) — Caddy Documentation

Can shorten it to this:

			file {path}.gz

But aside from that, makes sense to me :man_shrugging:

2 Likes

Well, not bad then.

Thanks!

I’ve been tinkering a bit, got it to a point I’m relatively happy with
Looks a lot cleaner than what I had before

	handle_path /BlueMap/* {
		root ../bluemap/web
		file_server

		# Match the textures.json file & .prbm files
		@gz path /maps/*/textures.json *.prbm
		# Find .gz files (if not found respond with 204) for the above matcher, and set the "Content-Encoding gzip" header
		handle @gz {
			try_files {path}.gz =204
			header Content-Encoding gzip
		}

		# Respond with 204 for non-existant map-tiles
		@204 path */tiles/*
		handle @204 {
			try_files {path} =204
		}
	}
1 Like

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