diff --git a/docs/cloud/redirects.md b/docs/cloud/redirects.md index 43cbd4169..9320db423 100644 --- a/docs/cloud/redirects.md +++ b/docs/cloud/redirects.md @@ -40,7 +40,7 @@ The `request` object contains these properties: - `url` — A [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL) object wrapping the original request’s full URL. - `uri` — Shorthand for everything after the origin in the original request, and similar to `$_SERVER['REQUEST_URI']` in PHP. It is equivalent to `{request.url.pathname}{request.url.search}{request.url.hash}`. - `method` — The request’s [HTTP verb](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Methods). -- `headers` — A map of request headers and values. (Headers with dashes can be used in `destination` substitutions like any other property, e.g: `{request.headers.Content-Type}`) +- `headers` — A map of request header names (case-insensitive) and values. ### HTTP Status Codes @@ -150,3 +150,62 @@ rewrites: ``` Headers are combined with those from the `destination`, overwriting any shared names. + +These are response headers, sent back to the browser. To change the request sent to the destination, use [`request`](#request-headers-and-cookies). + +### Request Headers and Cookies + +When a rule’s `destination` is anything other than our CDN or one of the environment’s own [domains](domains.md), the gateway forwards only these headers from the visitor’s request: + +- `Content-Type`, `Content-Encoding`, and `Content-Language` +- `Range` +- `If-Match`, `If-None-Match`, `If-Modified-Since`, `If-Unmodified-Since`, and `If-Range` + +To send other request headers, add a `request` key to the rule. + +This example proxies a Google tag gateway path through your own domain, and forwards the Google Analytics cookies plus the visitor’s user agent, IP address, and location: + +```yml +rewrites: + - pattern: + pathname: '/abc123{/*}?' + destination: 'https://G-XXXXXXX.fps.goog/abc123/{matches.pathname.groups.0}{request.url.search}' + request: + cookies: [_ga, _ga_XXXXXXX] + headers: + User-Agent: '{request.headers.user-agent}' + X-Forwarded-For: '{request.headers.cf-connecting-ip}' + X-Forwarded-Country: '{request.cf.country}' + X-Forwarded-Region: '{request.cf.regionCode}' +``` + +The `request` options only apply to these destinations. The gateway doesn’t cache responses for rules that use them, and returns the destination’s redirects to the browser instead of following them. + +#### Cookies + +`request.cookies` is a list of cookie names. The gateway sends each one with the raw value the visitor’s browser sent. It leaves out cookies the browser didn’t send, and cookies with a name or value that isn’t valid in a `Cookie` header. + +#### Headers + +`request.headers` maps header names to values. Values are parsed as templates, using the same placeholders as `destination`, plus two objects that only header templates can read: + +- `request.cookies`: The visitor’s cookies, by name, with the raw values the browser sent. Put names that contain dots in brackets: `{request.cookies["app.id"]}`. +- `request.cf`: [Request data from Cloudflare](https://developers.cloudflare.com/workers/runtime-apis/request/#incomingrequestcfproperties), such as `country`, `regionCode`, and `city`. + +```yml +headers: + # Replaces the visitor's Content-Type header + Content-Type: 'application/json' + # Not sent when the visitor has no `_ga` cookie + X-Client-Id: '{request.cookies._ga}' + # Not sent, because `request.cf` is an object + X-Location: '{request.cf}' +``` + +Rules can’t set these headers, and the gateway skips them: + +- `Host`, `Content-Length`, `Accept-Encoding`, and `Expect` +- Hop-by-hop headers, like `Connection`, `Keep-Alive`, `Transfer-Encoding`, and `Upgrade` +- `Cookie` (use [`request.cookies`](#cookies) instead) +- `Signature`, `Signature-Input`, and `Signature-Agent` +- Any header starting with `cf-`, `x-cf-`, `x-gateway-`, `x-amz-`, or `x-amzn-`, except `CF-Access-Client-Id` and `CF-Access-Client-Secret` (for destinations behind [Cloudflare Access](https://developers.cloudflare.com/cloudflare-one/identity/service-tokens/))