Caddy 2 and TCP Proxying
How to configure Caddy2 to proxy TCP streams.
To illustrate the architecture, I have the following entities running in Docker.
I’m using Caddy 2 as a reverse proxy, but it looks like it supports many more scenarios. Previously I used nginx, or traeffic. The appeal of Caddy is that it supports TLS (with LetsEncrypt) out of the box and integrates with Consul for an eventual clustering solution with Nomad.
Startup with Caddy was very simple. Configuration with Caddyfile
is quite straightforward. I was up and proxying internal HTTP services in no time. I did hit a snag when I needed to proxy non HTTP, in this case an MQTT stream. Turns out it’s not very difficult but there’s no good description of how to do it.
First, you need to build caddy with caddy-l4 plugin:
|
|
Since caddy-l4
does not support Caddyfile
s, the configuration needs to be in JSON. Since I already started with a Caddyfile
, I converted it to JSON:
|
|
I then merged the output with the following manually crafted JSON:
|
|
This JSON directs Caddy to:
- establish a connection on port 1883
- terminate the TLS
- pass the unencrypted connection to
mqtt-server
(this hostname is provided and resolved by Docker)
Finally, I had to modify the default execution string for the caddy container in my docker-compose.yml
:
|
|