Help With Header Listener Rule For Forwarding Request

1. The problem I’m having:

I’m building a public facing API with two environments (production and testing/staging) which will utilise API-Keys for authentication, I’d like to make it such that I have two instances for both env, and a single API base URL (say api.example.com ).

Is it possible to achieve this using Caddy, that is, forward request base on a given header value, say, "api-key", if it starts with "api_test_theKeys" it send the requests to to the test server/instance, and if it were to start with "api_live_theKeys" it send the requests to to the production/live server/instance.

Mind you, I’ve done something similar with different base URLs for both environments (say api.example.com for live, and api.test.example.com for testing env), in this case the client’s demand I have it this way, and I’ve been looking at ways to go about it.

The client current infras are hosted on DigitalOcean, it occurred to me to put both instances (Droplets on DO) behind a load balancer, and set a listening rule to watch for the header key-value and forward requests accordingly to the droplets, unfortunately, DO only have forwarding rules.

Pardon my many NA below, I’m not having issues with Caddy, I’ve used it a couple of times in the past, I just need advice, suggestions, pointers, and/or input to assist me here.

2. Error messages and/or full log output:

NA

3. Caddy version:

v2.6.4

4. How I installed and ran Caddy:

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy

a. System environment:

Ubuntu 22.04.2 LTS (GNU/Linux 5.15.0-67-generic x86_64) - On DigitalOcean

b. Command:

NA

c. Service/unit/compose file:

NA

d. My complete Caddy config:

# The Caddyfile is an easy way to configure your Caddy web server.
#
# Unless the file starts with a global options block, the first
# uncommented line is always the address of your site.
#
# To use your own domain name (with automatic HTTPS), first make
# sure your domain's A/AAAA DNS records are properly pointed to
# this machine's public IP, then replace ":80" below with your
# domain name.

api.example.com {
        # Set this path to your site's directory.
        # root * /var/www/html

        # Enable the static file server.
        file_server

        # Another common task is to set up a reverse proxy:
        reverse_proxy localhost:3000

        # Or serve a PHP site through php-fpm:
        # php_fastcgi localhost:9000
}

# Refer to the Caddy docs for more information:
# https://caddyserver.com/docs/caddyfile

5. Links to relevant resources:

Yes, you can match by header

I’d suggest using standard header fields, i.e. the Authorization header.

See Caddyfile Concepts — Caddy Documentation, you can have multiple sites, each site being a different domain name.

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