React w/ Kubernetes 部署的 API 给出 ​​cors 错误

Kar*_*mar 8 node.js reactjs digital-ocean kubernetes microservices

这与其他 cors 相关问题不同

我正在部署在 Digitalocean 上的 kubernetes 上的微服务上运行我的节点后端 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.
Run Code Online (Sandbox Code Playgroud)

kubernetes 集群的负载均衡器正在监听“cultor.dev” ,它在/etc/hosts中设置为本地域。 我可以使用 Postman 让它工作!

注意: 我也尝试过使用 cors 包,但没有帮助。另外,如果我在我不想要的kubernetes 集群内运行这个 React 应用程序,它也可以正常工作。

Ingress nginx 配置(尝试使用官方网站上提到的注释):

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.
Run Code Online (Sandbox Code Playgroud)

微服务配置之一(也尝试过 cors 包):

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"
Run Code Online (Sandbox Code Playgroud)

Kar*_*mar 11

好的,经过大量研究并在其他答案的帮助下,我做了以下工作:

  1. 我将对后端的请求(从客户端)更改为https而不是http。这修复了错误Redirect is not allowed for a preflight request
  2. 我更改了配置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, 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"
Run Code Online (Sandbox Code Playgroud)

我希望它也能帮助其他人。


Som*_*ial 5

为什么 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;
Run Code Online (Sandbox Code Playgroud)

GitHub上有同样的问题