Skip to content

Instantly share code, notes, and snippets.

@tommyvn
Created March 22, 2022 22:10
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 tommyvn/c8663fce9c2e3e3402b74c80f3df03c8 to your computer and use it in GitHub Desktop.
Save tommyvn/c8663fce9c2e3e3402b74c80f3df03c8 to your computer and use it in GitHub Desktop.
nginx server name path routing

nginx server name path routing

This example nginx configuration will enable subdomain routing to a single backend path based on the subdomain.

It shows both catch-all routing and specific subdomain routing.

#!/bin/bash
# use host networking to enable forward to another port on localhost
docker run -v $PWD/nginx.conf:/etc/nginx/nginx.conf:ro --network=host nginx
events {
worker_connections 512;
}
http {
server {
# route any subdomain to localhost/subdomain
listen 8080;
server_name ~^(?<name>\w+)\.example\.com$;
location / {
proxy_pass http://127.0.0.1:8081/$name;
}
}
server {
# route app3 subdomain to localhost/app3-specific
listen 8080;
server_name app3.example.com;
location / {
proxy_pass http://127.0.0.1:8081/app3-specific;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment