DNS filtering
Secure Web Gateway allows you to inspect DNS traffic — the queries your devices make to translate domain names like example.com into IP addresses — and control which websites users can visit. Because every connection starts with a DNS lookup, DNS filtering blocks threats at the earliest stage of a connection, before the device ever reaches the destination. Use DNS policies to block malware domains, phishing sites, or entire content categories across your organization.
You can filter DNS queries from individual devices (for example, employee laptops) or from entire network locations (for example, an office router). Choose the option that matches your deployment.
To filter DNS requests from an individual device such as a laptop or phone:
- Install the WARP client on your device. WARP is a lightweight agent that routes the device's DNS queries through Cloudflare so Gateway can inspect and filter them.
- In the WARP client, open Settings and log in to your organization's Zero Trust instance. This tells WARP which Gateway policies to enforce.
- (Optional) If you want to display a custom block page instead of a generic browser error when a request is blocked, install a Cloudflare root certificate on your device.
To filter DNS requests from a network location such as an office or data center without installing software on each device:
- Add the location to your Cloudflare One settings. A DNS location represents a network (such as an office) whose DNS queries you want to filter.
- On your router, browser, or OS, change the DNS server setting to point to the Cloudflare address shown in the location setup UI. This forwards all DNS queries from that network through Gateway.
To confirm that your device's DNS queries are flowing through Gateway:
- In Cloudflare One ↗, go to Traffic policies > Traffic settings.
- Under Log traffic activity, enable activity logging for all DNS logs.
- On your device, open a browser and go to any website.
- In Cloudflare One, go to Insights > Logs > DNS.
- Make sure DNS queries from your device appear.
A DNS policy has two parts: a traffic condition that defines which queries to match (for example, all queries to gambling sites) and an action that defines what to do with matching queries (for example, block them). To create a new DNS policy:
- In Cloudflare One ↗, go to Traffic policies > Firewall policies.
- In the DNS tab, select Add a policy.
- Name the policy.
- Under Traffic, use the condition builder to define which DNS queries this policy applies to. Select a selector (such as Security Categories), an operator (such as in), and one or more values.
- Choose an Action to take when traffic matches the condition. For example, we recommend adding a policy to block all security categories:
Selector Operator Value Action Security Categories in All security risks Block - Select Create policy.
-
Create an API token with the following permissions:
Type Item Permission Account Zero Trust Edit -
(Optional) Configure your API environment variables to include your account ID and API token.
-
Send a
POSTrequest to the Create a Zero Trust Gateway rule endpoint. For example, the following request creates a policy that blocks all default security categories. The numeric IDs in thetrafficfield (such as68,178,80) correspond to Cloudflare's predefined security threat categories — refer to domain categories for the full mapping. Theprecedencefield controls evaluation order when multiple policies match (0means this policy is evaluated first).Create a Zero Trust Gateway rule curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \--request POST \--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \--json '{"name": "Block security threats","description": "Block all default Cloudflare DNS security categories","precedence": 0,"enabled": true,"action": "block","filters": ["dns"],"traffic": "any(dns.security_category[*] in {68 178 80 83 176 175 117 131 134 151 153})","identity": ""}'{"success": true,"errors": [],"messages": []}The API will respond with a summary of the policy and the result of your request.
For more information, refer to DNS policies.
Once your first policy is active, refer to common DNS policies for other policies you may want to add. Common additions include blocking specific content categories (such as social media or streaming), enabling SafeSearch on search engines, and restricting DNS queries so devices can only use resolvers that you have approved.