我们的生态系统中有许多微服务,其中两个处理用户数据:
用户服务->
- 发布/用户
- 获取 /users/[[:alnum:]]+
文件服务 ->
- POST /users/[[:alnum:]]+/documents
- GET /users/[[:alnum:]]+/documents/[[:alnum:]]+
因此我想在 Ingress 中定义它:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: {{.Release.Name}}
annotations:
kubernetes.io/ingress.class: "nginx"
kubernetes.io/tls-acme: "true"
ingress.kubernetes.io/enable-cors: "false"
ingress.kubernetes.io/ssl-redirect: "true"
labels:
chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
app: "{{ template "fullname" . }}"
heritage: "{{ .Release.Service }}"
release: "{{ .Release.Name }}"
tier: frontend
spec:
tls:
- secretName: {{ template "tls.fullname" . }}
hosts:
- "staging.ourhost.com"
rules:
- host: "staging.ourhost.com"
http:
paths:
- path: "/users/[[:alnum:]]+"
backend:
serviceName: {{ .Values.api.services.user_service | quote }}
servicePort: 8080
- path: "/users/[[:alnum:]]+/documents"
backend:
serviceName: {{ .Values.api.services.document_service | quote }}
servicePort: 8888
这导致在 ngix.conf 中设置以下内容:
location /users/[[:alnum:]]+ {
...
}
location /users/[[:alnum:]]+/documents{
...
}
即正则表达式未解析。
我尝试将其拆分为单独的入口并使用注释
ingress.kubernetes.io/rewrite-target: "/users/$1/documents"
但这导致了 nginx.conf 中的以下内容
location ~* ^/users/[[:alnum:]]+/documents/(?<baseuri>.*) {
set $proxy_upstream_name "default-dev-document_service-8888";
...
rewrite /users/[[:alnum:]]+/documents/(.*) /users/$1/documents/$1 break;
proxy_pass http://default-dev-document_service-8888;
}
问题(最后):
- 为什么总是附加 $1?这对我来说似乎很奇怪
- 有人知道使路径与正则表达式一起使用的另一种方法吗?
任何建议将不胜感激