December 1, 2018 · Kubernetes

Kubernetes & ingress-nginx: Performing HTTP Redirects

I've recently been doing a lot of work with Kubernetes (in fact, this whole blog now runs on Kubernetes, with more posts to follow). To avoid lots of load balancer charges, I've been using ingress-nginx. This allows me to have one load balancer for all of my web traffic (pointing to the ingress-nginx service) and offload traffic to the appropriate backend based on ingress definitions. The below diagram shows how this works.

ingress-nginx Diagram

This blog sits under the url https://benjamin.maynard.io. As you'll notice, this is a subdomain of maynard.io. I've always been keen to make sure that anyone who mistypes the URL of my blog (e.g. maynard.io or www.benjamin.maynard.io) still ends up on my site.

Previously, when using standard nginx. I have achieved this using Virtual Hosts that are configured to match the appropriate host headers. Once matched, the Virtual Host is configured to return a HTTP 301 redirect, which in turn redirects users to the correct URL of https://benjamin.maynard.io.

When I was planning the migration of this blog to Kubernetes, I wasn't initially sure on how to replicate this. I absolutely did not want to do this redirection at a container level, and so spent some time researching how to perform it at an ingress level.

Fortunately, it turns out that this is pretty simple, and can be performed by using a custom configuration snippet. The ingress-nginx documentation lists all of the annotations available. The one that was of interest was "nginx.ingress.kubernetes.io/configuration-snippet". This allows custom configuration to be added to nginx.

Once this was determined, it was just a case of adding the configuration snippet to my ingress definition:

You also need to ensure that your ingress spec contains a definition for the host you wish to redirect (see the full configuration example below). Once you have these two elements covered, you're good to go! Any users requesting the defined FQDN will be redirected.


As promised, below is an example of how a full ingress definition might look:

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket