<p>I have a kubernetes setup that looks like this:</p>

<p>nginx ingress -> load balancer -> nginx app</p>

<p>after getting an SSL certificate for <strong>www</strong>.foo.com, I've installed it in my nginx ingress as a secret, and it works as expected - traffic to <code>www.foo.com</code> gets redirected to the <code>https</code> version instead, and browsers display a secure connection indicator. Great. </p>

<p>What <em>hasn't</em> been easy, however, is getting the ingress to redirect <strong>non-www</strong> traffic to the www version of the site. <s>I've tried using <code>kubernetes.io/from-to-www-redirect: "true"</code>, but it doesn't seem to do anything - navigating to <code>foo.com</code> doesn't redirect me to the <strong>www</strong> version of the url, but either takes me to an insecure version of my site, or navigates me to <code>default backend - 404</code> depending on whether i include <code>foo.com</code> as a host with it's own path in my ingress.</p>

<p>I have been able to set up a patchy redirect by adding the following to my actual application's nginx config -</p>

<pre><code>server {
  listen       80;
  server_name  foo.com;
  return       301 http://www.foo.com$request_uri;
}
</code></pre>

<p></s></p>

<p><strong>UPDATE:</strong> <code>from-to-www-redirect</code> DOES work; you just have to reference it with <code>nginx.ingress.kubernetes.io</code> rather than <code>kubernetes.io</code> as I was. But, this only works for <code>foo.com</code> - typing in <code>https://foo.com</code> explicitly causes browsers to display a security warning and no redirect to the proper URL of <code>https://www.foo.com</code> occurs. </p>

<p>Here's my current config for the nginx ingress itself:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: foo-https-ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/from-to-www-redirect: "true"
spec:
  rules:
    - host: www.foo.com
      http:
        paths:
          - backend:
              serviceName: foo-prod-front
              servicePort: 80
            path: /
  tls:
      - hosts:
          - www.foo.com
        secretName: tls-secret
</code></pre>

<p>You need to add the certificate for the domain you want to be redirected:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: foo-https-ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/from-to-www-redirect: "true"
spec:
  rules:
    - host: foo.com
      http:
        paths:
          - backend:
              serviceName: foo-prod-front
              servicePort: 80
            path: /
    - host: www.foo.com
      http:
        paths:
          - backend:
              serviceName: foo-prod-front
              servicePort: 80
            path: /
  tls:
      - hosts:
          - foo.com
          - www.foo.com
        secretName: tls-secret
</code></pre>

<p>I am not completely sure, whether <code>from-to-www-redirect</code> works with this setup, but you can replace it with the following lines, which do work:</p>

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