【问题标题】:CORS policy is blocking due to that getting server not found error由于未找到服务器错误,CORS 策略被阻止
【发布时间】:2020-09-13 14:58:56
【问题描述】:

我们已经为我们的应用程序配置了 kubernetes 环境。其中有一个主服务器,两个从服务器,nginx 用作网络服务器。在访问我们应用程序的 url 时,得到 cors 错误。我已按照 kubernetes 文档(https://kubernetes.io/docs/tasks/access-application-cluster/connecting-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

【问题讨论】:

    标签: nginx kubernetes kubernetes-ingress nginx-reverse-proxy


    【解决方案1】:

    新答案:

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

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

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

    答案是肯定的吗?

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

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

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

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

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

    选择一种方式,让您可以尽可能多地扩展,同时尽可能少地消耗时间。

    仔细研究您的具体问题

    您在应用程序和 nginx 中的决策似乎是重叠的。

    关于 HTTPS 的附注

    您在通过 Internet 转发时可能会泄漏未加密的 http 流量,而无需进一步设置 DNS 和 VPN。

    旧答案:

    在您的情况下,发送到浏览器的标头必须与此完全相同:

    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';
    

    也删除它。

    【讨论】:

    • 嗨 Daniel tq,我删除了 proxy_hide_header 'Access-Control-Allow-Origin';并添加了 add_header Access-Control-Allow-Origin *;仍然得到 CORS 的东西。
    • 请显示节点(客户端、代理、服务器)之间当前发送的标头(Origin 和 ACAO)。请记住:CORS 为用户(浏览器)而不是您的服务器增加了安全性。您不需要任何带有 CORS 的 add_header,因为标头无需配置即可传递。
    • @Andy 请使用格式并在您的问题中添加其他信息。这种方式不可读。只有 OriginAccess-Control-Allow-Origin 标头是相关的,您的评论中也没有:-)
    • Daniel 我已经添加了这些详细信息,请在上面找到。
    • @Andy 你不小心把信息放在了我的回答中,而不是你的问题中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    • 2021-04-16
    • 2018-02-26
    • 2019-11-19
    • 2020-09-12
    • 2019-08-04
    相关资源
    最近更新 更多