Multiple services from one server/ port

Hey all. I feel like I’m asking a stupid question here, but I really am curious. Obviously, the best option for running two different services is to run them from different addresses, but let’s say I’m a broke college student / farmer who just wants to build some toy utilities in his spare time and can really only justify one single VPS and a bottle of coke given my paychecks. After that, my assumption would be to just run the service on a different port. Beyond that, though, I’m considering a more programmatic solution. If I were to have two different “.tk” addresses and point them at the same IP, I could just use the referrer to tell which address I was actually using from my end, right? So “example1.tk” and “example2.tk” could lead to two very different experiences, even though they’re running on the same server on the same port, right? Is there a proper way to actually accomplish this that doesn’t involve breaking down the request and burning as many CPU cycles?

try this, Traefik

1 Like

You run them on different unprivileged ports and have a third service on 80/443, this third service is a so called reverse proxy. It will hand over requests to the “backend” services configured according to the rules that are configured.

3 Likes

Hi @ashinnv,

You have a couple of options.

Option 1: Identify the two services by URL path. For example,

https://yourdomain.tk/api/service1/...
https://yourdomain.tk/api/service2/...

Option 2: Use subdomains. If you own a domain, you can create subdomains, for example,

https://service1.yourdomain.tk/api/...
https://service2.yourdomain.tk/api/...

Typically, each subdomain would resolve to an individual IP address but you can also have them resolve to the same IP address and have your code route the requests by subdomain.

Option 3: Use a reverse proxy to route the requests to two different processes - this is what @NobbZ and @luk4z7 suggest. (Traefic is such a reverse proxy.)

This option requires the most work (setting up a reverse proxy, managing three different processes,…), but in return you get more flexibility. You can restart one service without affecting the other, you can add a third service quite eaisily, you can update a service without downtime by first spawning the new version of the service on a different port, then have the proxy use this one for all new requests, and when all existing requests are complete, you can take down the old version of the service.

Regarding CPU cycles: Especially Option 1 and 2 should not add any significant load on your CPU. In other words, processing a request should require significantly moe CPU cycles than the routing.

If in doubt, test and measure.

2 Likes

You can do this using Nginx and proxy them to different ports. I recommend Cloudflare to manage the DNS. https://hosting.go4webdev.org/howto

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.