由于找不到服务器错误,CORS 策略被阻止
CORS policy is blocking due to that getting server not found error
我们已经为我们的应用配置了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
新答案:
基本上你需要在某个时候做出以下决定:
Is the given Origin allowed to access the requested content?
您可以在反向代理、端点网络服务器或应用程序逻辑中回答该问题。高级:结合这些并在多个地方做出决定。请小心,不要无意中覆盖之前设置的 headers。
答案是吗?
然后,header 必须设置为包含以下内容的 URI 样式:
http[s]://<trusted_origin_domain>[:port]
从你的问题来看,不清楚你在哪一点设置了逻辑并相应地设置了信息。
为简单起见,您可以先让 nginx 发送正确的 header。重要的是不要混淆发送到节点的 header 和发送到客户端的 header。
如果您在应用程序中实现了 CORS,则应通过环境或在构建步骤或类似阶段传递参数(可信来源)。
选择一种方式,使您可以根据需要进行扩展,同时花费尽可能少的时间。
仔细研究您的具体问题
看来你在应用程序和nginx中的决策是重叠的。
关于 HTTPS 的旁注
在没有进一步的 DNS 和 VPN 设置的情况下通过 Internet 转发时,您可能会泄露未加密的 http 流量。
旧答案:
发送到浏览器的 header 必须与您的情况完全一样:
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 header 到达浏览器:
proxy_hide_header 'Access-Control-Allow-Origin';
也将其删除。
我们已经为我们的应用配置了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
新答案:
基本上你需要在某个时候做出以下决定:
Is the given Origin allowed to access the requested content?
您可以在反向代理、端点网络服务器或应用程序逻辑中回答该问题。高级:结合这些并在多个地方做出决定。请小心,不要无意中覆盖之前设置的 headers。
答案是吗?
然后,header 必须设置为包含以下内容的 URI 样式:
http[s]://<trusted_origin_domain>[:port]
从你的问题来看,不清楚你在哪一点设置了逻辑并相应地设置了信息。
为简单起见,您可以先让 nginx 发送正确的 header。重要的是不要混淆发送到节点的 header 和发送到客户端的 header。
如果您在应用程序中实现了 CORS,则应通过环境或在构建步骤或类似阶段传递参数(可信来源)。
选择一种方式,使您可以根据需要进行扩展,同时花费尽可能少的时间。
仔细研究您的具体问题看来你在应用程序和nginx中的决策是重叠的。
关于 HTTPS 的旁注在没有进一步的 DNS 和 VPN 设置的情况下通过 Internet 转发时,您可能会泄露未加密的 http 流量。
旧答案:
发送到浏览器的 header 必须与您的情况完全一样:
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 header 到达浏览器:
proxy_hide_header 'Access-Control-Allow-Origin';
也将其删除。