<p>I have a requirement to rewrite all URLs to lowercase.</p>
<p>E.g. <code>test.com/CHILD</code> to <code>test.com/child</code></p>
<p>Frontend application is developed on docker on azure kubernetes services. Ingress is controlled on nginx ingress controller.</p>

<p>You can rewrite URLs using Lua as described in the <a href="https://www.rewriteguide.com/nginx-enforce-lower-case-urls/" rel="nofollow noreferrer">Enforce Lower Case URLs (NGINX)</a> article.</p>
<p>All we need to do is add the following configuration block to nginx:</p>
<pre><code>location ~ [A-Z] {
    rewrite_by_lua_block {
        ngx.redirect(string.lower(ngx.var.uri), 301);
    }
}
</code></pre>
<p>I will show you how it works.</p>
<hr />
<p>First, I created an Ingress resource with the previously mentioned configuration:</p>
<pre><code>$ cat test-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: test-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/server-snippet: |
     location ~ [A-Z] {
        rewrite_by_lua_block {
            ngx.redirect(string.lower(ngx.var.uri), 301);
        }
      }
spec:
  rules:
  - http:
      paths:
      - path: /app-1
        pathType: Prefix
        backend:
          service:
            name: app-1
            port:
              number: 80
              
              
$ kubectl apply -f test-ingress.yaml
ingress.networking.k8s.io/test-ingress created

$ kubectl get ing
NAME           CLASS    HOSTS   ADDRESS          PORTS   AGE
test-ingress   &lt;none&gt;   *       &lt;PUBLIC_IP&gt;      80      58s
</code></pre>
<p>Then I created a sample <code>app-1</code> Pod and exposed it on port <code>80</code>:</p>
<pre><code>$ kubectl run app-1 --image=nginx
pod/app-1 created

$ kubectl expose pod app-1 --port=80
service/app-1 exposed
</code></pre>
<p>Finally, we can test if rewrite works as expected:</p>
<pre><code>$ curl -I &lt;PUBLIC_IP&gt;/APP-1
HTTP/1.1 301 Moved Permanently
Date: Wed, 06 Oct 2021 13:53:56 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive
Location: /app-1

$ curl -L &lt;PUBLIC_IP&gt;/APP-1
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Welcome to nginx!&lt;/title&gt;
...
</code></pre>
<p>Additionally, in the <code>ingress-nginx-controller</code> logs, we can see the following log entries:</p>
<pre><code>10.128.15.213 - - [06/Oct/2021:13:54:34 +0000] &quot;GET /APP-1 HTTP/1.1&quot; 301 162 &quot;-&quot; &quot;curl/7.64.0&quot; 83 0.000 [-] [] - - - - c4720e38c06137424f7b951e06c3762b
10.128.15.213 - - [06/Oct/2021:13:54:34 +0000] &quot;GET /app-1 HTTP/1.1&quot; 200 615 &quot;-&quot; &quot;curl/7.64.0&quot; 83 0.001 [default-app-1-80] [] 10.4.1.13:80 615 0.001 200 f96b5664765035de8832abebefcabccf
</code></pre>
