ingress nginx redirect from www to https

Kugel

I'm trying to redirect http://www... and https://www... to https://... using ingress-nginx. How can I do that?

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

# 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;

But I get an error in the nginx controller logs for #1:

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

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:

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

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

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
Kugel

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

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Nginx ingress resource - Redirect from to www (SSL doesn't work)

Nginx redirect everything to www https

How to redirect from http://www.* to https://* in Nginx?

Nginx: Redirect non-www to www https

Redirect Nginx HTTP/HTTPS www to non www

nginx Redirect non-www to www and https

EKS ingress-nginx and NLB with https redirect

kubernetes nginx ingress fails to redirect HTTP to HTTPS

Nginx redirect http://www and naked http/https to https://www

NGINX: redirect non-www https to https://www

nginx k8s ingress - forcing www AND https?

Nginx Redirect HTTP to HTTPS and WWW to Non-WWW

nginx redirect all non-WWW to HTTPS WWW

How to redirect www to non-www using certbot for HTTPS & nginx

NGINX redirect http to https and www. to non-www

301 Redirect in htaccess from NON WWW to https://www with single redirect

HTTPS redirect not working for default backend of nginx-ingress-controller

Kubernetes Nginx Ingress HTTP to HTTPS redirect via 301 instead of 308?

Helm chart nginx-ingress controller TCP redirect http to https

Redirect www https site to non-www https cause too many redirect in nginx

Redirect https to https://www

Nginx redirect all [http / www / https www] to [https non-www]

Keycloak redirect nginx ingress

Nginx ingress not redirecting to https

NGINX Redirect http to https and non-www to ww

Nginx 301 Redirect http (naked and www) to https www, plus wildcard subdomain to https

Is there no way to redirect from https://www.. to https://.. in Go?

ALB Ingress - Redirect Traffic from HTTP to HTTPS not working

HTTPS to HTTPS redirect Nginx

TOP Ranking

HotTag

Archive