14

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
  • 27,020
  • 6
  • 48
  • 102

4 Answers4

22

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
  • 10,292
  • 6
  • 35
  • 53
3

Used nginx ingress annotation

nginx.ingress.kubernetes.io/server-snippet: |
      location ~ /preview {
         rewrite /preview https://test.app.example.com$uri permanent;
      }
Harsh Manvar
  • 27,020
  • 6
  • 48
  • 102
1

I may be late for OP but I don't think the accepted solution completely answers the original question.

We can use NGINX's conditional URL rewrites offered by the ngx_http_rewrite_module to detect if a host matches the initial domain before redirecting to the new host. Here, the condition is based on the host name : $host (other vars are also available in NGINX, for a more advanced configuration).

nginx.ingress.kubernetes.io/server-snippet: |
  if ($host = "<OLD_HOSTNAME>") {
    rewrite /preview $scheme://<NEW_HOSTNAME>$uri permanent;
  }

Also, and I'm in accordance with OP's comment on that point, I would not recommend using nginx.ingress.kubernetes.io/configuration-snippet for such custom configuration, as this kind of snippet would add the custom configuration into the location block of each ingress rule.

Instead, if we still want to use annotation snippets, use a nginx.ingress.kubernetes.io/server-snippet to add a configuration that would apply to the whole NGINX instance. Also, it could lead to unexpected results.

Fantom974
  • 81
  • 3
0

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
  • 2,044
  • 1
  • 7
  • 18