RedirectScheme Middleware
The RedirectScheme
middleware is used to redirect incoming HTTP requests to a different scheme (e.g., from http
to https
).
This is particularly useful for enforcing secure connections or redirecting traffic to a specific port.
Configuration
Below is an example configuration for the RedirectScheme
middleware:
middlewares:
- name: redirectScheme
type: redirectScheme
rule:
scheme: https # The target scheme to redirect to (e.g., https).
port: 8443 # (Optional) The target port to redirect to. If not specified, the default port for the scheme is used.
permanent: false # (Optional) If set to `true`, the redirect will use a 301 (permanent) status code. Default is `false` (302 temporary redirect).
Parameters:
-
scheme
(Required)
Specifies the target scheme for the redirect. Common values arehttps
for secure connections orhttp
for non-secure connections. -
port
(Optional)
Specifies the target port for the redirect. If not provided, the default port for the specified scheme will be used (e.g.,443
forhttps
,80
forhttp
). -
permanent
(Optional)
Determines whether the redirect is permanent or temporary.- If set to
true
, a301 Permanent Redirect
status code will be used. - If set to
false
(default), a302 Temporary Redirect
status code will be used.
- If set to
Example Use Cases
- Enforcing HTTPS
Redirect all HTTP traffic to HTTPS to ensure secure communication:
middlewares:
- name: enforceHttps
type: redirectScheme
rule:
scheme: https
- Custom Port Redirection
Redirect HTTP traffic to HTTPS on a custom port (e.g.,8443
):
middlewares:
- name: redirectToCustomPort
type: redirectScheme
rule:
scheme: https
port: 8443
- Permanent Redirect
Permanently redirect HTTP traffic to HTTPS:
middlewares:
- name: permanentHttpsRedirect
type: redirectScheme
rule:
scheme: https
permanent: true