Reputation: 21
I have a kubernetes cluster with nginx-ingress and I try to redirect http://test.domain.com
to http://www.test.domaine.com
with nginx.ingress.kubernetes.io/from-to-www-redirect annotation.
But it doesn't seem to work.
My ingress resource:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: app-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/from-to-www-redirect: "true"
spec:
rules:
- host: www.test.domain.com
http:
paths:
- path: /
backend:
serviceName: app
servicePort: 8080
- host: test.domain.com
http:
paths:
- path: /
backend:
serviceName: app
servicePort: 8080
I have tried many configurations but I can't make it work. The nginx.ingress.kubernetes.io/from-to-www-redirect annotation has no effect!
Upvotes: 2
Views: 3394
Reputation: 6987
Something like this should work:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: app-ingress
annotations:
nginx.ingress.kubernetes.io/from-to-www-redirect: "true"
cert-manager.io/cluster-issuer: letsencrypt
spec:
ingressClassName: nginx
rules:
- host: www.test.domain.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: app
port:
number: 8080
tls:
- hosts:
- www.test.domain.com
- test.domain.com
secretName: your-cert-secret
If you don't use TLS, you can safely remove the tls
section and the line mentioning cert-manager
. Also, the line mentioning cert-manager
only works if you've configured a certificate with Cert Manager.
Note that since the question was asked, the APIs have changed a bit, which is why the apiVersion
is now networking.k8s.io/v1
and the ingress class is specified in ingressClassName
, not the ingress.class
annotation.
Upvotes: 0
Reputation: 63
kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:
name: webui-ingress
namespace: default
annotations:
kubernetes.io/ingress.allow-http: 'false'
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/affinity: cookie
nginx.ingress.kubernetes.io/configuration-snippet: |
more_set_headers "X-Frame-Options: SAMEORIGIN";
more_set_headers "X-Xss-Protection: 0";
more_set_headers "Referrer-Policy: use strict-origin-when-cross-origin";
if ($host = 'test.domain.com' ) {
return 301 https://www.test.domain.com ;
}
nginx.ingress.kubernetes.io/force-ssl-redirect: 'true'
nginx.ingress.kubernetes.io/proxy-body-size: 10m
nginx.ingress.kubernetes.io/rewrite-target: /
nginx.ingress.kubernetes.io/session-cookie-hash: sha1
nginx.ingress.kubernetes.io/session-cookie-name: route
nginx.ingress.kubernetes.io/ssl-passthrough: 'true'
nginx.ingress.kubernetes.io/ssl-redirect: 'true'
spec:
tls:
- hosts:
- test.domain.com
secretName: test-tls
- hosts:
- www.test.domain.com
secretName: www-tls
rules:
- host: test.domain.com
http:
paths:
- path: /
pathType: ImplementationSpecific
backend:
service:
name: webui
port:
number: 8022
- host: www.test.domain.com
http:
paths:
- path: /
pathType: ImplementationSpecific
backend:
service:
name: webui
port:
number: 8022
once the below annotation is added your domain will redirect to www
nginx.ingress.kubernetes.io/configuration-snippet: |
more_set_headers "X-Frame-Options: SAMEORIGIN";
more_set_headers "X-Xss-Protection: 0";
more_set_headers "Referrer-Policy: use strict-origin-when-cross-origin";
if ($host = 'test.domain.com' ) {
return 301 https://www.test.domain.com ;
}
Upvotes: 0
Reputation: 13858
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.
Upvotes: 2