How to expose my localhost subdomain?

My web application consists of the following:

  • Frontend listening to port 80
  • Backend listening to port 8080

Using Nginx, I was able to re-route traffic from my subdomain api.localhost to localhost port 8080.
When I send requests to api.localhost from localhost, the backend server is able to pick the requests up.
The problem is when I try to deploy my application to production, where I have cloudflare as my dns.
When I access mywebsite.com, I can load my frontend pages.
When I access api.mywebsites.com, there’s no response from the backend.
However, if I go to my vm and run curl on api.localhost, the backend is able to see the requests.
How do I reroute api.mywebsite.com to api.localhost?

This sounds like something I’d do with a Cloudflare Tunnel: Configure an Ingress Rule to map api.example.com to http://api.localhost:8080

https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/configuration/configuration-file/ingress

3 Likes

Thanks! I’ll try adding an ingress rule as you mentioned.

Out of curiosity, is it possible to achieve this with only the dns settings?
I’d like to try setting this up without any cloudflare-only software if possible

DNS only doesn’t forward ports. It only replicates what you already have without using any part of Cloudflare (what you already had before adding your domain here).

The only exception is SRV records, which tends to be application-specific. Your application would need to support it, but that’s separate from any Cloudflare functionality.

2 Likes

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