Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request: Enable Ingress rewrite to direct all non-www to www sudomain #605

Closed
nicky-lenaers opened this issue Apr 14, 2017 · 22 comments
Closed

Comments

@nicky-lenaers
Copy link

nicky-lenaers commented Apr 14, 2017

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.

@pieterlange
Copy link
Contributor

pieterlange commented Apr 14, 2017

This doesn't seem to be a supported feature of the GCE loadbalancers right now.

@nicky-lenaers
Copy link
Author

@pieterlange So the alternative is to use NGINX then?

@pieterlange
Copy link
Contributor

Yes

@nicky-lenaers nicky-lenaers changed the title Using Ingress rewrite to direct all non-www to www sudomain Request: Enable Ingress rewrite to direct all non-www to www sudomain Apr 14, 2017
@BaldwinKoo
Copy link

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.

@arjanschaaf
Copy link

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;

@agustincastanio
Copy link

+1

3 similar comments
@kyv
Copy link

kyv commented May 22, 2017

+1

@hpio
Copy link

hpio commented Jun 30, 2017

+1

@dylannlaw
Copy link

+1

@montanaflynn
Copy link

Same request but the other way around, we want to redirect www to non-www.

@aledbf
Copy link
Member

aledbf commented Aug 22, 2017

Closing. Fixed in master.

@aledbf aledbf closed this as completed Aug 22, 2017
@montanaflynn
Copy link

@aledbf any link to the PR or documentation on how to use this?

@aledbf
Copy link
Member

aledbf commented Aug 22, 2017

@montanaflynn today I will open a PR for that

@aledbf
Copy link
Member

aledbf commented Aug 22, 2017

@montanaflynn
Copy link

@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?

@aledbf
Copy link
Member

aledbf commented Aug 22, 2017

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

@kyv
Copy link

kyv commented Sep 7, 2017

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.

@aledbf
Copy link
Member

aledbf commented Sep 7, 2017

@kyv no because this annotation is just a redirect, there's no content in the server section of nginx.conf

@kyv
Copy link

kyv commented Sep 7, 2017

Ok. Might be worthwhile mentioning in the readme or configuration.md.

@fiws
Copy link

fiws commented Sep 14, 2017

Is there a workaround with kube-lego? I think it's a pretty common setup.

@larsha
Copy link

larsha commented Oct 7, 2017

@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.

nottrobin added a commit to nottrobin/deployment-configs that referenced this issue Nov 2, 2017
and redirect.

We initially wanted to use [the redirect-from-to-www annotation](https://github.com/kubernetes/ingress-nginx/blob/master/docs/user-guide/annotations.md#redirect-from-to-www), as this is nice and neat, but unfortunately that [isn't able to serve a TLS certificate for the redirected domain](kubernetes/ingress-nginx#605).

Until that's fixed, we have to use this solution, [as others are doing](kubernetes/ingress-nginx#605 (comment)).

(It would also help to make this more DRY if we were able to [specify multiple hosts in a http rule](kubernetes/kubernetes#43633))
@edbergavera
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests