skip to content

In a cloudflared config.yml, how are ingress rules matched against a request, and why must the last rule be a catch-all such as http_status:404?

level: middleimportance: must knowfreq 40%

answer

  1. order decides everything
  2. hostname and path both must match
  3. regex, not prefix
  4. validator demands a match-all ending

basics

~20 s

cloudflared tests ingress rules top to bottom and uses the first whose hostname and path regex both match. The final rule must match every URL, typically http_status:404, so requests for unexpected hostnames get a defined answer; the validator rejects a list whose last rule has a hostname or path filter.

solid answer

~40 s

`cloudflared` walks the `ingress` list **in order** and the **first rule** whose `hostname` and `path` both match wins. A missing `hostname` or `path` matches everything. Hostnames are exact or a leading `*.` wildcard, which matches deeper subdomains but not the apex. `path` is an **unanchored regular expression**, so `/v1` also matches `/legacy/v1-export` unless you write `^/v1/`, and the full path is forwarded unchanged. The last rule must have no hostname or path filter. `cloudflared tunnel ingress validate` rejects a file without one, and also one with a catch-all earlier in the list. `http_status:404` is the usual catch-all, so unplanned hostnames get a `404` rather than a real service. `cloudflared tunnel ingress rule <URL>` shows which rule a URL hits.

code

yaml · 12 lines
yaml
tunnel: 6ff42ae2-765d-4adf-8112-31c55c1551ef
credentials-file: /etc/cloudflared/6ff42ae2-765d-4adf-8112-31c55c1551ef.json

ingress:
  - hostname: admin.example.com
    service: http://localhost:8080
  - hostname: staging-api.example.com
    path: ^/v1/
    service: http://localhost:9000
  - hostname: staging-api.example.com
    service: http_status:403
  - service: http_status:404

go deeper

for a junior

Recall that rules are read top to bottom, the first match wins, and the list must end with a rule that has no hostname or path, usually service: http_status:404.

for a middle

Explain the matching details: leading-wildcard hostnames matched by suffix, unanchored path regexes, the full path forwarded unchanged, and what ingress validate rejects.

for a senior

Show the habits that prevent incidents: anchoring path regexes, a 404 catch-all rather than a real service, and ingress validate plus ingress rule run on every change.

for a principal

Argue for treating the ingress list as reviewed configuration, with a 404 catch-all as the team standard, so no hostname reaches a service nobody wrote a rule for.

## What an ingress rule is `cloudflared` can serve many hostnames through one tunnel. The **`ingress`** list in its configuration says which local service each request goes to. Each rule has up to four parts: - **`hostname`**: an exact name such as `admin.example.com`, or a leading wildcard such as `*.example.com`. - **`path`**: a **regular expression** tested against the request path. - **`service`**: where the request goes. That can be `http://localhost:8080`, `https://localhost:8443`, `tcp://`, `ssh://`, a `unix:` socket, the built-in `hello_world` test server, or a fixed status such as `http_status:404`. - **`originRequest`**: optional per-rule settings for how `cloudflared` talks to that service. For the internal admin dashboard and the staging API, a locally-managed tunnel's `config.yml` might read: ```yaml tunnel: 6ff42ae2-765d-4adf-8112-31c55c1551ef credentials-file: /etc/cloudflared/6ff42ae2-765d-4adf-8112-31c55c1551ef.json ingress: - hostname: admin.example.com service: http://localhost:8080 - hostname: staging-api.example.com path: ^/v1/ service: http://localhost:9000 - hostname: staging-api.example.com service: http_status:403 - service: http_status:404 ``` ## How a request is matched 1. `cloudflared` takes the request's `Host` (dropping any port) and its path. 2. It walks the rules **from top to bottom** and the **first match wins**. Later rules are never consulted. 3. A rule matches when both parts match. A missing `hostname` matches every host, and a missing `path` matches every path. 4. When nothing earlier matched, the last rule, the catch-all, handles the request. The two parts each have a trap. - **Wildcards match by suffix.** `*.example.com` matches any name ending in `.example.com`, deeper names such as `a.b.example.com` included, but not the bare `example.com`. A wildcard anywhere except the first character, such as `test.*.example.com`, is rejected, and so is a hostname that carries a port. - **Paths are unanchored regular expressions.** `path: /v1` matches `/v1/users`, but it also matches `/legacy/v1-export`, because the expression only has to occur somewhere in the path. Write `^/v1/` to mean "starts with". Matching never rewrites anything: `cloudflared` forwards the **full original path** to the service, so `/v1/users` arrives as `/v1/users`. In the example, `staging-api.example.com/v1/orders` goes to port 9000, `staging-api.example.com/debug` hits the third rule and gets a `403`, and `unknown.example.com` falls through to the `404`. ## Why the last rule must be a catch-all `cloudflared` has to do something with every request that reaches it, so the configuration must end with a rule that matches **every** URL. That means a rule with no `hostname` and no `path`, or with `hostname: "*"`. The validator enforces this from both sides. | Configuration | Result | |---|---| | Last rule has a `hostname` or `path` | Rejected: "The last ingress rule must match all URLs" | | A catch-all appears before the end | Rejected: the rules after it would never be triggered | | Wildcard in the middle of a hostname | Rejected | | Hostname with a port, such as `admin.example.com:8443` | Rejected | The catch-all can point at a real service, but **`http_status:404`** is the usual choice, and it is the safe one. Any request whose hostname you did not mean to serve, a typo or another record in the account pointed at the tunnel, gets a plain `404` instead of landing on whatever service happens to be listed last. ## Mistakes that reach production - **A broad rule placed too early.** A `*.example.com` rule above `admin.example.com` captures the admin traffic first, so the specific rule never runs. Put specific rules above general ones. - **An unanchored path used as a gate.** `path: /v1` meant to expose only the versioned API also exposes `/internal/v1-debug`. Anchor it with `^`, and consider a closing `/`. - **A real service as the catch-all.** Ending with `service: http://localhost:8080` publishes the dashboard under every hostname that reaches the tunnel. - **Expecting a prefix to be stripped.** A service that serves `/orders` gets `/v1/orders` and answers `404`, which looks like a routing failure but is not one. - **Forgetting the DNS side.** An ingress rule alone publishes nothing; the hostname also needs a CNAME to the tunnel, created separately. ## Checking rules before they ship - **`cloudflared tunnel ingress validate`** parses the ingress rules in the configuration file and prints `OK`, or the validation error. It also warns about unused keys in the file. - **`cloudflared tunnel ingress rule <URL>`** tests one URL against every rule, first to last, and prints the rule that matches, for example `Matched rule #2`. - Both read the default configuration file, or the one named with `--config`. - Ingress rules do not mix with the single-origin **`--url`** flag: `cloudflared`'s help calls the two incompatible, and `ingress validate` reports an error when `--url` is also set. For a **remotely-managed** tunnel, the published application routes edited in the dashboard or through the API form the same ordered list. The API's `ingress` array also ends with a catch-all, and the same first-match rules apply.

  • Does an ingress rule with path ^/v1/ strip /v1 before the request reaches the service?
    No. `cloudflared` uses the path only to choose a rule and forwards the full original path, so `/v1/orders` arrives at `http://localhost:9000` as `/v1/orders`. If the service expects `/orders`, rewrite the URL at Cloudflare's edge with a URL rewrite rule, or put a local reverse proxy behind `cloudflared` that strips the prefix.
  • Why not make the catch-all point at the admin dashboard instead of http_status:404?
    Then any hostname that reaches the tunnel without a rule of its own, such as a typo or a stray record in the account, lands on the dashboard. A `404` catch-all turns every unplanned hostname into a defined refusal, and each service is reachable only under the hostname written for it.
  • How do you test a rule change before restarting the tunnel?
    Run `cloudflared tunnel ingress validate` against the edited file, which prints `OK` or the validation error. Then run `cloudflared tunnel ingress rule` with each URL you care about and confirm the rule number it reports, all without touching the running tunnel.

  • cloudflared picks the most specific ingress rule, wherever it sits in the list
  • path: /v1 matches only paths that start with /v1
  • *.example.com also matches the bare example.com
  • The catch-all is optional; unmatched requests just time out
  • cloudflared strips the matched path prefix before forwarding
  • The catch-all must be http_status:404 and nothing else