Hi,
Apologies is this is not the place for this question, but I can't seem to find an answer anywhere else. As the question states, I'm trying to redirect all traffic from example.com to www.example.com using an Ingress resource on Kubernetes. I've tried the Ingress Rewrite Annotation, but I can't get it to work. I'd like to avoid having to use and configure NGINX, since I already use e.g. loadbalancing on GKE and it works great.
Here is my Ingress (JSON format):
{
"apiVersion": "extensions/v1beta1",
"kind": "Ingress",
"metadata": {
"name": "example-staging",
"annotations": {
"ingress.kubernetes.io/rewrite-target": "/",
"kubernetes.io/ingress.global-static-ip-name": "example-static-ip"
}
},
"spec": {
"rules": [
{
"host": "www.example.nl",
"http": {
"paths": [
{
"path": "/",
"backend": {
"serviceName": "example-service",
"servicePort": 80
}
}
]
}
}
]
}
}
I can't find a proper example of rewriting all non-www to www subdomain, hence this question.
Hopefully you guys can help me out.
Thanks!
EDIT
Since this is an unsupported feature, I changed the issue into a request.
This doesn't seem to be a supported feature of the GCE loadbalancers right now.
@pieterlange So the alternative is to use NGINX then?
Yes
How would one go about redirecting from non www to www using the nginx-ingress controller? Having a hard time trying to figure this out. Not sure if its possible without editing the nginx.tmpl.
I would love to see the proposed solution by @aledbf in this comment. I think those annotations would cover this case as well. My case it a bit more specific where I need to redirect permanently to an entirely different domain. Currently I use a trick by explicitly adding an NGINX rewrite rule to the Ingress configuration using a Configuration Snippet:
ingress.kubernetes.io/configuration-snippet: |
rewrite ^/(.*)$ https://<new-domain>/$1 permanent;
+1
+1
+1
+1
Same request but the other way around, we want to redirect www to non-www.
Closing. Fixed in master.
@aledbf any link to the PR or documentation on how to use this?
@montanaflynn today I will open a PR for that
@montanaflynn here https://github.com/kubernetes/ingress/blob/master/controllers/nginx/configuration.md#redirect-from-to-www
This is not released yer. Only in master
@aledbf thanks!
In some scenarios is required to redirect from www.domain.com to domain.com or viceversa. To enable this feature use the annotation ingress.kubernetes.io/from-to-www-redirect: "true"
How do you specify if it should remove or add the www subdomain?
How do you specify if it should remove or add the www subdomain?
If you add the annotation in an ingress for host foo.com it will add a redirect from www.foo.com -> foo.com
and viceversa
Does this work with kube-lego? I want example.com to redirect to www.example.com. If I list both hosts in tls.hosts and rules then ssl works for both but the redirect does not happen. If I remove one of the hosts then ssl does not work for the host I've removed from the ingress.
@kyv no because this annotation is just a redirect, there's no content in the server section of nginx.conf
Ok. Might be worthwhile mentioning in the readme or configuration.md.
Is there a workaround with kube-lego? I think it's a pretty common setup.
@fiws The workaround I'm currently using is another nginx service that's doing all www to root or vice versa logic before proxying to web service etc. Would be really nice to get rid of that extra nginx service.
The one suggested here works in our situation but the ff. annotation doesn't work:
nginx.ingress.kubernetes.io/permanent-redirect: https://www.google.com
Most helpful comment
I would love to see the proposed solution by @aledbf in this comment. I think those annotations would cover this case as well. My case it a bit more specific where I need to redirect permanently to an entirely different domain. Currently I use a trick by explicitly adding an NGINX rewrite rule to the Ingress configuration using a Configuration Snippet:
ingress.kubernetes.io/configuration-snippet: | rewrite ^/(.*)$ https://<new-domain>/$1 permanent;