Reaction w/Kubernetes Deployment API出现CORS错误 [英] React w/ Kubernetes deployed API giving cors error
本文介绍了Reaction w/Kubernetes Deployment API出现CORS错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这与其他CORS相关问题不同
我在DigitalOcean上部署的Kubernetes上的微服务上运行我的节点后端API。我确实阅读了所有与此问题相关的博客/论坛,但没有找到任何解决方案(特别是与DigitalOcean相关的那些)。
我无法通过在"localhost:3000"或Kubernetes群集之外的任何位置上运行的Reaction应用程序连接到群集。
它给出以下错误:
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中设置为本地域。 我可以使用邮递员使其正常工作!
注意: 我也试过使用CORS软件包,但无济于事。而且,如果我在Kubernetes群集内运行此Reaction应用,它也可以正常工作,这是我不想要的。
Inress nginx config(尝试使用官网提到的注释):
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();
});
推荐答案
好的,经过大量研究并借助其他答案,我做了以下工作:
- 我将后台(客户端)的请求改成了https,而不是http。这修复了错误
Redirect is not allowed for a preflight request
- 我更改了配置入口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, GET, OPTIONS";
add_header Access-Control-Allow-Credentials true;
nginx.ingress.kubernetes.io/enable-cors: "true"
nginx.ingress.kubernetes.io/cors-allow-methods: "PUT, GET, POST, OPTIONS"
我希望它也能帮助别人。
这篇关于Reaction w/Kubernetes Deployment API出现CORS错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文