我在两个不同的Pod上运行React前端和Nodejs REST API作为后端,我为两个Pod添加了nginx入口。但是,当我尝试从前端访问nodejs时,我得到了503。我尝试了许多解决方案,但没有一个起作用。
我已经尝试在nodejs入口中给出以下注释
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-allow-credentials: "true"
下面是我用于前端和后端nodejs api的入口文件
backend-ingress.yml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: coreapi-ingress
annotations:
kubernetes.io/ingress.class: 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-allow-headers: "Access-Control-Allow-Origin"
nginx.ingress.kubernetes.io/cors-allow-credentials: "true"
spec:
rules:
- host: backend-api.com
http:
paths:
- path: /
backend:
serviceName: backend-api
servicePort: 80
tls:
- secretName: generic
hosts:
- backend-api.comm
frontend-ingress.yml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: exchange-ingress
annotations:
kubernetes.io/ingress.class: nginx
spec:
rules:
- host: front-end.com
http:
paths:
- path: /
backend:
serviceName: webserver
servicePort: 80
tls:
- secretName: generic
hosts:
- front-end.com
从前端应用程序调用后端时出现以下错误
Access to XMLHttpRequest at 'https://backend-api.com/api/v1/user/login' from origin 'https://front-end.com' 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.
我注意到POST请求被重定向到OPTIONS
Request URL: https://backend-api.com/api/v1/user/login
Request Method: OPTIONS
Status Code: 503
Remote Address: 104.25.214.4:443
Referrer Policy: no-referrer-when-downgrade
Request Headers
Provisional headers are shown
Access-Control-Request-Headers: content-type
Access-Control-Request-Method: POST
Origin: https://front-end.com
Referer: https://front-end.com/login
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36