RedirectScheme

Redirecting the Client to a Different Scheme/Port

The RedirectScheme middleware redirects the request if the request scheme is different from the configured scheme.

When behind another reverse-proxy

When there is at least one other reverse-proxy between the client and Traefik, the other reverse-proxy (i.e. the last hop) needs to be a trusted one.

Otherwise, Traefik would clean up the X-Forwarded headers coming from this last hop, and as the RedirectScheme middleware relies on them to determine the scheme used, it would not function as intended.

Configuration Examples

Docker

  1. # Redirect to https
  2. labels:
  3. - "traefik.http.middlewares.test-redirectscheme.redirectscheme.scheme=https"
  4. - "traefik.http.middlewares.test-redirectscheme.redirectscheme.permanent=true"

Kubernetes

  1. # Redirect to https
  2. apiVersion: traefik.containo.us/v1alpha1
  3. kind: Middleware
  4. metadata:
  5. name: test-redirectscheme
  6. spec:
  7. redirectScheme:
  8. scheme: https
  9. permanent: true

Consul Catalog

  1. # Redirect to https
  2. labels:
  3. - "traefik.http.middlewares.test-redirectscheme.redirectscheme.scheme=https"
  4. - "traefik.http.middlewares.test-redirectscheme.redirectscheme.permanent=true"

Marathon

  1. "labels": {
  2. "traefik.http.middlewares.test-redirectscheme.redirectscheme.scheme": "https"
  3. "traefik.http.middlewares.test-redirectscheme.redirectscheme.permanent": "true"
  4. }

Rancher

  1. # Redirect to https
  2. labels:
  3. - "traefik.http.middlewares.test-redirectscheme.redirectscheme.scheme=https"
  4. - "traefik.http.middlewares.test-redirectscheme.redirectscheme.permanent=true"

File (YAML)

  1. # Redirect to https
  2. http:
  3. middlewares:
  4. test-redirectscheme:
  5. redirectScheme:
  6. scheme: https
  7. permanent: true

File (TOML)

  1. # Redirect to https
  2. [http.middlewares]
  3. [http.middlewares.test-redirectscheme.redirectScheme]
  4. scheme = "https"
  5. permanent = true

Configuration Options

permanent

Set the permanent option to true to apply a permanent redirection.

Docker

  1. # Redirect to https
  2. labels:
  3. # ...
  4. - "traefik.http.middlewares.test-redirectscheme.redirectscheme.permanent=true"

Kubernetes

  1. # Redirect to https
  2. apiVersion: traefik.containo.us/v1alpha1
  3. kind: Middleware
  4. metadata:
  5. name: test-redirectscheme
  6. spec:
  7. redirectScheme:
  8. # ...
  9. permanent: true

Consul Catalog

  1. # Redirect to https
  2. labels:
  3. # ...
  4. - "traefik.http.middlewares.test-redirectscheme.redirectscheme.permanent=true"

Marathon

  1. "labels": {
  2. "traefik.http.middlewares.test-redirectscheme.redirectscheme.permanent": "true"
  3. }

Rancher

  1. # Redirect to https
  2. labels:
  3. # ...
  4. - "traefik.http.middlewares.test-redirectscheme.redirectscheme.permanent=true"

File (YAML)

  1. # Redirect to https
  2. http:
  3. middlewares:
  4. test-redirectscheme:
  5. redirectScheme:
  6. # ...
  7. permanent: true

File (TOML)

  1. # Redirect to https
  2. [http.middlewares]
  3. [http.middlewares.test-redirectscheme.redirectScheme]
  4. # ...
  5. permanent = true

scheme

The scheme option defines the scheme of the new URL.

Docker

  1. # Redirect to https
  2. labels:
  3. - "traefik.http.middlewares.test-redirectscheme.redirectscheme.scheme=https"

Kubernetes

  1. # Redirect to https
  2. apiVersion: traefik.containo.us/v1alpha1
  3. kind: Middleware
  4. metadata:
  5. name: test-redirectscheme
  6. spec:
  7. redirectScheme:
  8. scheme: https

Consul Catalog

  1. # Redirect to https
  2. labels:
  3. - "traefik.http.middlewares.test-redirectscheme.redirectscheme.scheme=https"

Marathon

  1. "labels": {
  2. "traefik.http.middlewares.test-redirectscheme.redirectscheme.scheme": "https"
  3. }

Rancher

  1. # Redirect to https
  2. labels:
  3. - "traefik.http.middlewares.test-redirectscheme.redirectscheme.scheme=https"

File (YAML)

  1. # Redirect to https
  2. http:
  3. middlewares:
  4. test-redirectscheme:
  5. redirectScheme:
  6. scheme: https

File (TOML)

  1. # Redirect to https
  2. [http.middlewares]
  3. [http.middlewares.test-redirectscheme.redirectScheme]
  4. scheme = "https"

port

The port option defines the port of the new URL.

Docker

  1. # Redirect to https
  2. labels:
  3. # ...
  4. - "traefik.http.middlewares.test-redirectscheme.redirectscheme.port=443"

Kubernetes

  1. # Redirect to https
  2. apiVersion: traefik.containo.us/v1alpha1
  3. kind: Middleware
  4. metadata:
  5. name: test-redirectscheme
  6. spec:
  7. redirectScheme:
  8. # ...
  9. port: "443"

Consul Catalog

  1. # Redirect to https
  2. labels:
  3. # ...
  4. - "traefik.http.middlewares.test-redirectscheme.redirectscheme.port=443"

Marathon

  1. "labels": {
  2. "traefik.http.middlewares.test-redirectscheme.redirectscheme.port": "443"
  3. }

Rancher

  1. # Redirect to https
  2. labels:
  3. # ...
  4. - "traefik.http.middlewares.test-redirectscheme.redirectscheme.port=443"

File (YAML)

  1. # Redirect to https
  2. http:
  3. middlewares:
  4. test-redirectscheme:
  5. redirectScheme:
  6. # ...
  7. port: "443"

File (TOML)

  1. # Redirect to https
  2. [http.middlewares]
  3. [http.middlewares.test-redirectscheme.redirectScheme]
  4. # ...
  5. port = 443

Port in this configuration is a string, not a numeric value.