Forum Discussion

michaelgardner's avatar
michaelgardner
Icon for Altocumulus rankAltocumulus
Jun 02, 2022

How to rewrite a path to a backend service dropping the prefix and passing the remaining path?

Hello,
I am not sure whether my posting is appropriate in this area, so please delete it if there is a violation of posting rules...

This must be a common task, but I cannot figure out how to do the following fanout rewrite in our nginx ingress:
http://abcccc.com/httpbin/anything ->   /anything (the httpbin backend service)

 

When I create the following ingress with a path of '/' and send the query, I receive a proper response.

curl -I -k http://abczzz.com/anything

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: mikie-ingress
  namespace: mikie
spec:
  ingressClassName: nginx
  rules:
    - host: abczzz.com
      http:
        paths:
          - path: / 
            pathType: Prefix
            backend:
              service:
                name: httpbin-service
                port:
                  number: 8999

 

 

 

What I really need is to be able to redirect to different services off of this single host, so I changed the ingress to the following, but the query always fails with a 404. Basically, I want the /httpbin to disappear and pass the path onto the backend service, httpbin.

curl -I -k http://abczzz.com/httpbin/anything

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: mikie-ingress
  namespace: mikie
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  ingressClassName: nginx
  rules:
    - host: abczzz.com
      http:
        paths:
          - path: /httpbin(/|$)(.*)
            pathType: Prefix
            backend:
              service:
                name: httpbin-service
                port:
                  number: 8999

 

Thank you for your time and interest,

Mike