How to proxy requests to a particular path to another domain?

I have a cloudflare page from which I will have to send API requests to my heroku backend.

Let’s say my backend is hosted at backend(dot)domain(dot)url

Both my backend and frontend dns is handled by cloudflare.

I want all requests from domain(dot)com starting with /api in its path to be proxied back to
backend domain.

I am unable to specify what my backend url is for one of the library I am using. It will always send it to the current url /api route.

I can achieve something similar in nginx using

I tried doing my research but a lot of them were outdated at this point.
I already tried using the Bulk Redirect option but that is well redirecting the browser to the backend which I dont want.
I want the url to stay the same but the actual request be forwarded/proxied to my backend and then the response to be supplied back.

For anyone facing similar needs,
Here is what I have done using Cloudflare Functions

[[api]].js

I placed this file under “functions/api” in my projects root directory

This is correctly routing all the “/api” calls into my backend.

Only downside to this methods is you are limited to 10,000 requests per day.
This is fine for my current use case but would have been great if there was some other way without a limit in place.

1 Like