Rewrite only specific route with nginx Ingress

◇◆丶佛笑我妖孽 提交于 2021-02-07 07:00:22

问题


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

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/...).

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.


回答1:


You have two options:

a) Change the port of the API and have it serve / on that port.

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.

Either way, you'll have your resources at "myapp.westeurope.cloudapp.azure.com/api/v1/myresource".



来源:https://stackoverflow.com/questions/51820476/rewrite-only-specific-route-with-nginx-ingress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!