Proxying from one app to another

I’m having some trouble proxying from one Netlify app to another. I won’t use actual application names or domains here but can do so privately.

I have two applications:

  • An API. This has a custom domain (e.g. api.robsapi.com) and a bunch of functions.
  • A SPA React API, say react-rob-ui.netlify.app. I want to proxy requests to react-rob-ui.netlify.app/api to the API so I don’t have to faff about with CORS (at least for now!)

I’m doing some rewriting of URLs for the API app. Here’s the netlify.toml file:

Rewrite API requests to actual location of serverless functions

[[redirects]]
from = “/v1/*”
to = “/.netlify/functions/:splat”
status = 200

I have a “ping” function that returns “pong”. Calling “https://api.robsapi.com/v1/ping” successfully returns “pong” so I know the API application is working as expected.

For the React app, I’ve got the following:

[[redirects]]
from = “/api/*”
to = “https://api.robsapi.com/v1/:splat
status = 200
force = true

Browsing to “https://react-rob-ui.netlify.app/api/ping” should be proxied through to the API app and then return “pong”. Instead it returns a 200 with an empty body (i.e. no pong!).

Should this work? What am I doing wrong?

TIA,

Rob

Hi, @robbiehudson, yes. That should work. If it is not, it would be helpful to test the actual URLs which don’t proxy.

You can private message (PM) that to one of our support staff and I’ve confirmed that PMs are enabled for your community login. Note, that only one person can see the PM and this will likely mean a slower reply than posting the information publicly. Please feel free to reply to however you prefer though.

Thanks @luke!

I’ve PM’d you…

@luke still waiting for a reply on this!

Hi, @robbiehudson. It turns out the behavior is a bug. I’ve gotten an issued filed and the issue is cross-linked to this topic for tracking.

If/when this is known to be resolved, we will post a follow-up here to let you know about it.

The issue works like this:

  • there is a proxy redirect from site-1 to site-2
  • there is a proxy redirect on site-2 to a different path on the same site - site-2
  • the redirect systems checks the source URL path (for site-1) when checking the redirects on site-2
  • because the original source URL path doesn’t match the redirect on site-2 the second proxy rule doesn’t trigger

The only workaround at this time would be to add the same proxy redirect rule to the second site.

Let’s say the site-1 redirect looks like this:

[[redirects]]
  from = "/api/*"
  to = "https://api.example.com/v1/:splat"
  status = 200
  force = true

And that the site-2 redirect looks like this:

[[redirects]]
  from = "/v1/*"
  to = "/.netlify/functions/:splat"
  status = 200

If so, the the workaround would be to add a second rule at site-2 to match the first URL as well like so (this is both rules):

[[redirects]]
  from = "/v1/*"
  to = "/.netlify/functions/:splat"
  status = 200

[[redirects]]
  from = "/api/*"
  to = "/.netlify/functions/:splat"
  status = 200

Again, we will follow-up if the issue itself is known to be resolved and the solution above is only a workaround for the behavior.

If this workaround doesn’t work and/or if there are other questions about this, please let us know.