I have a requirement to rewrite all URLs to lowercase.
E.g. test.com/CHILD
to test.com/child
Frontend application is developed on docker on azure kubernetes services. Ingress is controlled on nginx ingress controller.
I have a requirement to rewrite all URLs to lowercase.
E.g. test.com/CHILD
to test.com/child
Frontend application is developed on docker on azure kubernetes services. Ingress is controlled on nginx ingress controller.
3
Answers
The ingress controller supports case insensitive regular expressions in the
spec.rules.http.paths.path
field. This can be enabled by setting thenginx.ingress.kubernetes.io/use-regex
annotation to true (the default is false). For more information please check hereUsing the
nginx.ingress.kubernetes.io/use-regex
annotation will indicate whether or not the paths defined on an Ingress use regular expressions. The default value is false.The following will indicate that regular expression paths are being used:
The following will indicate that regular expression paths are not being used:
When this annotation is set to
true
, the case insensitive regular expression location modifier will be enforced on ALL paths for a given host regardless of what Ingress they are defined on.[Reference]
You can rewrite URLs using Lua as described in the Enforce Lower Case URLs (NGINX) article.
All we need to do is add the following configuration block to nginx:
I will show you how it works.
First, I created an Ingress resource with the previously mentioned configuration:
Then I created a sample
app-1
Pod and exposed it on port80
:Finally, we can test if rewrite works as expected:
Additionally, in the
ingress-nginx-controller
logs, we can see the following log entries:The idea of the solution is ok, but this would rewrite all urls of this controller not only the one for this ingress. Putting it into the configuration-snippet is also not possible, as this leads to an duplicate rewrite_by_lua_block.
I put the rule into a differen lua block