Rules and Priorities
A router is in charge of connecting incoming requests to the services that can handle them. To customize the connection, Traefik Hub API Gateway allows you to define your matching rules and prioritize the routes.
Rules
Rule consists of a set of matchers configured with values, that determine if a particular request matches specific criteria. If the rule is verified, the router becomes active, calls middlewares, and then forwards the request to the service.
- The character
@is not authorized in the router name. - To set the value of a rule, use backticks
`or escaped double-quotes\". - Single quotes
'are not accepted since the values are Go's String Literals. - Regular Expressions:
- Matchers that accept a regexp as their value use a Go flavored syntax.
- The usual AND (
&&) and OR (||) logical operators can be used, with the expected precedence rules, as well as parentheses to express complex rules. - The NOT (
!) operator allows you to invert the matcher
The table below lists all the available matchers:
| Matcher | Description |
|---|---|
Header(`key`, `value`) | Matches requests containing a header named key set to value. |
HeaderRegexp(`key`, `regexp`) | Matches requests containing a header named key matching regexp. |
Host(`domain`) | Matches requests host set to domain. |
HostRegexp(`regexp`) | Matches requests host matching regexp. |
Method(`method`) | Matches requests method set to method. |
Path(`path`) | Matches requests path set to path. |
PathPrefix(`prefix`) | Matches requests path prefix set to prefix. |
PathRegexp(`regexp`) | Matches request path using regexp. |
Query(`key`, `value`) | Matches requests query parameters named key set to value. |
QueryRegexp(`key`, `regexp`) | Matches requests query parameters named key matching regexp. |
ClientIP(`ip`) | Matches requests client IP using ip. It accepts IPv4, IPv6 and CIDR formats. |
Header and HeaderRegexp
The Header and HeaderRegexp matchers allow matching requests that contain specific header.
| Behavior | Rule |
|---|---|
Match requests with a Content-Type header set to application/yaml | Header(`Content-Type`, `application/yaml`) |
Match requests with a Content-Type header set to either application/json or application/yaml | HeaderRegexp(`Content-Type`, `^application/(json|yaml)$`) |
| Match headers case-insensitively | HeaderRegexp(`Content-Type`, `(?i)^application/(json|yaml)$`) |
Host and HostRegexp
The Host and HostRegexp matchers allow matching requests that are targeted to a given host.
These matchers do not support non-ASCII characters, use punycode encoded values (rfc 3492) to match such domains.
If no Host is set in the request URL (for example, it's an IP address), these matchers will look at the Host header.
These matchers will match the request's host in lowercase.
| Behavior | Rule |
|---|---|
Match requests with Host set to example.com | Host(`example.com`) |
Match requests sent to any subdomain of example.com | HostRegexp(`^.+\.example\.com$`) |
Match requests with Host set to either example.com or example.org | HostRegexp(`^example\.(com|org)$`) |
Match Host case-insensitively | HostRegexp(`(?i)^example\.(com|org)$`) |
Method
The Method matchers allows matching requests sent based on their HTTP method (also known as request verb).
| Behavior | Rule |
|---|---|
Match OPTIONS requests: | Method(`OPTIONS`) |
Path, PathPrefix, and PathRegexp
These matchers allow matching requests based on their URL path.
For exact matches, use Path and its prefixed alternative PathPrefix, for regexp matches, use PathRegexp.
Path are always starting with a /, except for PathRegexp.
| Behavior | Rule |
|---|---|
Match /products but neither /products/shoes nor /products/ | Path(`/products`) |
Match /products as well as everything under /products, such as /products/shoes, /products/ but also /products-for-sale | PathPrefix(`/products`) |
Match both /products/shoes and /products/socks with and ID like /products/shoes/31 | PathRegexp(`^/products/(shoes|socks)/[0-9]+$`) |
Match requests with a path ending in either .jpeg, .jpg or .png | PathRegexp(`\.(jpeg|jpg|png)$`) |
Match /products as well as everything under /products, such as /products/shoes, /products/ but also /products-for-sale, case-insensitively | PathRegexp(`(?i)^/products`) |