Home > Mobile >  Q: How to rewrite single path among many with the ingress-nginx
Q: How to rewrite single path among many with the ingress-nginx

Time:09-17

I already have on my Ingress a lot of domains with so many paths as this is an environment with many microservices.

How can I edit my ingress in some way that when someone access to path /servicex it gets instead /serviceb for example

My current ingress is as follow (for simplicity I'm omitting some path from other hosts)

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: main-ingress
  annotations:
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
    nginx.ingress.kubernetes.io/force-ssl-redirect: "false"
    nginx.ingress.kubernetes.io/proxy-body-size: "100m"
    nginx.ingress.kubernetes.io/max-worker-connections: "0"
    nginx.ingress.kubernetes.io/max-worker-open-files: "0"
    nginx.ingress.kubernetes.io/client-header-buffer-size: "4k"
spec:
  tls:
  - hosts:
    - subdomain-a.domain.com
    - subdomain-b.domain.com
    - subdomain-c.domain.com
    - subdomain-d.domain.com
    - subdomain-e.domain.com
    secretName: domain-com-secret
  rules:
  - host: subdomain-a.domain.com
    http:
      paths:
        - path: /
          pathType: Prefix
          backend:
            service:
              name: default-service
              port:
                number: 80
        - path: /serviceb
          pathType: Prefix
          backend:
            service:
              name: b-service
              port:
                number: 80
        - path: /servicec
          pathType: Prefix
          backend:
            service:
              name: c-service
              port:
                number: 80
        - path: /serviced
          pathType: Prefix
          backend:
            service:
              name: d-service
              port:
                number: 80
        - path: /servicee
          pathType: Prefix
          backend:
            service:
              name: e-service
              port:
                number: 80
        - path: /servicee
          pathType: Prefix
          backend:
            service:
              name: e-service
              port:
                number: 80
        - path: /servicef
          pathType: Prefix
          backend:
            service:
              name: f-service
              port:
                number: 80
        - path: /serviceg
          pathType: Prefix
          backend:
            service:
              name: g-service
              port:
                number: 80

  - host: subdomain-b.domain.com
    < tons of other rules >

  - host: subdomain-c.domain.com
    < tons of other rules >

  - host: subdomain-d.domain.com
    < tons of other rules >

  - host: subdomain-e.domain.com
    < tons of other rules >

In other words we want to get access to the /serviceb if the user enters any of the following:

I have reviewed the rewrite option but its seems to me that if I add the annotation nginx.ingress.kubernetes.io/rewrite-target: /servicex/$2 for example and then try to do something like:

- path: /serviceb(/|$)(.*)
          pathType: Prefix
          backend:
            service:
              name: serviceb
              port:
                number: 80

Won't work because first, we already have that path taken and second I think that the annotation will apply to the whole thing and this will damage all the other routes.

Thanks in advance, any help would be appreciated

CodePudding user response:

NGINX ingress is almost the same as in the NGINX. In most of the cases, the built-in annotation helps you to configure the standard operation in NGINX.

However, if you have encountered any kind of customization like your case, you are also free to input the server or configuration snippet through these two annotations.

nginx.ingress.kubernetes.io/server-snippet and nginx.ingress.kubernetes.io/configuration-snippet.

Inside the snippet, you can do whatever you want like an ordinary NGINX.

Grabbing the configuration from your reply here for others' information.

nginx.ingress.kubernetes.io/configuration-snippet: |-
  rewrite ^(/servicex)(.*) /serviceb$2 last; 
  • Related