Score:0

Redirect to url from load balancer without CORS error

nr flag

I was wondering if any of you know how to achieve that a GCP load balancer redirect to an url with "CORS enabled". What do I mean by that?, well I have the following scenario:

  • One load balancer that has to redirect to other load balancers depending on the path of the URL (LB A)
  • "Simple" load balancer that has many backends attached (LB B, LB C, etc)

So my flow is as follow:

  • LB A (/pathB) -- redirect -> LB B
  • LB A (/pathC) -- redirect -> LB C

This works as expected if requested by a simple HTTP Request (like cURL or Postman) but fails if its requested on a website. Why?, because the preflight OPTIONS request is redirected and that brings a CORS error Redirect is not allowed for a preflight request, and even if the OPTIONS request is skipped, a simple GET request will also have a redirected response without the CORS headers (which will fail).

Is this possible?, if so how can I achieve it?, I tried to add a cors policy on LB A but a LB can't have a routeAction with a urlRedirect.

Practically I just want to inject the CORS headers on the 301 Response to avoid the error.

Fariya Rahmat avatar
ve flag
Could you please provide the exact error which you are facing? In the meantime please have a look into this [SO link](https://stackoverflow.com/questions/60902310/301-response-with-cross-origin-request-blocked-despite-having-correct-cors-hea).
Alejandro Barone avatar
nr flag
The error itself is CORS, the problem is that on the 301 GET redirect, I can't inject the CORS headers so the request failed. I finally mitigate this without redirecting at all, using GCPs Internet NEG to handle forwarding, don't know if injecting headers on a url redirect is a missing feature of GCP's Load balancers or I just couldn’t find how to do it
Fariya Rahmat avatar
ve flag
Is your issue resolved? Can you check this [SO link](https://stackoverflow.com/questions/42168773/how-to-resolve-preflight-is-invalid-redirect-or-redirect-is-not-allowed-for). Let me know if this helps.
Score:0
ve flag

To add a URL direct follow the below steps:

  1. Go to your list of load balancers in the Google Cloud console.
  2. For a load balancer of type HTTP(S) (Classic), click the load balancer's name link.
  3. Click Edit.
  4. In Host and path rules, select Advanced host and path rule (URL redirect, URL rewrite).
  5. Click Add host and path rule.
  6. In the Hosts field, enter *.
  7. Click on Edit.
  8. For the Action, select Redirect the client to different host/path.
  9. For the host redirect, enter a hostname to redirect to, or omit to keep the requested host.
  10. For Path redirect, select Full path redirect.
  11. For Path value, enter a path to redirect to, or omit to keep the requested path.
  12. For Strip query, click Enable.
  13. Click Save to finish configuring the host and path rule.
  14. Click Done to finish editing the URL map.
  15. Click Update to finish editing the load balancer.

Refer to this document for more information.

Alejandro Barone avatar
nr flag
Thanks for the answer!. But the problem is not making the redirect itself, is more about injecting a header con the 301 response (becuase the 301 redirect does not have the cors headers, so the request failed if its made on a browser)
Fariya Rahmat avatar
ve flag
Refer to this SO [link1](https://stackoverflow.com/questions/12883385/is-it-possible-to-modify-or-add-custom-http-headers-on-a-301-302-303-or-307-re) and [link2](https://stackoverflow.com/questions/26371461/with-redirection-301-what-i-wrote-to-http-header-is-gone-in-php). Let me know if this helps.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.