Through Cloudflare Tunnel, a staging API at https://localhost:8443 returns 502 with an x509 error, then the wrong site once TLS works; which cloudflared originRequest settings fix each?
answer
- 502 means the hop after cloudflared
- name checked defaults to the service URL host
- trust the private CA, keep verification
- Host header sent to the origin
basics
~20 sSet originServerName to the name on the certificate, and caPool to the private CA if cloudflared does not trust it; keep noTLSVerify off. Then set httpHostHeader to the internal site name, because cloudflared forwards the visitor's Host by default.
solid answer
~30 sThe `502` means the tunnel is connected but `cloudflared` cannot complete the hop to `https://localhost:8443`. With `originServerName` empty, `cloudflared` expects the certificate to cover `localhost`. Setting `originServerName: staging-api.internal.example.com` fixes the name mismatch and sets SNI. If the log says 'signed by unknown authority', point `caPool` at the private root CA. `noTLSVerify: true` accepts any certificate and is only a temporary last resort. The zone's SSL/TLS mode does not affect this hop. The wrong site comes from the `Host` header: `cloudflared` forwards the visitor's `staging-api.example.com`, so set `httpHostHeader` to the internal name; the original moves to `X-Forwarded-Host`.
code
yaml · 11 linestunnel: 6ff42ae2-765d-4adf-8112-31c55c1551ef
credentials-file: /etc/cloudflared/6ff42ae2-765d-4adf-8112-31c55c1551ef.json
ingress:
- hostname: staging-api.example.com
service: https://localhost:8443
originRequest:
originServerName: staging-api.internal.example.com
caPool: /etc/cloudflared/corp-root-ca.pem
httpHostHeader: staging-api.internal.example.com
- service: http_status:404go deeper
Recall that a 502 through a connected tunnel points at the hop from cloudflared to the local service, and that its logs name the exact error.
Explain the originRequest settings: originServerName for the expected name and SNI, caPool for a private CA, httpHostHeader for the Host, and noTLSVerify as the last resort.
Diagnose from the log line, fix with verification kept on, and explain why the zone's SSL/TLS mode and Flexible are not the lever for this hop.
Set the team rule that noTLSVerify never ships to production without an expiry, and that private origins get their CA distributed through caPool.
## The setup The staging API sits behind an internal reverse proxy on `https://localhost:8443`. That proxy serves several sites and picks one by the `Host` header. Its certificate is issued by the company's **private certificate authority** for `staging-api.internal.example.com`. The public name is `staging-api.example.com`, routed through a Cloudflare Tunnel. ## First, locate the failure A `502 Bad Gateway` reading "Unable to reach the origin service" means the **tunnel is connected** and the failure lies **between `cloudflared` and the local service**. That is a different fault from error `1033`, where no connector is available. The tunnel's Healthy status says nothing about this hop. The `cloudflared` logs, at `error` level, name the cause: | Log line | Cause | |---|---| | `dial tcp ...: connect: connection refused` | Nothing is listening on that port, or the port in the rule is wrong | | `malformed HTTP response` | The rule says `http://` but the service speaks TLS, or the reverse | | `x509: certificate is valid for staging-api.internal.example.com, not localhost` | The certificate does not cover the name `cloudflared` checks | | `x509: certificate signed by unknown authority` | `cloudflared` does not trust the issuing CA | ## The TLS fixes, in order of preference `cloudflared` verifies the origin certificate like any TLS client. With **`originServerName`** empty, it expects the certificate to cover the host in the service URL, here `localhost`, so a certificate for the internal name fails. 1. **`originServerName: staging-api.internal.example.com`** tells `cloudflared` which name to expect, and it also sends that name as the TLS **SNI**. That fixes the name mismatch with verification still on. 2. **`caPool: /etc/cloudflared/corp-root-ca.pem`** adds the private root certificate to the trust pool `cloudflared` uses for this origin. That fixes "unknown authority". Adding the CA to the host's system trust store and restarting works too. 3. **`noTLSVerify: true`** accepts **any** certificate. Cloudflare documents it as a temporary last resort, to be turned off once trust is fixed. Left on, it means anything that can answer on that address can pose as the staging API. The zone's SSL/TLS encryption mode does not touch this hop. It governs Cloudflare's own connection to an origin, while `cloudflared` checks the local certificate itself. Switching to **Flexible** fixes neither error; the encryption-modes lesson covers what each mode does. ## The second symptom: the wrong site With TLS fixed, the API answers with the reverse proxy's default site. By default `cloudflared` sends the **visitor's `Host`**, `staging-api.example.com`, and the reverse proxy has no site under that name. - **`httpHostHeader: staging-api.internal.example.com`** rewrites the `Host` sent to the origin. `cloudflared` then puts the original value in **`X-Forwarded-Host`**, so the application can still see the public name. - **`matchSNItoHost: true`** makes the TLS SNI follow the `Host` sent to the origin, which is `httpHostHeader` when that is set. It suits one entry point that serves many names by SNI, such as wildcard routing, and saves setting `originServerName` per rule. The corrected rule: ```yaml tunnel: 6ff42ae2-765d-4adf-8112-31c55c1551ef credentials-file: /etc/cloudflared/6ff42ae2-765d-4adf-8112-31c55c1551ef.json ingress: - hostname: staging-api.example.com service: https://localhost:8443 originRequest: originServerName: staging-api.internal.example.com caPool: /etc/cloudflared/corp-root-ca.pem httpHostHeader: staging-api.internal.example.com - service: http_status:404 ``` ## A diagnosis order that saves time 1. From the `cloudflared` host, call the service the way the rule does, for example `curl -v https://localhost:8443`. That separates "service down" from "tunnel misconfigured". 2. Read the `cloudflared` log line for the failing request. The error text names the layer: TCP, protocol or certificate. 3. Fix one layer at a time, run `cloudflared tunnel ingress validate`, restart, and retest. 4. When the response arrives but is wrong, compare the `Host` the origin expects with the visitor's hostname. Two habits keep this from coming back. Give the origin a certificate whose name you can set in `originServerName`. Distribute the private CA file with the connector's configuration, so `caPool` points at a file every replica has. ## Where the settings go - In `config.yml`, `originRequest` can sit at the **top level**, as defaults for every rule, or **inside one rule** to override them for that service. - For a remotely-managed tunnel, the same settings appear in the dashboard under a published route's additional application settings (TLS, HTTP and Connection groups). - Command-line flags such as `--origin-ca-pool` and `--no-tls-verify` apply only to a single origin given with `--url`. With ingress rules, put the settings under `originRequest`. - After editing, run `cloudflared tunnel ingress validate` and restart the connector.
- Why not leave noTLSVerify: true once the API works?It makes `cloudflared` accept any certificate, so anything that can answer on that address or path, a misconfigured service or an interposed proxy, is trusted as the staging API. Cloudflare documents it as a temporary last resort. Set `originServerName` and `caPool` instead, and verification keeps doing its job.
- When would you use matchSNItoHost instead of originServerName?When one origin entry point serves many hostnames and picks a certificate by SNI, as with wildcard routing. `matchSNItoHost: true` sets SNI to the `Host` sent to the origin (the `httpHostHeader` value when set) on each request, so you do not have to write an `originServerName` per rule.
- Would switching the zone to Full (strict) make cloudflared validate the certificate?No. The zone's encryption mode governs Cloudflare's own connection to an origin. `cloudflared` checks the local certificate itself, through `originServerName`, `caPool` and `noTLSVerify`. Full (strict) is compatible with a correct tunnel route, but it does not replace those settings, and Flexible fixes nothing here.
- A 502 through the tunnel means cloudflared is disconnected from Cloudflare
- Setting the zone to Flexible fixes cloudflared's x509 errors
- noTLSVerify: true is a fine permanent setting for internal services
- cloudflared sends localhost as the Host header by default
- httpHostHeader on its own changes the name cloudflared verifies on the certificate
- The --no-tls-verify command-line flag also applies to every ingress rule