问题描述
这与其他与cors相关的问题不同
我正在在部署在Digitalocean上的kubernetes上的微服务上运行节点backend-api。我已经阅读了与该问题有关的所有博客/论坛,却没有找到任何解决方案(特别是与Digitalocean有关的解决方案)。
我无法通过运行在' localhost:3000 '或kubernetes集群之外的任何地方的React应用连接到集群。
这给了我下面的错误:
Access to XMLHttpRequest at 'http://cultor.dev/api/users/signin'
from origin 'http://localhost:3000' has been blocked by
CORS policy: Response to preflight request doesn't pass access
control check: Redirect is not allowed for a preflight request.
kubernetes集群的负载均衡器正在监听“ cultor.dev” ,该服务器在 / etc / hosts 中设置为本地域。 我可以使用Postman使它正常工作!
注意: 我也尝试过使用cors软件包,它无济于事。另外,如果我在不需要的kubernetes集群中运行此react应用 ,也可以正常工作。
入口nginx配置(尝试使用官方网站上提到的注释):
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-service
## this tells ingress to pick up the routing rules mentioned in this config
annotations:
nginx.ingress.kubernetes.io/default-backend: ingress-nginx-controller
kubernetes.io/ingress.class: nginx
## tells ingress to check for regex in the config file
nginx.ingress.kubernetes.io/use-regex: 'true'
# nginx.ingress.kubernetes.io/enable-cors: 'true'
# nginx.ingress.kubernetes.io/cors-allow-methods: "PUT,GET,POST,OPTIONS"
# nginx.ingress.kubernetes.io/cors-allow-origin: "*"
# nginx.ingress.kubernetes.io/cors-max-age: 600
# certmanager.k8s.io/cluster-issuer: letsencrypt
# kubernetes.io/ingress.class: nginx
# service.beta.kubernetes.io/do-loadbalancer-enable-proxy-protocol: "true"
其中一个微服务配置(也尝试过cors软件包):
// APP SETTINGS
app.set('trust proxy',true);
app.use(json());
app.use((req,res,next) => {
res.header('Access-Control-Allow-Origin','*');
res.header('Access-Control-Allow-Headers','*');
res.header('Access-Control-Request-Headers','*');
if (req.method === "OPTIONS") {
res.header('Access-Control-Allow-Methods','*');
return res.status(200).json({});
}
next();
});
解决方法
为什么对cors设置发表评论?
nginx.ingress.kubernetes.io/configuration-snippet: |
add_header Access-Control-Allow-Origin $http_origin;
add_header Access-Control-Allow-Methods "POST,GET,OPTIONS";
add_header Access-Control-Allow-Credentials true;
与GitHub相同的问题
,好吧,经过大量研究并在其他答案的帮助下,我进行了以下操作:
- 我将对后端的请求(从客户端)更改为 https ,而不是 http 。这解决了错误
Redirect is not allowed for a preflight request
- 我更改了配置 ingress nginx 的配置,以消除错误
multiple values in Access-Control-Allow-Origin
:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-service
annotations:
nginx.ingress.kubernetes.io/default-backend: ingress-nginx-controller
kubernetes.io/ingress.class: nginx
## tells ingress to check for regex in the config file
nginx.ingress.kubernetes.io/use-regex: "true"
nginx.ingress.kubernetes.io/configuration-snippet: |
add_header Access-Control-Allow-Methods "POST,OPTIONS";
add_header Access-Control-Allow-Credentials true;
nginx.ingress.kubernetes.io/enable-cors: "true"
nginx.ingress.kubernetes.io/cors-allow-methods: "PUT,POST,OPTIONS"
我希望它也能帮助其他人。
,预检请求不允许重定向。
似乎发生了重定向。没有足够的信息可以断定在哪里。我的猜测是您在Ingress上设置了TLS,并且http://cultor.dev/api/users/signin
被自动重定向到https
。
CORS policy: Response to preflight request doesn't pass access
control check: Redirect is not allowed for a preflight request.
飞行前CORS请求实际上是浏览器向您的服务器发出的单独请求,以检查响应中发回的访问控制标头是否将接受主请求。预检请求使用HTTP OPTIONS
方法。我敢打赌,如果您将req.method
记录为传入请求,您将看到OPTIONS
个请求进入。
当您收到这些预检请求之一时,应使用相应的Access-Control-Allow-Origin
和Access-Control-Allow-Headers
响应标题进行响应。