|
| 1 | +# Support for path regular expressions |
| 2 | + |
| 3 | +NGINX and NGINX Plus support regular expression modifiers for [location](https://nginx.org/en/docs/http/ngx_http_core_module.html#location) |
| 4 | + directive. |
| 5 | + |
| 6 | +The NGINX Ingress Controller provides the following annotations for configuring regular expression support: |
| 7 | + |
| 8 | +- Optional: ```nginx.org/path-regex: "case_sensitive"``` - specifies a preceding regex modifier to be case sensitive (`~*`). |
| 9 | +- Optional: ```nginx.org/path-regex: "case_insensitive"``` - specifies a preceding regex modifier to be case sensitive (`~`). |
| 10 | +- Optional: ```nginx.org/path-regex: "exact"``` - specifies exact match preceding modifier (`=`). |
| 11 | + |
| 12 | +[NGINX documentation](https://docs.nginx.com/nginx/admin-guide/web-server/web-server/#nginx-location-priority) provides |
| 13 | +additional information about how NGINX and NGINX Plus resolve location priority. |
| 14 | +Read [it](https://docs.nginx.com/nginx/admin-guide/web-server/web-server/#nginx-location-priority) before using |
| 15 | + the ``path-regex`` annotation. |
| 16 | + |
| 17 | +Nginx uses a specific syntax to decide which location block to use to handle a request. |
| 18 | +Location blocks live within server blocks (or other location blocks) and are used to decide how to process |
| 19 | + the request URI, for example: |
| 20 | + |
| 21 | +```bash |
| 22 | +location optional_modifier location_match { |
| 23 | + ... |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +The ``location_match`` defines what NGINX checks the request URI against. The existence or nonexistence of the modifier |
| 28 | + in the example affects the way that the Nginx attempts to match the location block. |
| 29 | + The modifiers you can apply using the ``path-regex`` annotation will cause the associated location block |
| 30 | + to be interpreted as follows: |
| 31 | + |
| 32 | +- **no modifier** : No modifiers (no annotation applied) - the location is interpreted as a prefix match. |
| 33 | +This means that the location given will be matched against the beginning of the request URI to determine a match |
| 34 | + |
| 35 | +- **~** : Tilde modifier (annotation value ``case_sensitive``) - the location is interpreted as a case-sensitive |
| 36 | +regular expression match |
| 37 | + |
| 38 | +- **~***: Tilde and asterisk modifier (annotation value ``case_insensitive``) - the location is interpreted |
| 39 | +as a case-insensitive regular expression match |
| 40 | + |
| 41 | +- **=** : Equal sign modifier (annotation value ``exact``) - the location is considered a match if the request |
| 42 | + URI exactly matches the location provided. |
| 43 | + |
| 44 | +## Example 1: Case Sensitive RegEx |
| 45 | + |
| 46 | +In the following example you enable path regex annotation ``nginx.org/path-regex`` and set its value to `case_sensitive`. |
| 47 | + |
| 48 | +```yaml |
| 49 | +apiVersion: networking.k8s.io/v1 |
| 50 | +kind: Ingress |
| 51 | +metadata: |
| 52 | + name: cafe-ingress |
| 53 | + annotations: |
| 54 | + nginx.org/path-regex: "case_sensitive" |
| 55 | +spec: |
| 56 | + tls: |
| 57 | + - hosts: |
| 58 | + - cafe.example.com |
| 59 | + secretName: cafe-secret |
| 60 | + rules: |
| 61 | + - host: cafe.example.com |
| 62 | + http: |
| 63 | + paths: |
| 64 | + - path: /tea/[A-Z0-9] |
| 65 | + backend: |
| 66 | + serviceName: tea-svc |
| 67 | + servicePort: 80 |
| 68 | + - path: /coffee/[A-Z0-9] |
| 69 | + backend: |
| 70 | + serviceName: coffee-svc |
| 71 | + servicePort: 80 |
| 72 | +``` |
| 73 | +
|
| 74 | +Corresponding NGINX config file snippet: |
| 75 | +
|
| 76 | +```bash |
| 77 | +... |
| 78 | + |
| 79 | + location ~ "^/tea/[A-Z0-9]" { |
| 80 | + |
| 81 | + set $service "tea-svc"; |
| 82 | + status_zone "tea-svc"; |
| 83 | + |
| 84 | +... |
| 85 | + |
| 86 | + location ~ "^/coffee/[A-Z0-9]" { |
| 87 | + |
| 88 | + set $service "coffee-svc"; |
| 89 | + status_zone "coffee-svc"; |
| 90 | + |
| 91 | +... |
| 92 | +``` |
| 93 | + |
| 94 | +## Example 2: Case Insensitive RegEx |
| 95 | + |
| 96 | +In the following example you enable path regex annotation ``nginx.org/path-regex`` and set its value to `case_insensitive`. |
| 97 | + |
| 98 | +```yaml |
| 99 | +apiVersion: networking.k8s.io/v1 |
| 100 | +kind: Ingress |
| 101 | +metadata: |
| 102 | + name: cafe-ingress |
| 103 | + annotations: |
| 104 | + nginx.org/path-regex: "case_insensitive" |
| 105 | +spec: |
| 106 | + tls: |
| 107 | + - hosts: |
| 108 | + - cafe.example.com |
| 109 | + secretName: cafe-secret |
| 110 | + rules: |
| 111 | + - host: cafe.example.com |
| 112 | + http: |
| 113 | + paths: |
| 114 | + - path: /tea/[A-Z0-9] |
| 115 | + backend: |
| 116 | + serviceName: tea-svc |
| 117 | + servicePort: 80 |
| 118 | + - path: /coffee/[A-Z0-9] |
| 119 | + backend: |
| 120 | + serviceName: coffee-svc |
| 121 | + servicePort: 80 |
| 122 | +``` |
| 123 | +
|
| 124 | +Corresponding NGINX config file snippet: |
| 125 | +
|
| 126 | +```bash |
| 127 | +... |
| 128 | + |
| 129 | + location ~* "^/tea/[A-Z0-9]" { |
| 130 | + |
| 131 | + set $service "tea-svc"; |
| 132 | + status_zone "tea-svc"; |
| 133 | + |
| 134 | +... |
| 135 | + |
| 136 | + location ~* "^/coffee/[A-Z0-9]" { |
| 137 | + |
| 138 | + set $service "coffee-svc"; |
| 139 | + status_zone "coffee-svc"; |
| 140 | + |
| 141 | +... |
| 142 | +``` |
| 143 | + |
| 144 | +## Example 3: Exact RegEx |
| 145 | + |
| 146 | +In the following example you enable path regex annotation ``nginx.org/path-regex`` and set its value to `exact` match. |
| 147 | + |
| 148 | +```yaml |
| 149 | +apiVersion: networking.k8s.io/v1 |
| 150 | +kind: Ingress |
| 151 | +metadata: |
| 152 | + name: cafe-ingress |
| 153 | + annotations: |
| 154 | + nginx.org/path-regex: "exact" |
| 155 | +spec: |
| 156 | + tls: |
| 157 | + - hosts: |
| 158 | + - cafe.example.com |
| 159 | + secretName: cafe-secret |
| 160 | + rules: |
| 161 | + - host: cafe.example.com |
| 162 | + http: |
| 163 | + paths: |
| 164 | + - path: /tea/ |
| 165 | + backend: |
| 166 | + serviceName: tea-svc |
| 167 | + servicePort: 80 |
| 168 | + - path: /coffee/ |
| 169 | + backend: |
| 170 | + serviceName: coffee-svc |
| 171 | + servicePort: 80 |
| 172 | +``` |
| 173 | +
|
| 174 | +Corresponding NGINX config file snippet: |
| 175 | +
|
| 176 | +```bash |
| 177 | +... |
| 178 | + |
| 179 | + location = "/tea" { |
| 180 | + |
| 181 | + set $service "tea-svc"; |
| 182 | + status_zone "tea-svc"; |
| 183 | + |
| 184 | +... |
| 185 | + |
| 186 | + location = "/coffee" { |
| 187 | + |
| 188 | + set $service "coffee-svc"; |
| 189 | + status_zone "coffee-svc"; |
| 190 | +... |
| 191 | +``` |
0 commit comments