Skip to content

Instantly share code, notes, and snippets.

@minism
Created April 25, 2020 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minism/0e8cf6a653bed32bf887ed57a59a7971 to your computer and use it in GitHub Desktop.
Save minism/0e8cf6a653bed32bf887ed57a59a7971 to your computer and use it in GitHub Desktop.
Simple https nginx config to route to docker containers by subdomain
upstream foo {
server foo-container:80;
}
upstream bar {
server bar-container:3000;
}
map $subdomain $container {
foo foo;
foo_alias foo;
bar bar;
}
# Routes to the docker container given in the subdomain map or 404s.
server {
listen 443 ssl;
server_name ~^(?<subdomain>\w+)\.example\.com$;
ssl_certificate /etc/ssl/certs/live/example.com/fullchain.pem;
ssl_certificate_key /etc/ssl/certs/live/example.com/privkey.pem;
location / {
if ($container = "") {
return 404;
}
proxy_redirect off;
proxy_pass http://$container;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-Host $host;
}
}
# Universal HTTPs redirect and challenge serving.
server {
listen 80;
listen [::]:80;
server_name ~^(?<subdomain>\w+)\.example\.com$;
location ^~ /.well-known/acme-challenge/ {
root /var/www/letsencrypt;
}
location / {
return 301 https://$subdomain.example.com$request_uri;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment