Skip to content

Instantly share code, notes, and snippets.

@v1shwa
Created March 21, 2019 13:54
Show Gist options
  • Save v1shwa/65cc0d168b29d24d09e478c9ea8e1cb7 to your computer and use it in GitHub Desktop.
Save v1shwa/65cc0d168b29d24d09e478c9ea8e1cb7 to your computer and use it in GitHub Desktop.
Dynamically map subdomains to different ports on the server - using Nginx
server {
listen 80;
# maps p8080.example.com -> localhost:8080
server_name ~^p(?<port>[^.]+)\.example\.com$;
location / {
proxy_pass http://localhost:$port;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
  • You need to create an wilcard A record for the domain(or subdomain) first.

    A * points to 1.2.3.4 Automatic

  • Create an nginx config file with above content & restart the web server.

  • Done.

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