Rewrite
This example demonstrates how to use the Rewrite annotations
Prerequisites
You will need to make sure your Ingress targets exactly one Ingress controller by specifying the ingress.class annotation, and that you have an ingress controller running in your cluster.
Deployment
Rewriting can be controlled using the following annotations:
Name | Description | Values |
nginx.ingress.kubernetes.io/rewrite-target | Target URI where the traffic must be redirected | string |
nginx.ingress.kubernetes.io/ssl-redirect | Indicates if the location section is accessible SSL only (defaults to True when Ingress contains a Certificate) | bool |
nginx.ingress.kubernetes.io/force-ssl-redirect | Forces the redirection to HTTPS even if the Ingress is not TLS Enabled | bool |
nginx.ingress.kubernetes.io/app-root | Defines the Application Root that the Controller must redirect if it's in '/' context | string |
nginx.ingress.kubernetes.io/use-regex | Indicates if the paths defined on an Ingress use regular expressions | bool |
Examples
Rewrite Target
!!! attention Starting in Version 0.22.0, ingress definitions using the annotation nginx.ingress.kubernetes.io/rewrite-target
are not backwards compatible with previous versions. In Version 0.22.0 and beyond, any substrings within the request URI that need to be passed to the rewritten path must explicitly be defined in a capture group.
!!! note Captured groups are saved in numbered placeholders, chronologically, in the form $1
, $2
... $n
. These placeholders can be used as parameters in the rewrite-target
annotation.
Create an Ingress rule with a rewrite annotation:
In this ingress definition, any characters captured by (.*)
will be assigned to the placeholder $2
, which is then used as a parameter in the rewrite-target
annotation.
For example, the ingress definition above will result in the following rewrites:
rewrite.bar.com/something
rewrites torewrite.bar.com/
rewrite.bar.com/something/
rewrites torewrite.bar.com/
rewrite.bar.com/something/new
rewrites torewrite.bar.com/new
App Root
Create an Ingress rule with a app-root annotation:
Check the rewrite is working
Last updated