-1

I'm using Google Cloud Load Balancing service, and want to enable CORS for all subdomains. For example, I want to be able to run an XHR request from https://sub.mywebsite.example to https://www.mywebsite.example

Typically, I will do the below, but it does not work:

enter image description here

Ted Hoang
  • 41
  • 6
  • Does this answer your question? [Access-Control-Allow-Origin wildcard subdomains, ports and protocols](https://stackoverflow.com/questions/14003332/access-control-allow-origin-wildcard-subdomains-ports-and-protocols) – derpirscher Jul 08 '22 at 08:22
  • No, my case in Google Cloud Load Balancing is not any different app. Maybe i can't add any variable or regex in the header value field – Ted Hoang Jul 08 '22 at 09:24
  • 1
    doesnt't matter. The main point of that answer is: You must either specify `*` as Allow-Origin header or the exact `protocol://host:port`. Something like `https://*.myhost.com` is just not allowed ... – derpirscher Jul 08 '22 at 20:36

1 Answers1

2

As mentioned by @ derpirscher you must either specify * as Allow-Origin header or the exact protocol://host:port.

In your use case the response to the CORS request is missing the required Access-Control-Allow-Origin header, which is used to determine whether or not the resource can be accessed by content operating within the current origin.

You can also configure a site to allow any site to access it by using the * wildcard. You should only use this for public APIs. Private APIs should never use *, and should instead have a specific domain or domains set. In addition, the wildcard only works for requests made with the crossorigin attribute set to anonymous, and it prevents sending credentials like cookies in requests.

Access-Control-Allow-Origin: *

Ensure that the request has an Origin header and that the header value matches at least one of the Origins values in the CORS configuration. Note that the scheme, host, and port of the values must match exactly. Some examples of acceptable matches are as follows:

http://origin.example.com matches http://origin.example.com:80 (because 80 is the default HTTP port), but does not match https://origin.example.com, http://origin.example.com:8080, http://origin.example.com:5151, or http://sub.origin.example.com.

https://example.com:443 matches https://example.com but not http://example.com or http://example.com:443.

http://localhost:8080 only matches exactly http://localhost:8080 , not http://localhost:5555 or http://localhost.example.com:8080 .

Jyothi Kiranmayi
  • 2,090
  • 5
  • 14