-
Notifications
You must be signed in to change notification settings - Fork 2k
Add path-regex annotation for ingress #4127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ce60d93
Generate config for path-regex annotation
jjngx 1df5ae2
Add validation for path-regex annotation
jjngx 61d40ab
Add documentation for path-regex annotation
jjngx 651d8e1
Add tests for invalid path
jjngx 0427a0b
Merge branch 'main' into feat/regex-ingress-resource
jjngx a05f9fb
Update docs
jjngx d53949c
Update docs
jjngx d48d852
Merge branch 'main' into feat/regex-ingress-resource
jjngx 86d15a2
Merge branch 'main' into feat/regex-ingress-resource
jjngx a84cc65
Merge branch 'main' into feat/regex-ingress-resource
jjngx 87b88cb
Update docs/content/configuration/ingress-resources/advanced-configur…
jjngx f1e7293
Update examples/ingress-resources/path-regex/README.md
jjngx 355572d
Update annotation name
jjngx fab89aa
Update docs
jjngx 6d0c969
Merge branch 'main' into feat/regex-ingress-resource
jjngx 585bedc
Merge branch 'main' into feat/regex-ingress-resource
jjngx 92a4fa6
Update readme doc
jjngx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
# Support for path regular expressions | ||
|
||
NGINX and NGINX Plus support regular expression modifiers for [location](https://nginx.org/en/docs/http/ngx_http_core_module.html#location) | ||
directive. | ||
|
||
The NGINX Ingress Controller provides the following annotations for configuring regular expression support: | ||
|
||
- Optional: ```nginx.org/path-regex: "case_sensitive"``` - specifies a preceding regex modifier to be case sensitive (`~*`). | ||
- Optional: ```nginx.org/path-regex: "case_insensitive"``` - specifies a preceding regex modifier to be case sensitive (`~`). | ||
- Optional: ```nginx.org/path-regex: "exact"``` - specifies exact match preceding modifier (`=`). | ||
|
||
[NGINX documentation](https://docs.nginx.com/nginx/admin-guide/web-server/web-server/#nginx-location-priority) provides | ||
additional information about how NGINX and NGINX Plus resolve location priority. | ||
Read [it](https://docs.nginx.com/nginx/admin-guide/web-server/web-server/#nginx-location-priority) before using | ||
the ``path-regex`` annotation. | ||
|
||
Nginx uses a specific syntax to decide which location block to use to handle a request. | ||
Location blocks live within server blocks (or other location blocks) and are used to decide how to process | ||
the request URI, for example: | ||
|
||
```bash | ||
location optional_modifier location_match { | ||
... | ||
} | ||
``` | ||
|
||
The ``location_match`` defines what NGINX checks the request URI against. The existence or nonexistence of the modifier | ||
in the example affects the way that the Nginx attempts to match the location block. | ||
The modifiers you can apply using the ``path-regex`` annotation will cause the associated location block | ||
to be interpreted as follows: | ||
|
||
- **no modifier** : No modifiers (no annotation applied) - the location is interpreted as a prefix match. | ||
This means that the location given will be matched against the beginning of the request URI to determine a match | ||
|
||
- **~** : Tilde modifier (annotation value ``case_sensitive``) - the location is interpreted as a case-sensitive | ||
regular expression match | ||
|
||
- **~***: Tilde and asterisk modifier (annotation value ``case_insensitive``) - the location is interpreted | ||
as a case-insensitive regular expression match | ||
|
||
- **=** : Equal sign modifier (annotation value ``exact``) - the location is considered a match if the request | ||
URI exactly matches the location provided. | ||
|
||
## Example 1: Case Sensitive RegEx | ||
|
||
In the following example you enable path regex annotation ``nginx.org/path-regex`` and set its value to `case_sensitive`. | ||
|
||
```yaml | ||
apiVersion: networking.k8s.io/v1 | ||
kind: Ingress | ||
metadata: | ||
name: cafe-ingress | ||
annotations: | ||
nginx.org/path-regex: "case_sensitive" | ||
spec: | ||
tls: | ||
- hosts: | ||
- cafe.example.com | ||
secretName: cafe-secret | ||
rules: | ||
- host: cafe.example.com | ||
http: | ||
paths: | ||
- path: /tea/[A-Z0-9] | ||
backend: | ||
serviceName: tea-svc | ||
servicePort: 80 | ||
- path: /coffee/[A-Z0-9] | ||
backend: | ||
serviceName: coffee-svc | ||
servicePort: 80 | ||
``` | ||
|
||
Corresponding NGINX config file snippet: | ||
|
||
```bash | ||
... | ||
|
||
location ~ "^/tea/[A-Z0-9]" { | ||
|
||
set $service "tea-svc"; | ||
status_zone "tea-svc"; | ||
|
||
... | ||
|
||
location ~ "^/coffee/[A-Z0-9]" { | ||
|
||
set $service "coffee-svc"; | ||
status_zone "coffee-svc"; | ||
|
||
... | ||
``` | ||
|
||
## Example 2: Case Insensitive RegEx | ||
|
||
In the following example you enable path regex annotation ``nginx.org/path-regex`` and set its value to `case_insensitive`. | ||
|
||
```yaml | ||
apiVersion: networking.k8s.io/v1 | ||
kind: Ingress | ||
metadata: | ||
name: cafe-ingress | ||
annotations: | ||
nginx.org/path-regex: "case_insensitive" | ||
spec: | ||
tls: | ||
- hosts: | ||
- cafe.example.com | ||
secretName: cafe-secret | ||
rules: | ||
- host: cafe.example.com | ||
http: | ||
paths: | ||
- path: /tea/[A-Z0-9] | ||
backend: | ||
serviceName: tea-svc | ||
servicePort: 80 | ||
- path: /coffee/[A-Z0-9] | ||
backend: | ||
serviceName: coffee-svc | ||
servicePort: 80 | ||
``` | ||
|
||
Corresponding NGINX config file snippet: | ||
|
||
```bash | ||
... | ||
|
||
location ~* "^/tea/[A-Z0-9]" { | ||
|
||
set $service "tea-svc"; | ||
status_zone "tea-svc"; | ||
|
||
... | ||
|
||
location ~* "^/coffee/[A-Z0-9]" { | ||
|
||
set $service "coffee-svc"; | ||
status_zone "coffee-svc"; | ||
|
||
... | ||
``` | ||
|
||
## Example 3: Exact RegEx | ||
|
||
In the following example you enable path regex annotation ``nginx.org/path-regex`` and set its value to `exact` match. | ||
|
||
```yaml | ||
apiVersion: networking.k8s.io/v1 | ||
kind: Ingress | ||
metadata: | ||
name: cafe-ingress | ||
annotations: | ||
nginx.org/path-regex: "exact" | ||
spec: | ||
tls: | ||
- hosts: | ||
- cafe.example.com | ||
secretName: cafe-secret | ||
rules: | ||
- host: cafe.example.com | ||
http: | ||
paths: | ||
- path: /tea/ | ||
backend: | ||
serviceName: tea-svc | ||
servicePort: 80 | ||
- path: /coffee/ | ||
backend: | ||
serviceName: coffee-svc | ||
servicePort: 80 | ||
``` | ||
|
||
Corresponding NGINX config file snippet: | ||
|
||
```bash | ||
... | ||
|
||
location = "/tea" { | ||
|
||
set $service "tea-svc"; | ||
status_zone "tea-svc"; | ||
|
||
... | ||
|
||
location = "/coffee" { | ||
|
||
set $service "coffee-svc"; | ||
status_zone "coffee-svc"; | ||
... | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.