我们已经为我们的应用配置了kubernetes环境,其中有一个主站,两个从站,nginx作为web服务器使用。其中有一个主站,两个从站和nginx作为Web服务器。当访问我们的应用程序的url时,得到cors错误。我已经按照kubernetes document(https:/kubernetes.iodocstasksaccess-application-clusterconnecting-frontend-backend。)用于设置后台和前台之间的连接,你可以在下面找到所有这些细节。这里我没有提到yamls文件的全部细节,如果我遗漏了什么,请让我知道。
这是我得到的错误。
Access to XMLHttpRequest at 'http://andy.fin.com:9090/configuration/api/v1/configuration' from origin 'http://172.16.198.102:32603' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
#nginx configuration
upstream zuul {
server zuul;
}
location / {
proxy_pass http://andy.fin.com:9090/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto "http";
proxy_set_header Origin "http://localhost:32603";
proxy_set_header Referer "http://localhost:32603";
proxy_hide_header 'Access-Control-Allow-Origin';
}
apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend
spec:
strategy:
type: Recreate
selector:
matchLabels:
app: zuul
tier: frontend
replicas: 1
template:
metadata:
labels:
app: zuul
tier: frontend
spec:
containers:
- name: nginx
image: nginx
---
apiVersion: v1
kind: Service
metadata:
name: frontend
spec:
selector:
app: zuul
tier: frontend
ports:
- protocol: "TCP"
port: 80
targetPort: 80
type: LoadBalancer
apiVersion: apps/v1
kind: Deployment
metadata:
name: zuul-routing
spec:
selector:
matchLabels:
app: zuul
tier: backend
replicas: 1
template:
metadata:
labels:
app: zuul
tier: backend
spec:
containers:
- env:
---
apiVersion: v1
kind: Service
metadata:
name: zuul
spec:
selector:
app: zuul
tier: backend
ports:
- protocol: TCP
port: 9090
targetPort: http
基本上,你需要在某些时候做出以下决定:
给定的Origin是否允许访问请求的内容?
你可以在反向代理、endpoints webserver或应用程序的逻辑中回答这个问题。高级。把这些结合起来,在多个地方做决定。只是要小心,不要无意中覆盖之前设置的头文件。
答案是 "是 "吗?
那么,头必须设置为包含URI样式。
http[s]://<trusted_origin_domain>[:port]
从你的问题来看,并不清楚,这时你要设置逻辑,并设置相应的信息。
为了简单起见,你可以先让nginx发送正确的头。重要的是不要把发送到节点的头信息和发送到客户端的头信息混在一起。
如果你在应用中使用了CORS实现,你应该通过环境或在buildstep或类似的阶段传递参数(可信来源)。
选择一种允许你尽可能多的扩展的方式,同时消耗尽可能少的时间。
仔细观察你的具体问题看来你在应用程序和nginx中的决策是重叠的。
关于HTTPS的补充说明你在没有进一步设置DNS和VPN的情况下,通过互联网转发时可能会泄露未加密的http流量。
在你的情况下,发送到浏览器的头必须看起来像这样。
Access-Control-Allow-Origin: http://172.16.198.102:32603
因为你覆盖了Referer和Origin,整个安全系统就失效了。
proxy_set_header Origin "http://localhost:32603";
proxy_set_header Referer "http://localhost:32603";
移除这些.
你正在阻挡有关CORS头到达浏览器。
proxy_hide_header 'Access-Control-Allow-Origin';
把它也删掉.