Specific route handling when using try_files

1. The problem I’m having:

I have a static website with try_files and that works as expected. Now, I am trying to add a special handler for a specific route /ip. Though it just keeps loading the index.html

2. Error messages and/or full log output:

N/A

3. Caddy version:

caddy version
v2.10.0 h1:fonubSaQKF1YANl8TXqGcn4IbIRUDdfAkpcsfI/vX5U=

4. How I installed and ran Caddy:

Running using Docker, within a docker-compose.

a. System environment:

Docker

b. Command:

N/A

c. Service/unit/compose file:

N/A

d. My complete Caddy config:

{
	debug
	servers :80,:443 {
		protocols h1 h2c h2 h3
	}
}

(security_headers) {
	header * {
		# enable HSTS
		# https://cheatsheetseries.owasp.org/cheatsheets/HTTP_Headers_Cheat_Sheet.html#strict-transport-security-hsts
		# NOTE: Read carefully how this header works before using it.
		# If the HSTS header is misconfigured or if there is a problem with
		# the SSL/TLS certificate being used, legitimate users might be unable
		# to access the website. For example, if the HSTS header is set to a
		# very long duration and the SSL/TLS certificate expires or is revoked,
		# legitimate users might be unable to access the website until
		# the HSTS header duration has expired.
		# The recommended value for the max-age is 2 year (63072000 seconds).
		# But we are using 1 hour (3600 seconds) for testing purposes
		# and ensure that the website is working properly before setting
		# to two years.

		Strict-Transport-Security "max-age=3600; includeSubDomains; preload"

		# disable clients from sniffing the media type
		# https://cheatsheetseries.owasp.org/cheatsheets/HTTP_Headers_Cheat_Sheet.html#x-content-type-options
		X-Content-Type-Options "nosniff"

		# clickjacking protection
		# https://cheatsheetseries.owasp.org/cheatsheets/HTTP_Headers_Cheat_Sheet.html#x-frame-options
		X-Frame-Options "SAMEORIGIN"

		# xss protection
		# https://cheatsheetseries.owasp.org/cheatsheets/HTTP_Headers_Cheat_Sheet.html#x-xss-protection
		X-XSS-Protection "1; mode=block"

		# Remove -Server header, which is an information leak
		# Remove Caddy from Headers
		-Server

		# keep referrer data off of HTTP connections
		# https://cheatsheetseries.owasp.org/cheatsheets/HTTP_Headers_Cheat_Sheet.html#referrer-policy
		Referrer-Policy strict-origin-when-cross-origin
	}
}

kamaleshwar.com:80, kamaleshwar.com:443 {
	# serve from static directory with default as index.html
	root * /usr/share/caddy/kamaleshwar
	file_server {
		index index.html
	}
	# serve from static directory with default as index.html

	route /ip {
		respond "{remote_host}"
	}

	try_files {path} /index.html
	import security_headers
}

5. Links to relevant resources:

The route directive has a lower priority than, for example, try_files.

does that mean it’s impossible to do without doing specific pattern matching for try_files? e.g.

route /hello* {
try_files {path} /index.html
}

You can use handle or route to wrap around your other stuff in the block. For example,

your-site {

    handle /ip {
        # your /ip stuff
    }

    handle {
        # your default stuff
    }

}
1 Like