-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add cors #144
Add cors #144
Conversation
@bporter2387 I have a couple of suggestions/improvements:
|
…rol-allow-headers
added to other server in configurator, removed 401, added access-cont…
Hey, thanks for the feedback! I corrected 2-5, however I'm unsure how to go about number 1 since add_header isn't allowed in the server block when inside an 'if' statement. Any suggestions there? |
@bporter2387 It is important to be able to properly add CORS to the Ingress controller, so thanks for your pull request! However, I've been thinking more about this feature and it doesn't look like the two annotations --
To be able to customize those items, the user will have to change the We can try to provide the necessary level of customization by adding additional annotations. However, I don't think it would be the best solution, because CORS requirements can be quite different and it is possible it would require adding more and more annotations to cover those requirements. As an alternative, I can suggest the following solution that utilizes the ConfigMaps resource and the Step 1. Create a configMap with the CORS configuration Step 1.1. Create a file named set $cors "true";
set $corsmethod "${cors}nonoptions";
if ($request_method = 'OPTIONS') {
set $corsmethod "${cors}options";
}
if ($corsmethod = "truenonoptions") {
add_header 'Access-Control-Allow-Origin' "$http_origin" always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT, DELETE, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,Keep-Alive,X-Requested-With,If-Modified-Since';
add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
}
if ($corsmethod = "trueoptions") {
add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT, DELETE, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,Keep-Alive,X-Requested-With,If-Modified-Since';
add_header 'Content-Length' 0;
add_header 'Content-Type' 'text/plain charset=UTF-8';
return 204;
} Step 1.2. Create a ConfigMap resource from
Step 2. Deploy the Ingress controller and mount the ConfigMap resource on the file system, using the following manifest: apiVersion: v1
kind: ReplicationController
metadata:
name: nginx-ingress-rc
labels:
app: nginx-ingress
spec:
replicas: 1
selector:
app: nginx-ingress
template:
metadata:
labels:
app: nginx-ingress
spec:
containers:
- image: nginx-ingress:0.8.1
imagePullPolicy: Always
name: nginx-ingress
ports:
- containerPort: 80
hostPort: 80
- containerPort: 443
hostPort: 443
args:
- -v=3
volumeMounts:
- name: config-volume
mountPath: /etc/nginx/custom-snippets/
volumes:
- name: config-volume
configMap:
name: cors-config Note that Step 3. Using the apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: cafe-ingress
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.org/location-snippets: "include /etc/nginx/custom-snippets/cors.conf;"
spec:
rules:
- host: cafe.example.com
http:
paths:
- path: /tea
backend:
serviceName: tea-svc
servicePort: 80
- path: /coffee
backend:
serviceName: coffee-svc
servicePort: 80 Create the Ingress resource. ...
location /tea {
proxy_http_version 1.1;
include /etc/nginx/custom-snippets/cors.conf;
... Using the ConfigMap resource and the Please let me know your thoughts. Does the ConfigMaps/location-snippet approach work for you? Do you see any disadvantages? |
I think this is a good solution. You're right, too many hard-coded values. Give me a few days and I'll see what I can come up with. |
After using the location snippets annotation (which I didn't know of prior to this PR), I like this approach much better as it allows for much better customization vs the code in this PR. I'll go ahead and close this out. Thanks for the tips! |
Thanks |
Is there any update here? (or) alternatives? |
@dolftax Alternatively, you can use the kubernetes/ingress NGINX controller, it provides an annotation for enabling CORS -- |
No description provided.