简体   繁体   中英

Redirect to url from load balancer without CORS error

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.

After a long time searching for a solution I finally got to a "working" conclusion.

-Take this with a grain of salt-, but if you want that a google's load balancer inject some headers (specially cors headers) on a request thats redirected to another domain, then you are out of luck . Even with the new LB version it seems that is not possible (at the moment, am sure this feature will release in some time). Maybe there is a way of doing it, but neither the docs nor the api seems to tell you how.

Then you just "can't" do this?. Well, no, the classic load balancer has something call an Inte.net NEG which is like having "external backends" you can point to, so I just create various NEGs to meet my needs and attach them in the LB as backends. So I accomplish my example as follow:

  • If LB B has the domain lb-b.com and LB C has lb-c.com

    • Create 2 Global NEGs (Inte.net NEGs) with the full domain name as lb-b.com and lb-c.com respectibly
  • Then create 2 backend services on LB A with each one associated with each NEG.

  • Finally select Advanced host and path rule and create one Path rule for each LB, for example for LB B :

    • Create a path rule of /pathB/*
    • Select the option Route traffic to a single backend
    • Use path prefix as / (if you want to "remove" the pathB prefix on the forward)
    • Select the backend service you previously create corresponding to lb-b.com

Sharing this information with documentation and guidance as a workaround for handling and enabling CORS.

Another thing that I can think of for this scenario is by using cloud function for redirects and CORS headers. You can use this link as a guidance for this scenario. Let me also share another link where you can see different code sample.

To use a reverse proxy in front of the load balancer that can handle the CORS header and the redirecting request we can use Apache or Nginx. For this scenario we can follow this link as a guidance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM