Cloud Native 8 min read

Master URL Rewrites with Ingress Nginx Controller: Practical Configurations

This guide explains why URL rewriting is essential for security, performance, content transformation, load balancing, session persistence, and cache optimization, and provides step‑by‑step Ingress Nginx Controller configurations to reduce, add, or set root paths for Kubernetes services.

Linux Ops Smart Journey
Linux Ops Smart Journey
Linux Ops Smart Journey
Master URL Rewrites with Ingress Nginx Controller: Practical Configurations

In modern application development, managing many service endpoints is crucial; the Ingress Nginx Controller offers load balancing, service discovery, and complex routing, with URL rewrite functionality to hide internal structures, simplify URLs, or perform permanent redirects.

Why Rewrite?

Security enhancement : add or modify HTTP headers such as X-Frame-Options or Content‑Security‑Policy to prevent XSS, click‑jacking, and other threats.

Performance optimization : refine load‑balancer routing rules or modify requests/responses to boost application performance.

Content transformation : convert URL schemes, redirect HTTP to HTTPS, or migrate old URL patterns to new ones.

Load balancing : ensure traffic is distributed to servers according to specific rules.

Session persistence : maintain consistent user sessions across requests.

Cache optimization : control caching behavior by rewriting URLs or query parameters.

Practical Ingress Rewrite Configurations

1. Reduce URL Path

Scenarios include overly long URLs, multiple modules under one domain, or moving an application’s root from /xxx to /.

Tip : This article discusses three such cases.

$ kubectl get service simple
NAME   TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE
simple ClusterIP   10.97.177.81 <none>         9999/TCP  5d11h

$ curl 10.97.177.81:9999/who/hostname
simple-7864ccf7c9-s5dmz
$ cat <<'EOF' | kubectl apply -f -
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: simple
  namespace: default
  annotations:
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /who/$1
spec:
  ingressClassName: nginx
  rules:
  - host: simple.jiaxzeng.com
    http:
      paths:
      - backend:
          service:
            name: simple
            port:
              number: 9999
        path: /(.*)
        pathType: Prefix
EOF
$ curl -H "host: simple.jiaxzeng.com" 172.139.20.100/hostname
simple-7864ccf7c9-s5dmz

Tip : The simple app rewrites /who/hostname to /hostname; 172.139.20.100 is the Ingress address.

2. Add URL Path

Rewrite /header to /request/header.

$ kubectl get service simple
... (output) ...

$ curl 10.97.177.81:9999/header
{ "Accept": ["*/*"], "User-Agent": ["curl/7.29.0"] }
$ cat <<'EOF' | kubectl apply -f -
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: simple
  namespace: default
  annotations:
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  ingressClassName: nginx
  rules:
  - host: simple.jiaxzeng.com
    http:
      paths:
      - backend:
          service:
            name: simple
            port:
              number: 9999
        path: /request(/|$)(.*)
        pathType: Prefix
EOF
$ curl -H "host: simple.jiaxzeng.com" 172.139.20.100/request/header
{ "Accept": ["*/*"], "User-Agent": ["curl/7.29.0"], "X-Forwarded-For": ["172.139.20.92"], "X-Forwarded-Host": ["simple.jiaxzeng.com"], "X-Forwarded-Port": ["80"], "X-Forwarded-Proto": ["http"], "X-Forwarded-Scheme": ["http"], "X-Real-Ip": ["172.139.20.92"], "X-Request-Id": ["7c057e4e1bb4683a4ef72879e0789ebb"], "X-Scheme": ["http"] }

Tip : The simple app rewrites /header to /request/header; 172.139.20.100 is the Ingress address.

3. Set Application Root Path

Change the application’s root from /hello to /.

$ kubectl get service simple
... (output) ...

$ curl 10.97.177.81:9999/hello
hello go server.
$ cat <<'EOF' | kubectl apply -f -
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: simple
  namespace: default
  annotations:
    nginx.ingress.kubernetes.io/app-root: /hello
spec:
  ingressClassName: nginx
  rules:
  - host: simple.jiaxzeng.com
    http:
      paths:
      - backend:
          service:
            name: simple
            port:
              number: 9999
        path: /
        pathType: Prefix
EOF
$ curl -I -L -H "host: simple.jiaxzeng.com" 172.139.20.100/
HTTP/1.1 302 Moved Temporarily
Location: http://simple.jiaxzeng.com/hello
...
HTTP/1.1 200 OK
...

Tip : The simple app changes /hello to the root path; 172.139.20.100 is the Ingress address.

References

https://kubernetes.github.io/ingress-nginx/examples/rewrite

https://www.kancloud.cn/jiaxzeng/kubernetes/3125882

By configuring rewrite rules in the Ingress Nginx Controller, you can simplify URL structures and improve system maintainability and scalability.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Cloud NativeKubernetesDevOpsNGINXURL RewriteIngress
Linux Ops Smart Journey
Written by

Linux Ops Smart Journey

The operations journey never stops—pursuing excellence endlessly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.