Kubernetes ingress domain redirect

11/4/2019

i want to redirect domain in nginx ingress kubernete.

https://test.example.io/preview/qLxiVcDGxCaQ134650121853FTg4

if in url preview comes change domain redirect

https://test.app.example.io/preview/qLxiVcDGxCaQ134650121853FTg4

what i was trying

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    certmanager.k8s.io/cluster-issuer: staging
    nginx.ingress.kubernetes.io/rewrite-target: /
    kubernetes.io/ingress.class: nginx
  name: staging-ingress
spec:
  rules:
  - host: test.example.io
    http:
      paths:
      - path: /
        backend:
          serviceName: service-1
          servicePort: 80
      - path: /preview/*
        backend:
          url: 
          serviceName: service-2
          servicePort: 80
  tls:
  - hosts:
    - test.example.io
    secretName: staging

for simple nginx block is like

location ~ /preview
    {
      rewrite /preview https://test.app.example.com$uri permanent;
    }
-- Harsh Manvar
docker
kubernetes
kubernetes-ingress
nginx-ingress

3 Answers

11/4/2019

Used nginx ingress annotation

nginx.ingress.kubernetes.io/server-snippet: |
      location ~ /preview {
         rewrite /preview https://test.app.example.com$uri permanent;
      }
-- Harsh Manvar
Source: StackOverflow

11/4/2019

My logic thinking, try it :

metadata:
      annotations:
        nginx.ingress.kubernetes.io/configuration-snippet: |
         rewrite /preview https://test.app.example.com$uri permanent;

spec:
      rules:
      - host: test.example.io
        http:
          paths:
          - path: /
            backend:
              serviceName: service-1
              servicePort: 80
      - host: test.app.example.io
        http:
          paths:
          - path: /preview/*
            backend:
              serviceName: service-2
              servicePort: 80

Hope it works !

On code above: You should not access using: https://test.app.example.io/preview/ (It just be redirected link ) at all.

-- Thanh Nguyen Van
Source: StackOverflow

11/4/2019

Try this -

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    certmanager.k8s.io/cluster-issuer: staging
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    kubernetes.io/ingress.class: nginx
  name: staging-ingress
spec:
  rules:
  - host: test.example.io
    http:
      paths:
      - path: /(.*)
        backend:
          serviceName: service-1
          servicePort: 80
      - path: /preview/(.*)
        backend: 
          serviceName: service-2
          servicePort: 80
  tls:
  - hosts:
    - test.example.io
secretName: staging

You can refer this too, representing synonymous kind of issue

-- Tushar Mahajan
Source: StackOverflow