Skip to content

Instantly share code, notes, and snippets.

@simplesoft-duongdt3
Forked from v1shwa/dynamic_subdomains
Created May 24, 2023 09:43
Show Gist options
  • Select an option

  • Save simplesoft-duongdt3/080796cb8cc33079611a6c892dfe00b0 to your computer and use it in GitHub Desktop.

Select an option

Save simplesoft-duongdt3/080796cb8cc33079611a6c892dfe00b0 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