|
| 1 | +# Support for Rewrite Target |
| 2 | + |
| 3 | +The `nginx.org/rewrite-target` annotation enables URL path rewriting by specifying a target path that requests should be rewritten to. This annotation works with regular expression capture groups from the Ingress path to create dynamic rewrites. |
| 4 | + |
| 5 | +The annotation is mutually exclusive with `nginx.org/rewrites`. If both are present, `nginx.org/rewrites` takes precedence. |
| 6 | + |
| 7 | +## Running the Example |
| 8 | + |
| 9 | +## 1. Deploy the Ingress Controller |
| 10 | + |
| 11 | +1. Follow the [installation](https://docs.nginx.com/nginx-ingress-controller/installation/installation-with-manifests/) instructions to deploy the Ingress Controller. |
| 12 | + |
| 13 | +2. Save the public IP address of the Ingress Controller into a shell variable: |
| 14 | + ```console |
| 15 | + IC_IP=XXX.YYY.ZZZ.III |
| 16 | + ``` |
| 17 | + |
| 18 | +3. Save the HTTP port of the Ingress Controller into a shell variable: |
| 19 | + ```console |
| 20 | + IC_HTTP_PORT=<port number> |
| 21 | + ``` |
| 22 | + |
| 23 | +## 2. Deploy the Cafe Application |
| 24 | + |
| 25 | +Create the coffee and tea deployments and services: |
| 26 | + |
| 27 | +```console |
| 28 | +kubectl create -f cafe.yaml |
| 29 | +``` |
| 30 | + |
| 31 | +## 3. Configure Rewrite Examples |
| 32 | + |
| 33 | +### Example 1: Simple Static Rewrite |
| 34 | + |
| 35 | +Create an Ingress resource with basic rewrite functionality: |
| 36 | + |
| 37 | +```console |
| 38 | +kubectl create -f simple-rewrite.yaml |
| 39 | +``` |
| 40 | + |
| 41 | +This configures rewriting from `/coffee` to `/beverages/coffee`. |
| 42 | + |
| 43 | +### Example 2: Dynamic Rewrite with Regex |
| 44 | + |
| 45 | +Create an Ingress resource with regular expression-based rewriting: |
| 46 | + |
| 47 | +```console |
| 48 | +kubectl create -f regex-rewrite.yaml |
| 49 | +``` |
| 50 | + |
| 51 | +This configures dynamic rewriting using capture groups from `/menu/([^/]+)/([^/]+)` to `/beverages/$1/$2`. |
| 52 | + |
| 53 | +## 4. Test the Application |
| 54 | + |
| 55 | +### Test Simple Rewrite |
| 56 | + |
| 57 | +Access the coffee service through the rewritten path: |
| 58 | + |
| 59 | +```console |
| 60 | +curl --resolve cafe.example.com:$IC_HTTP_PORT:$IC_IP http://cafe.example.com:$IC_HTTP_PORT/coffee --insecure |
| 61 | +``` |
| 62 | + |
| 63 | +```text |
| 64 | +Server address: 10.16.0.16:8080 |
| 65 | +Server name: coffee-676c9f8944-n2bmb |
| 66 | +Date: 07/Nov/2025:11:23:09 +0000 |
| 67 | +URI: /beverages/coffee |
| 68 | +Request ID: c224b3e06d79b66f8f33e86cef046c32 |
| 69 | +``` |
| 70 | + |
| 71 | +The request to `/coffee` is rewritten to `/beverages/coffee`. |
| 72 | + |
| 73 | +### Test Regex Rewrite |
| 74 | + |
| 75 | +Access the service using the menu path with dynamic rewriting: |
| 76 | + |
| 77 | +```console |
| 78 | +curl --resolve cafe.example.com:$IC_HTTP_PORT:$IC_IP http://cafe.example.com:$IC_HTTP_PORT/menu/coffee/espresso --insecure |
| 79 | +``` |
| 80 | + |
| 81 | +```text |
| 82 | +Server address: 10.16.1.29:8080 |
| 83 | +Server name: coffee-676c9f8944-vj45p |
| 84 | +Date: 07/Nov/2025:11:26:05 +0000 |
| 85 | +URI: /beverages/coffee/espresso |
| 86 | +Request ID: 88334a8b0eeaee2ffe4fdb4c7768641b |
| 87 | +``` |
| 88 | + |
| 89 | +```console |
| 90 | +curl --resolve cafe.example.com:$IC_HTTP_PORT:$IC_IP http://cafe.example.com:$IC_HTTP_PORT/menu/tea/green --insecure |
| 91 | +``` |
| 92 | + |
| 93 | +```text |
| 94 | +Server address: 10.16.0.16:8080 |
| 95 | +Server name: coffee-676c9f8944-n2bmb |
| 96 | +Date: 07/Nov/2025:11:26:33 +0000 |
| 97 | +URI: /beverages/tea/green |
| 98 | +Request ID: 2ba8f9055aecc059b32f797f1ce2aca5 |
| 99 | +``` |
| 100 | + |
| 101 | +The requests to `/menu/coffee/espresso` and `/menu/tea/green` are rewritten to `/beverages/coffee/espresso` and `/beverages/tea/green` using the captured groups. |
| 102 | + |
| 103 | +## Validations |
| 104 | + |
| 105 | +1. Mutual Exclusivity: The `nginx.org/rewrite-target` annotation is mutually exclusive with `nginx.org/rewrites`. If both annotations are present, `nginx.org/rewrites` takes precedence and a warning will be generated. |
| 106 | + |
| 107 | +2. Security Validation: The annotation includes built-in security validation to prevent: |
| 108 | + - Absolute URLs (`http://` or `https://`) |
| 109 | + - Protocol-relative URLs (`//`) |
| 110 | + - Path traversal patterns (`../` or `..\\`) |
| 111 | + - Paths not starting with `/` |
| 112 | + |
0 commit comments