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

Redirect to another entryPoint per frontend #2133

Merged
merged 2 commits into from
Nov 18, 2017

Conversation

SantoDE
Copy link
Collaborator

@SantoDE SantoDE commented Sep 18, 2017

Description

This PR makes it possible, to enable a redirect to another entrypoint per frontend. Current Supported Backends are:

  • Docker
  • Kubernetes
  • Rancher

The only thing you need to do is, set e.g. traefik.frontend.redirect=https.

If this redirect is enabled, it's shown on the dashboard as well.

eingefugtes_bild_18_09_17__17_00

Fixes #1456, #541

Copy link
Member

@emilevauge emilevauge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @SantoDE !
Hum 🤔, I think it would be better to stick with the current behavior and not redefine another way to redirect.
How about using a label traefik.frontend.redirect=https to achieve this ? This would allow to reuse some code ;)
WDYT @containous/traefik ?

@SantoDE
Copy link
Collaborator Author

SantoDE commented Sep 19, 2017

Hey @emilevauge ,
could you check again please? :)

@dtomcej
Copy link
Contributor

dtomcej commented Sep 19, 2017

@SantoDE Do you think that you could leverage the Headers middleware to accomplish this?

By setting the SSLRedirect = true for that frontend will invoke the middleware, and will provide redirection. That would also allow different hosts, 301 vs 302 redirects etc.

I am currently implementing the full gamut of options in 2030. Unfortunately, I have been delayed due to school.

PR 2146 is attempting to do part of this.

@containous/traefik Thoughts?

@SantoDE
Copy link
Collaborator Author

SantoDE commented Sep 19, 2017

@dtomcej we would then be doing to different kind of rewrites for the "same" case. The entryPoint https redirect works the same way by utilizing the rewrite middleware. Im personally not bound to either way, but we should use the same in both cases

@dtomcej
Copy link
Contributor

dtomcej commented Sep 19, 2017

@SantoDE I don't disagree. Unfortunately we have a bunch of different places to accomplish the same thing. I don't want to duplicate or introduce duplicate code. All the providers have access to invoke the headers middleware, and so might be a clean way to invoke the same functions.

Although, we should decide on if that is the way we want to proceed.

@dtomcej
Copy link
Contributor

dtomcej commented Sep 19, 2017

An example would be that you introduce an httpsredirect property to the frontend struct. There is already a Header property there, that has a SSLRediret property inside. That would allow you to use already-existing code to accomplish the same function.

@SantoDE
Copy link
Collaborator Author

SantoDE commented Sep 19, 2017

Would you then move the entryPoint redirect behavior as well? I personally don't like to utilize different rewrites from entryPoint to frontends.

@emilevauge halp! :)

@kachkaev
Copy link
Contributor

Hi @SantoDE @emilevauge! Given that there are still a couple of other features pending for 1.4, do you think this one could also somehow fit the same release? Configrinfg the behavior of https redirects sounds like a great option for many mixed-use environments (especially those serving legacy pre-letsencrypt stuff). Agree with @SantoDE that the PR will close #1456 (which has relatively high number of 👍).

BTW there seems to exist an intersection with #2146 if I’m not wrong.

@SantoDE SantoDE force-pushed the feature/redirect_per_frontend branch 5 times, most recently from 6533724 to b5f435b Compare September 30, 2017 20:56
@SantoDE
Copy link
Collaborator Author

SantoDE commented Sep 30, 2017

Hey @emilevauge , @dtomcej and more.

I updated my code like you suggested. I also changed the web ui once again.

eingefugtes_bild_30_09_17__22_57

Looking forward to your feedback :)

@kachkaev yeah there is some intersection but it's not too bad :) This PR will be included in 1.5 I guess.

@SantoDE SantoDE force-pushed the feature/redirect_per_frontend branch from b5f435b to d37fe4d Compare September 30, 2017 21:05
Copy link
Member

@emilevauge emilevauge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Design LGTM
Thanks @SantoDE 👏
Could update you PR description accordingly ?

@traefiker traefiker merged commit 5d6384e into traefik:master Nov 18, 2017
@tiangolo
Copy link

Merged! Awesome news! 🎉 Thanks @SantoDE for the work!

Traefik team: when do you think this feature would be available in the public Docker images? Which tag would it be in? 1.4.4 ?

@ldez
Copy link
Contributor

ldez commented Nov 18, 2017

@tiangolo We put only bug fix in the branch 1.4 (https://github.com/containous/traefik#release-cycle)

As you see the milestone is 1.5.

@tourshi
Copy link

tourshi commented Dec 21, 2017

I've tried to make use of this https redirect in v1.5.0-rc3 but can't seem to get it to work. I've tried multiple scenarios, non of which result in http to https redirects.

        labels:
            - traefik.backend=host.com
            - traefik.frontend.rule=Host:host.com,www.host.com
            - traefik.docker.network=traefik
            - traefik.frontend.entryPoints=http
            - traefik.frontend.redirect=https
            - traefik.enable=true

Am I misunderstanding something here?

Thank you!

@ldez
Copy link
Contributor

ldez commented Dec 21, 2017

@tourshi Due to #2570:

        labels:
            - traefik.backend=host.com
            - traefik.frontend.rule=Host:host.com,www.host.com
            - traefik.docker.network=traefik
            - traefik.frontend.entryPoints=http
            - traefik.frontend.redirect.entryPoint=https
            - traefik.enable=true

http://v1-5.archive.docs.traefik.io/configuration/backends/docker/#on-service

@tourshi
Copy link

tourshi commented Dec 21, 2017

Thank you @ldez I gave the regex/replacement solution a try, but it seems to break my traefik setup. These are the labels I used:
- traefik.frontend.redirect.regex="^http:\/\/host.com\/(.*)" - traefik.frontend.redirect.replacement="https:\/\/host.com\/$1"

In any case, I want to thank you for your help! I did come across this label:
traefik.frontend.headers.SSLRedirect=true
which solves my basic need of redirecting all http to https

Thank you!

@ldez ldez changed the title redirect to another entryPoint per frontend Redirect to another entryPoint per frontend Jan 7, 2018
@h-bragg
Copy link

h-bragg commented Jan 22, 2018

I have a local setup that by default redirects http to https. However I would like a single container to work on http only (for legacy reasons [as always]). I couldn't figure out which combinations of labels would work to enable that.

in effect I would like:

  • http->http https->http for a single host
  • http->https https->https for everything else

The setup is plain old docker running locally with the following default configuration:

[entryPoints]
  [entryPoints.http]
  address = ":80"
    [entryPoints.http.redirect]
    entryPoint = "https"
  [entryPoints.https]
  address = ":443"
    [entryPoints.https.tls]

I have tried:

    labels:
      traefik.http.frontend.entryPoints: http
      traefik.http.frontend.redirect: http
      traefik.https.frontend.entryPoints: https
      traefik.https.frontend.redirect: http
    labels:
      traefik.frontend.headers.SSLRedirect: 'false'
    labels:
      traefik.frontend.entryPoints: http
      traefik.frontend.redirect.entryPoint: http

and various combinations of the above. Am I missing something obvious?

Version:      v1.5.0-rc5
Codename:     cancoillotte
Go version:   go1.9.2
Built:        2018-01-15_03:59:03PM
OS/Arch:      linux/amd64

@DMW007
Copy link

DMW007 commented Jan 28, 2018

@h-bragg The SSLRedirect header works but you've to pass it like any other label with equal sign and not colon. In addition to this, you need both http and https entrypoints. Without http we get an 404.

Working example for redirect http to https:

   labels: 
      - "traefik.port=80"
      - "traefik.frontend.rule=Host:MySecureDomain"
      - "traefik.frontend.headers.SSLRedirect=true"
      - "traefik.frontend.entryPoints=http,https"

For your other container, simply drop the redirect-header and https entrypoint like this:

    labels:
      - "traefik.port=80"
      - "traefik.frontend.rule=Host:MyInsecureDomain"
      - "traefik.frontend.entryPoints=http"

I tested this using latest stable docker release (traefik:1.5-alpine at the time of writing). Please keep in mind, that browser tend to cache redirects. So I used wget on the command line, there you can see the plain redirect without any caching issues.

@h-bragg
Copy link

h-bragg commented Jan 29, 2018

@DMW007 thanks, I have it working like this for now.

Was looking for it to be ssl by default with http as the exception (each thing connecting to it might be done by a different person, so the least amount of configuration required is better).

@mhumeSF
Copy link

mhumeSF commented Feb 21, 2018

Wanted to test this out locally. I'm in a use case where ssl termination is happening at a load balancer before hitting traefik, but would still like traefik to handle the redirect of http to https. Is there a way to disable ssl, or using a different port to handle http requests?

±  curl -I -H Host:web.localhost https://127.0.0.1:443
curl: (35) LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to 127.0.0.1:443 

docker-compose.yml

version: '2'

services:
  web:
    image: nginx:alpine
    ports:
      - "80"
    labels:
      - "traefik.enable=true"
      - "traefik.frontend.rule=Host:web.localhost"
      - "traefik.port=80"
      - "traefik.frontend.headers.SSLRedirect=true"
      - "traefik.frontend.entryPoints=http,https"

  proxy:
    image: traefik
    command: --api --docker --docker.domain=docker.localhost --logLevel=DEBUG
    ports:
      - "80:80"
      - "443:443"
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - /dev/null:/traefik.toml

@ldez
Copy link
Contributor

ldez commented Feb 22, 2018

Thanks for your interest in Traefik 😃

A pull request is not the place for question. Please come discuss this in :

@traefik traefik locked and limited conversation to collaborators Feb 22, 2018
@SantoDE SantoDE deleted the feature/redirect_per_frontend branch October 12, 2018 09:45
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.