Confusion about directive order

1. The problem I’m having:

I am having trouble understanding the order of directives. The caddyfile config listed below works as intended, but it’s contrary to the document:

Here in the document it says the same named directive will be sorted by matcher, and based on my understanding in my caddyfile config, the /share path handle directive should take priority over a handle directive with a named matcher like @wechat, but in reality the @wechat handle has priority and shows a banned message to user.

I am having a hard time understanding it.

2. Error messages and/or full log output:

N/A

3. Caddy version:

2.10

4. How I installed and ran Caddy:

Docker

a. System environment:

Synology DSM 7.2

b. Command:

N/A

c. Service/unit/compose file:

PASTE OVER THIS, BETWEEN THE ``` LINES.
Please use the preview pane to ensure it looks nice.

d. My complete Caddy config:

	@wechat header User-Agent *MicroMessenger*
	handle @wechat {
        header Content-Type "text/html; charset=utf-8"
		respond "<html><head><title>Banned</title></head><body><h1>UA banned</h1></body></html>" 200
    }
	handle /share* {
		file_server {
			browse
			root /etc/caddy/
		}
	}

5. Links to relevant resources:

1 Like

It sorts by longest path matchers only. There’s no good way to measure “longest” if the matchers are different. If they’re different, the matcher defined first stays before the other. This is the comment on the piece of code where the magic happens.

1 Like

Isn’t that contradictive to the document?

The highest priority is a directive with a single path matcher.
A directive with any other matcher is sorted next, in the order it appears in the Caddyfile.

Based on document, it seems that for all handle directives, all the ones with a single path matcher should have priority over any ones with non-path matchers.

However in the comments of the code, it is written:

			// if both directives don't have a single path to compare,
			// sort whichever one has a matcher first; if both have
			// a matcher, sort equally (stable sort preserves order)

Honestly if both directives don't have a single path to compare sounds grammatically wrong to me and confusing. Does it mean if one of the two directives doesn't have a single path to be compared? In that case, if one matcher has a single path, the other has a named matcher, there is no way to compare them, so the order they appear in the caddyfile will be the final order, is that what you are saying? If this is true, then the document is wrong, but this does coincide with my observation.

1 Like