---
title: "Protect a distribution with a WAF"
description: "Attach a WAF to a distribution, so its managed rule sets and your own rules inspect every request before it reaches your origin."
---

> Documentation Index
> Fetch the complete documentation index at: https://docs.ezghcloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Protect a distribution with a WAF

A [WAF](/waf) attached to a distribution inspects every request to the distribution's hostnames,
at every location, before it reaches your origin. It's the same WAF you attach to a load balancer:
the same [managed rule sets](/waf/managed-rule-sets), [custom rules](/waf/custom-rules),
[rate limits](/waf/rate-limits), log-only mode and [request logs](/waf/logging). A request it
blocks never reaches your origin.

## Before you start

- A WAF in the distribution's project. See [Create a WAF and attach it](/waf#create-a-waf-and-attach-it)
  for writing one; leave it in log-only mode at first to see what it would block.
- `cdn.distributions.update` on the distribution and `waf.wafs.attach` on the WAF.

## Attach it

1. In the console, open the distribution and select **Attach WAF** in its **WAF** section, choose
   the WAF and save. With the CLI:

   ```sh
   ezgh cdn distributions update cdn_k2m9x0a7qpe4 --waf waf_4k2m9x0a7qpe
   ```

   On a new distribution, choose it in the **WAF** section of **Create distribution**, or
   `ezgh cdn distributions create … --waf waf_4k2m9x0a7qpe`.

2. The distribution's WAF status is **Applying** (`deploying` in the API) until every location
   inspects its requests, then **Applied** (`active`). Each location starts as soon as it's ready.

**Change WAF** attaches another one in its place. A WAF can be attached to several distributions
and load balancers; a distribution has at most one WAF.

## What it inspects

- Every request to the distribution: its method, path, query, headers and cookies, and its client
  IP, country and AS number.
- The request body, when the request gives its length (`Content-Length`) and it's at most 16 KB.
  Larger bodies, bodies sent without a length and WebSocket upgrades are inspected on everything but
  the body, and reach your origin unchanged.
- The path the visitor asked for: the WAF runs before [rewrites](/cdn/guides/serve-a-single-page-app),
  so rules match your public URLs.

A blocked request is answered `403` with a short page naming its request ID, which is also the
`requestId` of its [log record](/waf/logging). Requests to `/cdn-cgi/` paths aren't inspected.

## Rate limits

A rate limit counts each client's requests at each location separately, and each distribution
separately: a WAF attached to two distributions counts their requests apart.

## Request logs

With logging on, the WAF's records land in its environment as they do for a load balancer, with
`distributionId` where a load balancer's records have `loadBalancerId` and `listenerId`.

## Detach it

Select **Detach** in the distribution's **WAF** section, or:

```sh
ezgh cdn distributions update cdn_k2m9x0a7qpe4 --no-waf
```

Its requests stop being inspected. The WAF stays in the project, and can't be deleted while any
distribution or load balancer has it.

## With Terraform

```terraform
resource "ezghcloud_cdn_distribution_waf" "web" {
  project_id      = var.project_id
  distribution_id = ezghcloud_cdn_distribution.web.id
  waf_id          = ezghcloud_waf.web.id
}
```

`ezghcloud_cdn_distribution` doesn't manage the WAF, so the two never undo each other.

## With the API

A create can attach one ([CreateDistribution](/cdn-api/CreateDistribution/)):

```json
{
  "name": "storefront",
  "origin": { "host": "shop-lb.example.com", "port": 443, "protocol": "https", "hostHeader": "viewer" },
  "waf": { "wafId": "waf_4k2m9x0a7qpe" }
}
```

On an existing distribution, a replace ([UpdateDistribution](/cdn-api/UpdateDistribution/))
is the whole document: read the distribution, send back everything it has (its domains, rewrites,
tags and description included) with `waf` added, and `If-Match` set to the ETag of the
read. Anything you leave out takes its default, except `waf`: a replace that leaves `waf` out keeps
the attached WAF, so a client that doesn't know the field never detaches it. `"waf": null`
detaches it.

The distribution's `waf` shows `wafId`, `status` (`deploying`, `active`, or `failed` when a
location couldn't apply the WAF's latest version and keeps inspecting with the one it had) and
`version`, the WAF version `status` refers to.

Attaching is refused with `409 waf_mismatch` for a WAF in another project or region, and `403`
without `waf.wafs.attach`.

## Pricing

$5.00 per distribution per month with a WAF attached, billed by the hour, plus Observability ingest
for request logs. See [WAF pricing](/waf/pricing).

Source: https://docs.ezghcloud.com/cdn/guides/protect-with-a-waf/index.mdx
