Skip to content

Instantly share code, notes, and snippets.

@turtlemonvh
Last active June 12, 2023 17:51
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save turtlemonvh/1e6dc4621202b2c1a8c4 to your computer and use it in GitHub Desktop.
Save turtlemonvh/1e6dc4621202b2c1a8c4 to your computer and use it in GitHub Desktop.
Multi-host wildcard caddy example

Caddy multiple virtual hosts

Run caddy to proxy requests to multiple upstream servers running on different ports on the same box.

How to

On the box you want to analyze:

# Download caddy
mkdir -p caddyserver && cd caddyserver
wget <>
tar -xzvf <>
cd -

# Add the Caddy file, flask server from this example

# Start caddy
./runserver.sh

Create a wildcard route to your server (*.myhost.com). Route 53 works well for this.

In your browser:

How it works

The Caddyfile in the current directory makes caddy serve to

a.myhost.com {
tls off
root /var/www/
proxy / localhost:8091
log log/access.a.log
}
b.myhost.com {
tls off
root /var/www/
proxy / localhost:8092
log log/access.b.log
}
import argparse
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World from port %d!' % options.port
if __name__ == '__main__':
parser = argparse.ArgumentParser("Hello world with port information")
parser.add_argument('port',
action='store',
type=int,
help="Port to run on."
)
options = parser.parse_args()
app.run(port=options.port)
#!/bin/bash
# Create directory for access log
mkdir -p log
# Start upstreams
python hello_port.py 8091 &
python hello_port.py 8092 &
# Start caddy proxy server
caddyserver/caddy -conf Caddyfile -port 80
Copy link

ghost commented Jul 11, 2021

what

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment