Kubernetes ingress domain redirect

Harsh Manvar picture Harsh Manvar · Nov 4, 2019 · Viewed 11.4k times · Source

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;
    }

Answer

Thanh Nguyen Van picture Thanh Nguyen Van · Nov 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.