CORS 策略由于"找不到服务器"错误而受到阻止



我们已经为我们的应用程序配置了 kubernetes 环境。它有一个主服务器,两个从属服务器,nginx用作网络服务器。在访问我们应用程序的 url 时,收到 cors 错误。我已经按照 kubernetes 文档(https://kubernetes.io/docs/tasks/access-application-cluster/connecting-frontend-backend/( 来设置后端和 fronend 之间的连接,您可以在下面找到所有这些详细信息。这里没有提到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

新答案:

基本上,您需要在某些时候做出以下决定:

是否允许给定的源访问请求的内容?

您可以在反向代理、端点 Web 服务器或应用程序的逻辑中回答该问题。高级:将这些结合起来并在多个位置做出决策。请注意,不要无意中覆盖以前设置的标头。

答案是肯定的吗?

然后,必须以包含以下内容的 URI 样式设置标头:

http[s]://<trusted_origin_domain>[:port] 

从您的问题来看,不清楚您在此时设置逻辑并相应地设置信息。

为简单起见,您可以从让nginx发送正确的标头开始。重要的是不要混淆发送到节点的标头和发送到客户端的标头。

如果在应用程序中具有 CORS 实现,则应通过环境或在构建步骤或类似阶段传递参数(受信任的源(。

选择一种允许您根据需要进行扩展的方式,同时尽可能少地消耗时间。

仔细研究您的具体问题似乎您在应用程序和nginx中的决策是重叠的。

关于 HTTPS 的旁注在通过互联网转发时,您可能会泄漏未加密的 http 流量,而无需进一步设置 DNS 和 VPN。

旧答案:

发送到浏览器的标头在您的情况下必须看起来完全符合以下条件:

Access-Control-Allow-Origin: http://172.16.198.102:32603

当您覆盖引用和源时,整个安全性不起作用:

proxy_set_header Origin "http://localhost:32603";
proxy_set_header Referer "http://localhost:32603";

删除这些。

您正在阻止相关 CORS 标头到达浏览器:

proxy_hide_header 'Access-Control-Allow-Origin';

也删除它。

相关内容

最新更新