I had the need to have my application running in a kubernetes cluster using nginx ingress proxy. And also have a bunch of redirect domains to point to the main appplication domain. At first I had some troubles to understand how to create a redirect url that didn’t collide with the application routes. As it turns out you should see each ingress configration in kubernetes as it’s own nginx server configuration or virtualhost in apache terms. So my solution became to create a separate ingress configuration with the redirect rule and associate all my domains I wanted to redirect to the main application. Overall it became quite simple and easy once I got the hang of how ingress scopes nginx resources.

How my setup became without the whole setup of our application parameters and domains:

---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: application-ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: primarydomain.tld
    http:
      paths:
      - path: /
        backend:
          serviceName: applicationServiceName
          servicePort: 9080

---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: redirect-ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
    ingress.bluemix.net/server-snippets: |
      rewrite ^ http://primarydomain.tld$request_uri permanent;
spec:
  rules:
  - host: misspelled.tld
    http:
      paths:
      - path: /
        backend:
          serviceName: applicationServiceName
          servicePort: 9080
  - host: alternativeapplication.tld
    http:
      paths:
      - path: /
        backend:
          serviceName: applicationServiceName
          servicePort: 9080