简体   繁体   English

Kubernetes Nginx 入口 - 重写动态 URL

[英]Kubernetes Nginx Ingress - Rewrite Dynamic URL

Here i have kubernetes running on minikube and it has 2 services backend and frontend .在这里,我有 kubernetes 在 minikube 上运行,它有 2 个服务backendfrontend And i have installed ingress addon for minikube.我已经为 minikube 安装了入口插件。

I wanted to rewrite whenever the frontend request the path url is /users/{user-id} it goes to backend service with the same url dynamically.每当前端请求路径 url 是/users/{user-id}时,我都想重写它动态地使用相同的 url 进入backend服务。

Let say the request is /users it rewrite to backend service to /users , /users/1 it goes to /users/1 .假设请求是/users它将backend服务重写为/users/users/1它转到/users/1

I tried to read nginx manual found some special expression i don't understand.我试图阅读nginx 手册发现一些我不明白的特殊表达。

This is my ingress configuration.这是我的入口配置。

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-be
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /users/$1
spec:
  ingressClassName: nginx
  rules:
    - host: backend
      http:
        paths:
          - path: /users/(.*)
            pathType: Prefix
            backend:
              service:
                name: backend
                port:
                  number: 5000

My configuration seems fine when the request is /users/{users-id} but when the request is /users , my ingress won't redirect me to '/users' in backend service.当请求是/users/{users-id}时,我的配置似乎很好,但是当请求是/users时,我的入口不会将我重定向到backend服务中的“/users”。

When i tested with curl, this is the output.当我用 curl 测试时,这是 output。

curl http://backend/users/3

{"id":3,"name":"Sofia","email":"sofia@gmail.com","gender":"Female","createdAt":"2022-04-05T15:46:46.000Z","updatedAt":"2022-04-05T15:46:46.000Z"}%
curl http://backend/users

<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx</center>
</body>
</html>

What am i missing here?我在这里想念什么?

Thank you in advance for your help!预先感谢您的帮助!

Update!更新!

Andreas in the comment suggest me to use /users/ and it works!评论中的 Andreas 建议我使用/users/并且它有效!

Then i decided to modified my ingress configuration.然后我决定修改我的入口配置。

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-be
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /users/$2
spec:
  ingressClassName: nginx
  rules:
    - host: backend
      http:
        paths:
          - path: /users(/|$)(.*)
            pathType: Prefix
            backend:
              service:
                name: backend
                port:
                  number: 5000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM