With Cloudflare Origin Rules, how do you send /api requests to another backend host and port, and which overrides need Enterprise?
answer
- four overrides in one rule
- port works on every plan
- Host header also rewrites SNI
- DNS override needs a same-account hostname
- later matching rules merge over earlier
basics
~20 sWrite an Origin Rule matching starts_with(http.request.uri.path, "/api/") that overrides the destination port, available on every plan, and on Enterprise the DNS record and Host header, so Cloudflare connects to the API backend and presents its name.
solid answer
~40 sAn Origin Rule has an expression and up to four overrides: the `Host` header, SNI, the DNS record (resolve override) and the destination port (1 to 65,535). Only the port override exists below Enterprise; Host header, SNI and DNS record overrides are Enterprise-only. On Enterprise, point the DNS record override at a hostname on the same account, such as a proxied `CNAME` for the API server, set the port, and rewrite `Host` if the backend serves another name; the DNS override moves the connection, while the Host override only changes the header. A Host override also sets SNI to the same value unless an SNI override says otherwise. When several rules match they merge, later settings winning. Below Enterprise, give the API its own proxied hostname, or use a Snippet or Worker.
code
json · 12 lines{
"expression": "starts_with(http.request.uri.path, \"/api/\")",
"description": "Send /api to the API backend",
"action": "route",
"action_parameters": {
"host_header": "api-origin.example.com",
"origin": {
"host": "api-origin.example.com",
"port": 8443
}
}
}go deeper
Recall the four Origin Rules overrides and that only the destination port is available below Enterprise.
Explain why a DNS record override and a Host header override are usually set together, how the Host override carries the SNI with it, and how matching rules merge.
Diagnose the failures: a Host-only override reaching the old server, a certificate mismatch from the inherited SNI, a DNS override to a hostname outside the account.
Decide whether a path-based backend split is worth Enterprise overrides or whether a separate API hostname is the simpler contract for clients and certificates.
## What Origin Rules change **Origin Rules** change where Cloudflare sends a request, or how it reaches that server, without the visitor seeing any difference in the URL. Each rule has a filter expression and up to four overrides: - **Host header**: rewrites the `Host` header the origin receives. - **Server Name Indication (SNI)**: the hostname Cloudflare sends in the TLS handshake with the origin. - **DNS record**, also called resolve override: makes Cloudflare resolve a different hostname and so connect to a different server. - **Destination port**: connects to another port, anywhere from 1 to 65,535. They run in the `http_request_origin` phase, after URL Rewrite Rules and Configuration Rules, use the `route` action in the API, and need the hostname proxied through Cloudflare. ## Plan boundaries | Override | Free | Pro | Business | Enterprise | |---|---|---|---|---| | Destination port | Yes | Yes | Yes | Yes | | Host header | No | No | No | Yes | | SNI | No | No | No | Yes | | DNS record | No | No | No | Yes | The number of Origin Rules per zone is 10 on Free, 25 on Pro, 50 on Business and 300 on Enterprise. The table shapes the design: sending `/api/` to port 8443 on the same origin works on every plan, while sending it to a different host needs Enterprise. ## Routing /api to another backend on Enterprise 1. Create a DNS record for the backend in the account, for example a proxied `CNAME` named `api-origin.example.com` that points at the API server's own hostname. A DNS record override must name a hostname on the same Cloudflare account, though it may sit in another zone. 2. Write an Origin Rule with the expression `starts_with(http.request.uri.path, "/api/")`. 3. Set **DNS Record** > **Override to** `api-origin.example.com` and **Destination Port** > **Rewrite to** the backend's port. 4. Set **Host Header** > **Rewrite to** the name the backend serves, if it differs from the visitor's hostname. The two hostname overrides do different jobs. The DNS record override moves the connection; the Host header override changes only the header. Set only the Host header and the request still goes to the zone's usual origin, carrying a name that origin may not serve. ## Host header and SNI A Host header override also sets the SNI to the same value, as a Page Rule's Host Header Override did. If the backend's certificate is for a different name, add an SNI override in the same rule or in a separate one; its value must be a static hostname on the same account. On a zone using Cloudflare for SaaS, an SNI override also wins over the SNI rewrite of a custom origin. ## When several rules match Matching Origin Rules are merged. Later rules override the settings that earlier rules set and add the ones they did not. A first rule that sets port 8081 and a second that sets the Host header and DNS hostname produce a request with all three; a port in the second rule would replace 8081. Three more precedence facts: - An Origin Rule's Host header or DNS record override wins over a Host header override in the load balancer configuration. - An Origin Rule wins over a Page Rule doing the same override; if the Page Rule sets only the Host header and the Origin Rule only the DNS record, the request gets both. - For a request a Worker handles, a Host header override is not taken into account when Workers routes are evaluated. ## Below Enterprise Without Host and DNS record overrides, the options are: - Give the API its own proxied hostname, such as `api.example.com`, that points at the backend; the frontend then calls that name and no override is needed. - Use the destination port override when the API is the same server listening on another port. - On Pro and above, run a Snippet for `/api/` requests that fetches the backend's hostname. - Use a Worker, with the cost and operation of code. Whichever path you take, remember that the ports visitors can reach through the proxy are a separate, fixed list; the destination port override only changes the port Cloudflare connects to on the origin.
- What happens if you set only the Host header override and not the DNS record override?Cloudflare still connects to the zone's normal origin, the one the visitor's hostname resolves to; only the Host header and SNI change. If that origin does not serve the new name, it answers with its default site or an error. To reach another server, add the DNS record override.
- Two Origin Rules match: the first sets port 8081, the second sets the Host header and DNS hostname. What does the request get?All three settings. Matching Origin Rules merge, with later rules overriding what earlier ones set and adding what they did not, so the request goes to the second rule's hostname, with its Host header, on port 8081. A port in the second rule would replace 8081.
- After a Host header override, the origin's TLS handshake fails on the certificate name. What is the fix?The Host override also changed the SNI to the same name, and the backend's certificate is for another one. Add an SNI override, in the same rule or a separate one, with the name the certificate covers; it must be a static hostname on the same Cloudflare account.
- A Host header override on its own sends the request to a different server.
- Origin Rules on a Pro zone can override the Host header.
- The destination port override only accepts Cloudflare's proxied HTTP ports.
- A DNS record override can name any external hostname directly.
- When two Origin Rules match, only the first one applies.