Search code examples
kubernetesnginxkubernetes-ingressnginx-ingress

Redirect sub-paths to different destinations in kubernetes nginx ingress


I am trying to redirect traffic from an older version of my site to the newer version of the site. Although, I have these 2 specific requirements. I need:

  1. old.mysite.com/ to be redirected to new.mysite.com/homepage.
  2. All the other sub paths to be redirected as is to the new site. Ex: old.mysite.com/this/sub/path should be redirected to new.mysite.com/this/sub/path

I am using the kubernetes nginx ingress controller and I am currently using configuration-snippets to redirect all the traffic from old to new site like this:

nginx.ingress.kubernetes.io/configuration-snippet: |
  if ($host = "old.mysite.com")
  {
    return 301 https://new.mysite.com$request_uri;
  }

Is there a way to modify this to fit my requirement or is there a different way to approach this?


Solution

  • You can do this by combining server snippet along with your configuration snippet.

    This will match to the default / URI and do the redirection.

    
        nginx.ingress.kubernetes.io/server-snippet: |
         location = / {
          rewrite ^ https://new.mysite.com/homepage permanent;
          break;
         }