Redirect from/to www with nginx-ingress

Solution 1:

This is a community wiki answer. Feel free to expand it.

In order to make it work you also need to use the nginx.ingress.kubernetes.io/configuration-snippet annotation. For example:

nginx.ingress.kubernetes.io/configuration-snippet: |
  if ($host = 'example.com' ) {
    rewrite ^ https://www.example.com$request_uri permanent;
  }

As already mentioned by @apoorvakamath in the comments, you can refer to this guide for a step by step detailed example:

The snippet is the clever aspect fo the solution. It allows you to add dynamic configuration to the ingress controller. We use it to detect the use of the non-www form of the URL and then simply issue a re-write that is pushed out (and back via your domain) to the www form.