Skip to content

Instantly share code, notes, and snippets.

@MrKou47
Forked from turtlemonvh/Caddyfile
Created October 28, 2018 02:50
Show Gist options
  • Save MrKou47/171999760a9fb25560b4028919e10ed7 to your computer and use it in GitHub Desktop.
Save MrKou47/171999760a9fb25560b4028919e10ed7 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment