Search code examples
pythondnsportsubdomainhosting

Assign different subdomain for different ports


I have a vm instance with a public external ip say 10.20.30.40

Now i am hosting my files via python http server at port 80

and running a fastapi app at port 8000

...

Let's say i own a domain abc.com

I want to point -

abc.com ----> 10.20.30.40

api.abc.com ----> 10.20.30.40:8000

...

but i won't be able to point a domain to specific port using dns records

Is there a way to do that or any other alternate solution to achieve similar behaviour ??


Solution

  • What you want to achieve is typically done with a "reverse proxy server". What is does is it looks for specific url paths and redirects your traffic to the corresponding server on the corresponding port.

    Search for 'reverse proxy nginx' for a tiny simple solution. (This server could even run on the same VM as you are currently working on). There are other solutions out there. E.g. Traefik or Squid

    What you do then is basically route both your subdomains to your same external IP. Best would be to let your reverse proxy server run on port 80 (or 443 for TLS) so you dont have to specify a port when using a browser. Give the python http server another port to listen on (e.g. 8001). Then you configure your reverse proxy to redirect your traffic to localhost:8000 (for API) and to localhost:8001 (for python http server)