<p>I'm trying to redirect <a href="http://www" rel="nofollow noreferrer">http://www</a>... and <a href="https://www" rel="nofollow noreferrer">https://www</a>... to https://... using ingress-nginx. How can I do that? </p>

<p>I've tried adding the following custom configuration using the annotation <code>nginx.ingress.kubernetes.io/server-snippet</code> and <code>nginx.ingress.kubernetes.io/configuration-snippet</code>:</p>

<pre><code># 1
if($host = "www.example.com") {
    return 308 https://example.com$request_uri;
}

# 2
server {
    server_name www.example.com;
    return 308 https://example.com$request_uri;
}

# 3
server_name www.example.com;
return 308 https://example.com$request_uri;
</code></pre>

<p>But I get an error in the nginx controller logs for #1:</p>

<pre><code>2019/12/07 20:58:47 [emerg] 48898#48898: unknown directive "if($host" in /tmp/nginx-cfg775816039:418
nginx: [emerg] unknown directive "if($host" in /tmp/nginx-cfg775816039:418
nginx: configuration file /tmp/nginx-cfg775816039 test failed
</code></pre>

<p>For #2 I get an error that the server block is not allowed at that position and using #3 leads to infinite redirects. My ingress yaml looks like this:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-nginx
  annotations:
    kubernetes.io/ingress.class: "nginx"
    kubernetes.io/ingress.global-static-ip-name: "example-com"
    nginx.ingress.kubernetes.io/rewrite-target: "/"
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
    nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
    nginx.ingress.kubernetes.io/proxy-read-timeout: "86400s"
    nginx.ingress.kubernetes.io/proxy-send-timeout: "86400s"
    nginx.ingress.kubernetes.io/proxy-body-size: "100m"
    nginx.ingress.kubernetes.io/limit-rps: "20"
    nginx.ingress.kubernetes.io/client-max-body-size: "100m"
    nginx.ingress.kubernetes.io/configuration-snippet: |
      # see above
spec:
  tls:
  - hosts:
      - example.com
    secretName: certificate-secret
  rules:
  - host: sub.example.com
    http:
      paths:
      - backend:
          serviceName: service-sub
          servicePort: 1234
# more subdomains here
  - host: example.com
    http:
      paths:
      - backend:
          serviceName: service-example
          servicePort: 1235
  - host: "*.example.com"
    http:
      paths:
      - backend:
          serviceName: service-example-wildcard
          servicePort: 1236
</code></pre>

<p>I've also tried setting the <code>nginx.ingress.kubernetes.io/from-to-www-redirect: "true"</code> annotation, but that leads to a different error:</p>

<pre><code>2019/12/07 21:20:34 [emerg] 51558#51558: invalid server name or wildcard "www.*.example.com" on 0.0.0.0:80
nginx: [emerg] invalid server name or wildcard "www.*.example" on 0.0.0.0:80
nginx: configuration file /tmp/nginx-cfg164546048 test failed
</code></pre>

<p>Ok I got it. The missing space after <code>if</code> fixed it. Thank you mdaniel :)
Here is a working configuration that redirects anything to <code>https://...</code> without <code>www</code>:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-nginx-integration
  namespace: integration
  annotations:
    kubernetes.io/ingress.class: "nginx"
    kubernetes.io/ingress.global-static-ip-name: "example-com"
    nginx.ingress.kubernetes.io/rewrite-target: "/"
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
    nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
    nginx.ingress.kubernetes.io/proxy-read-timeout: "86400s"
    nginx.ingress.kubernetes.io/proxy-send-timeout: "86400s"
    nginx.ingress.kubernetes.io/proxy-body-size: "100m"
    nginx.ingress.kubernetes.io/limit-rps: "20"
    nginx.ingress.kubernetes.io/client-max-body-size: "100m"
    nginx.ingress.kubernetes.io/configuration-snippet: |
      if ($host = "www.example.com") {
          return 308 https://example.com$request_uri;
      }
spec:
  tls:
  - hosts:
      - example.com
    secretName: certificate-integration-secret
  rules:
  - host: subdomain.example.com
    http:
      paths:
      - backend:
          serviceName: service-emviwiki
          servicePort: 4000
  # ... more rules, NO www here

</code></pre>
