I want to create a tunnel between my custom domain (e.g.,
backend.dinhanhthi.com
) and my locally hosted server (e.g., http://localhost:3000
). For this, I'll use Cloudflare's Zero Trust, which is free.You can access Zero Trust from the Cloudflare dashboard or via this link.
Follow these steps: Create a tunnel → Choose Cloudflared → Give it a name → Save tunnel → (install
cloudflared
following the docs) and then:- Either activate it once for the current session using
cloudflared tunnel run...
- Or install it permanently on your machine to run automatically at startup by using
sudo cloudflared service install ...
Next, click "Next" and configure Public hostnames:
- Subdomain:
backend
- Domain:
dinhanhthi.com
(select from the list)
- Path: leave empty
- Service: HTTP (Type) and
localhost:3000
(URL)
Then click "Complete setup."
You can now check the result based on the option you chose earlier (
cloudflared tunnel run
or sudo cloudflared service install...
).Remarks:
- To add another subdomain and port, simply modify the existing tunnel and add more public hostnames. No need to create a new tunnel.
- This solution doesn't work when you have an active VPN on your computer!
If you don't have a custom domain and need a simpler solution, consider using ngrok instead.