Skip to content

The Config File and Ingress Rules

Outcome: you can write the file that tells a named tunnel what to publish, ask the file — before anything runs — whether it is valid and which service would answer a given address, and read its refusals when it is not. This is the chapter the lab could verify most completely, because cloudflared's config machinery works entirely offline. Every output below is real.

The shape of the file

A locally managed tunnel is described by one YAML file, conventionally ~/.cloudflared/config.yml:

tunnel: photos
credentials-file: /home/you/.cloudflared/<uuid>.json

ingress:
  - hostname: demo.example.org
    service: http://localhost:18099
  - hostname: files.example.org
    path: ^/public/.*
    service: http://localhost:18099
  - service: http_status:404

(The file the lab interrogated — cloudflared-lab/config/config-good.yml — carries just the ingress: half; the two lines above it are the standard header a real deployment adds, naming the tunnel object and its credentials file from chapter 5.)

The ingress rules are the heart of it: a list, read top to bottom, first match wins. Each rule may filter on a hostname, on a path (a regular expression matched against the request's path), or both; a rule with neither matches everything. Each rule names a service — where matching requests are sent. A service is usually a local URL (http://localhost:18099, https://localhost:8443, a tcp:// or unix: address for the private-network uses this guide only points at), or one of two built-in non-services: http_status:404 (answer it yourself, with this status, and send nothing to any origin) and hello_world (a built-in demo page cloudflared serves itself — the project's own first-run check). Both validate: transcript cloudflared-lab/transcripts/t11-status-and-hello.txt puts a http_status:410 rule and a hello_world catch-all through the validator and gets OK — chapter 9's retirement recipe stands on the 410.

Two consequences of first-match-wins are worth stating before the evidence. Order is meaning: the specific rule must stand above the general one, or it will never be reached. And the list must end with a rule that catches everything, or some requests would have no answer at all — cloudflared refuses to run a file that leaves the question open, as the next section shows.

Ask the file: validate

cloudflared tunnel --config config-good.yml ingress validate

Check (transcript cloudflared-lab/transcripts/t04-ingress-validate.txt):

$ cloudflared tunnel --config config-good.yml ingress validate
Validating rules from  config-good.yml
OK

The same transcript breaks the file three ways, and each refusal names its cause:

No catch-all at the end:

Validation failed: The last ingress rule must match all URLs (i.e. it should not have a hostname or path filter)

A service that is not an address:

Validation failed: just-a-word is an invalid address, please make sure it has a scheme and a hostname

YAML with its indentation knocked sideways — the error arrives from the YAML parser before validation even begins:

error parsing YAML in config file at config-broken-yaml.yml: yaml: line 1: did not find expected '-' indicator

(The broken files are kept beside the good one in cloudflared-lab/config/, so the failures can be re-run, not just read about.) The habit this chapter is really teaching is the order of operations: validate is free, instant and offline — run it after every edit, before you restart anything.

Ask the file: which rule answers?

Validation says the file is well-formed. A different question — for this address, what actually happens? — has its own subcommand, and it settles arguments that staring at YAML does not:

cloudflared tunnel --config config-good.yml ingress rule https://files.example.org/public/report.pdf

Check (transcript cloudflared-lab/transcripts/t05-ingress-rule.txt), four questions and their answers:

$ cloudflared tunnel --config config-good.yml ingress rule https://demo.example.org/
Matched rule #0
        hostname: demo.example.org
        service: http://localhost:18099

$ cloudflared tunnel --config config-good.yml ingress rule https://files.example.org/public/report.pdf
Matched rule #1
        hostname: files.example.org
        path: ^/public/.*
        service: http://localhost:18099

$ cloudflared tunnel --config config-good.yml ingress rule https://files.example.org/private/notes.txt
Matched rule #2
        service: http_status:404

Read the third answer carefully: the hostname matched rule #1's host but the path did not match its pattern, so the request fell through to the catch-all and would be answered 404 — the files.example.org name publishes only its /public/ folder, and the rule engine, asked directly, proves it. The transcript's fourth question, an entirely unknown hostname, lands on the same catch-all. One tunnel can therefore answer for names it never mentions, politely refusing them all — or, with a different catch-all, handing them to a default service. Which behaviour you want is a decision; that it is a decision is the point of the catch-all rule.

The trap in the same tool

One behaviour of ingress validate belongs in this chapter rather than waiting for troubleshooting, because the lab lost real time to it: run it with no config anywhere — no --config, no file in the default places — and it does not complain. It waits. With no file to read, cloudflared reads the rules as JSON from standard input, and a terminal sits there appearing hung while it is in fact listening politely. Transcript cloudflared-lab/transcripts/t10-validate-no-config.txt shows the message that gives it away (reading config as JSON from standard input), the error when stdin is closed at once, and a valid JSON ruleset piped in and answered with OK. In a script, always pass --config; in a terminal, Ctrl-C loses nothing.