gpt4 book ai didi

nginx - 如何使用Nginx捕获组将前缀附加到重写规则

转载 作者:行者123 更新时间:2023-12-02 12:36:13 25 4
gpt4 key购买 nike

我目前正在尝试在kubernetes入口中重写四个路径。

这三个都完成了,但是静态文件路径有些棘手。

我的前端路径是/$1-> /(.*) web-front-cluster-ip-service

我还需要重写所有/img/$1-> /img/(.*) apollo-api-cluster-ip-service

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: servers-ingress-service
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
rules:
- http:
paths:
- backend:
serviceName: apollo-api-cluster-ip-service
servicePort: 4000
path: /apollo_api/?(.*)
- backend:
serviceName: rest-api-cluster-ip-service
servicePort: 5000
path: /rest/?(.*)
- backend:
serviceName: web-front-cluster-ip-service
servicePort: 6000
path: /?(.*)
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: statics-ingress-service
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /img/$1
spec:
rules:
- http:
paths:
- backend:
serviceName: apollo-api-cluster-ip-service
servicePort: 4000
path: /img/?$(.*)

我当前的设置如上所示,但由于未知原因,我无法到达第二条路线配置的路线。

所以我想做的是将两条路由组合在一起,并提供前缀以捕获与 /img匹配的组

如果我做
      - backend:
serviceName: apollo-api-cluster-ip-service
servicePort: 6000
path: /?(.*)

当我需要任何不以 apollo-api开头的路由时,它将所有内容路由到 /img
所以我需要下面的东西
      - backend:
serviceName: apollo-api-cluster-ip-service
servicePort: 4000
path: /img?(.*) <- somehow add /img to $(.*) because currently it rewrites `/img/static.png` -> `/static.png` but I need `/img/static.png` -> `/img/static.png`.

任何帮助,将不胜感激。

最佳答案

Rewrite Target documentation中所述:

!!! attention Starting in Version 0.22.0, ingress definitions usingthe annotation nginx.ingress.kubernetes.io/rewrite-target are notbackwards compatible with previous versions. In Version 0.22.0 andbeyond, any substrings within the request URI that need to be passedto the rewritten path must explicitly be defined in a capture group.

!!! note Captured groups are saved in numbered placeholders, chronologically, in the form $1, $2 ... $n. These placeholders can beused as parameters in the rewrite-target annotation.


因此,我想您的第二次入侵应如下所示:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: statics-ingress-service
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /img/$2
spec:
rules:
- http:
paths:
- backend:
serviceName: apollo-api-cluster-ip-service
servicePort: 4000
path: /img(/|$)(.*)
在这种情况下, $2将反射(reflect) (.*)

关于nginx - 如何使用Nginx捕获组将前缀附加到重写规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55627872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com