我的 k8s 集群中部署了一个 nginx-ingress 控制器。
此外,我还定义了服务路由的路径,这是负载均衡器的根路径。
应用程序(反应)工作正常,按照反应应用程序路由到页面。
URL随着React应用程序路由器而变化,但是当我刷新页面时,它给出错误404未找到。
我的想法: 重新加载页面时,它会调用入口控制器,但没有为 url 定义路径。所以没有找到错误(404)
我希望页面从 React 应用程序再次加载,但它显示 404。
我的入口:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: host-ingress
namespace: feeleat
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: admin.example.com
http:
paths:
- pathType: Prefix
path: /(.*)
backend:
service:
name: example-service
port:
number: 80
- host: api.example.ch
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: example-1-service
port:
number: 80
我认为你的重写是不正确的。从文档我认为所有请求都将被重写为
/
。
尝试在路由中指定捕获组,如下所示:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: host-ingress
namespace: feeleat
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
rules:
- host: admin.example.com
http:
paths:
- pathType: ImplementationSpecific
path: (/|$)(.*)
backend:
service:
name: example-service
port:
number: 80
- host: api.example.ch
http:
paths:
- pathType: ImplementationSpecific
path: (/|$)(.*)
backend:
service:
name: example-1-service
port:
number: 80
也就是说,我认为你实际上不需要重写。您正在将路径直接映射到服务。仅当后端应用程序需要不同的内容时才需要重写。 例如: 当后端应用程序期望
api.example.com/my-app/food
时,入口中的 api.example.com/food
路线需要像这样重写:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: host-ingress
namespace: feeleat
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
rules:
- host: api.example.com
http:
paths:
- pathType: ImplementationSpecific
path: /my-app(/|$)(.*)
backend:
service:
name: example-service
port:
number: 80
尝试完全删除它:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: host-ingress
namespace: feeleat
annotations:
kubernetes.io/ingress.class: nginx
spec:
rules:
- host: admin.example.com
http:
paths:
- pathType: Prefix
path: /
backend:
service:
name: example-service
port:
number: 80
- host: api.example.ch
http:
paths:
- pathType: Prefix
path: /
backend:
service:
name: example-1-service
port:
number: 80