I’m serving my static pages from Firebase.
I have an API service running on GKE cloud.
Both of the services under the same domain (example.com and api.example.com ),
and use the same protocol (https).
When I try to access the api.example.com I get access-control-allow-origin rejection.
How can I configure GKE to enable CORS from the same domain?
If you want to enable CORS at the ingress level you can do so within the spec using annotations.
name: $HOST
annotations:
kubernetes.io/ingress.class: "nginx"
#
# authentication
#
# nginx.ingress.kubernetes.io/auth-type: basic
# nginx.ingress.kubernetes.io/auth-secret: nginx-ingress-basic-auth
# nginx.ingress.kubernetes.io/auth-realm: "Authentication Required - foo"
#
# enable CORS
#
# nginx.ingress.kubernetes.io/enable-cors: "true"
# nginx.ingress.kubernetes.io/cors-allow-methods: "PUT, GET, POST, OPTIONS"
# nginx.ingress.kubernetes.io/cors-allow-origin: "https://onlythisdomaincanuseme.com"
# nginx.ingress.kubernetes.io/cors-allow-credentials: "true"
#
spec:
tls:
- hosts:
- $HOST
If you are referring only to firebase (though I don’t see the relation to GKE) then you need to tell your express app to use CORS:
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.setHeader('Access-control-Allow-Headers', 'Content-Type,Authorization');
next();
});
2 Likes
Hi,
I’m having a similar issue on GKE (but without using firebase). The answer here seems to provide a config for Ingress (whatever that is)… but it doesn’t explain what Ingress is nor whether it’s something that needs to be installed or if it comes standard with a gcloud kubernetes cluster or what? Also it doesn’t tell us what the yaml file shown is nor what to do with it… is it a Deployment yaml definition file?
Also is there no way to enable CORS without Ingress?