Redirect specific sub directory to new url in Ingress

12/27/2018

I need to redirect a specific URL to a new one. I can use this code to redirect root domain but it doesn't work for subdirectory likes testdomain.com/specific-url

nginx.ingress.kubernetes.io/configuration-snippet: |
  if ($host = 'testdomain.com/specific-url') {
    return 308 https://testdomain.com/new-url;
  }
-- ali ajoudanian
kubernetes-ingress

1 Answer

12/28/2018

Try to use below approach I suggested here. And copy paste for future:

My advice is to use NGINX Plus Ingress Controller annotation functionality if you can afford it.

You can find official example here.

Example:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: cafe-ingress
  annotations:
    nginx.org/rewrites: "serviceName=tea-svc rewrite=/;serviceName=coffee-svc rewrite=/beans/"
spec:
  rules:
  - host: cafe.example.com
    http:
      paths:
      - path: /tea/
        backend:
          serviceName: tea-svc
          servicePort: 80
      - path: /coffee/
        backend:
          serviceName: coffee-svc
          servicePort: 80

Below are the examples of how the URI of requests to the tea-svc are rewritten (Note that the /tea requests are redirected to /tea/).

/tea/  ->  /
/tea/abc  ->  /abc

Below are the examples of how the URI of requests to the coffee-svc are rewritten (Note that the /coffee requests are redirected to /coffee/).

/coffee/  ->  /beans/
/coffee/abc  ->  /beans/abc
-- VKR
Source: StackOverflow