Caddy 2.6 and dynamic content

From the release notes:

  • Serve dynamically-generated content that “feels” static

Any examples of how one could implement something like “what is my ip” that would just return the IP address of the client connecting to Caddy?

For bonus points, something more complicated that can handle proxying via Cloudflare and instead extract the IP from the HTTP_CF_CONNECTING_IP header and also do a reverse DNS lookup on the IP to show the DNS name too.

I know how to do all of the above with PHP and php-fpm, but I’m wondering whether it can be done in a simpler way just with Caddy which would make it much easier to just move it from server to server.

1 Like

I remember this conversation.

Caddy 2.6.0 works with a Caddyfile like this.

server.example.com

header Content-Type text/plain
respond "{{.RemoteIP}}"
templates
3 Likes

Just a quick addition to what @balloon wrote:

You could also directly use a {placeholder}, if all you want to do is to respond with the client’s IP:

localhost {
	respond {remote_host}
}

or its longer form

localhost {
	respond {http.request.remote.host}
}
2 Likes

Thanks, interesting.

Is it possible to do more advanced things such as looking at headers, reverse DNS, without writing a specialized module?

In order to make routing/handling decisions? You’re looking for matchers which already exist. There are matchers for headers, I don’t know about reverse DNS though because that’s extremely slow in terms of HTTP requests (you’d have to do DNS lookups…) so maybe someone has written a plugin to do that but that’s probably where you’d have to look.

1 Like

What is the easiest way to invoke dig +short -x <ip> from Caddy, or another external script, e.g. bash script? Or is fastcgi the way to do it? This is more of an educational question, not for a high volume site, etc. we can ignore the performance aspects of starting a new process per request.

Honestly I would just write a Caddy plugin in Go – after you copy-paste the template and change a few lines, you’d only probably be writing about 5-10 lines of code for something like that.

1 Like

I’m looking for something more dynamic/scriptable that I can experiment with quickly, without the full compile/rebuild Caddy cycle. Is there no non-PHP CGI built-in?

Arbitrary command execution? No. There is a plugin that does that, I think. Or you could use forward_auth or X-Accel-Redir to gate access to stuff behind a proxy.

Here’s my suggestion:

  1. Copy-paste the boilerplate: Extending Caddy — Caddy Documentation

  2. Add a Match method: caddyhttp package - github.com/caddyserver/caddy/v2/modules/caddyhttp - Go Packages – just have it return false for now.

  3. Run xcaddy run

Then write your code (probably a few lines).

I bet you can be up and running in a few minutes!

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