使用Nginx Ingress只重写特定的路由 [英] Rewrite only specific route with nginx Ingress
问题描述
后端有三个服务正在运行,并且入口路由的定义如下:
I have three services running in my backend and the Ingress routing is defined like this:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: myapp-ingress
annotations:
kubernetes.io/ingress.class: nginx
certmanager.k8s.io/cluster-issuer: letsencrypt-prod
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
tls:
- hosts:
- myapp.westeurope.cloudapp.azure.com
secretName: acme-crt-secret
rules:
- host: myapp.westeurope.cloudapp.azure.com
http:
paths:
- path: /
backend:
serviceName: myapp-mvc
servicePort: 80
- path: /api
backend:
serviceName: myapp-api
servicePort: 80
- path: /identity
backend:
serviceName: myapp-identity
servicePort: 80
问题在于 myapp-api 已经在侦听对/api/v1/myresource
的请求.在当前配置下, myapp-api 服务仅将请求提供给myapp.westeurope.cloudapp.azure.com/api/api/v1/myresource
(请注意.../api/api/...).
The problem is that myapp-api is already listening for requests to /api/v1/myresource
. With the current configuration, the myapp-api service only serves requests to myapp.westeurope.cloudapp.azure.com/api/api/v1/myresource
(please note the .../api/api/...).
myapp-api服务是否可以将请求提供给/api
,但可以将这些请求重写为服务的/
而无需创建另一个Ingress?因此,myapp-api应该将请求提供给myapp.westeurope.cloudapp.azure.com/api/v1/myresource
.
Is it possible to serve requests to /api
by the myapp-api service but rewriting these requests to /
for the service without creating another Ingress? So, myapp-api should serve requests to myapp.westeurope.cloudapp.azure.com/api/v1/myresource
.
推荐答案
您有两个选择:
a)更改API的端口,并在该端口上为其提供服务.
a) Change the port of the API and have it serve / on that port.
b)更改您的应用程序,使其可以在"/v1/myresource"上提供API,并通过Ingress为其提供URL的"api"部分.
b) Change your app so it will serve the API on "/v1/myresource" and give it the "api" part of the URL through the Ingress.
无论哪种方式,您都可以在"myapp.westeurope.cloudapp.azure.com/api/v1/myresource"中获得资源.
Either way, you'll have your resources at "myapp.westeurope.cloudapp.azure.com/api/v1/myresource".
这篇关于使用Nginx Ingress只重写特定的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!