Created
April 25, 2020 08:22
-
-
Save minism/0e8cf6a653bed32bf887ed57a59a7971 to your computer and use it in GitHub Desktop.
Simple https nginx config to route to docker containers by subdomain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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