AI bots

How to block AI crawlers in nginx and Apache

Block GPTBot, ClaudeBot and other AI crawlers in nginx or Apache with a user agent rule, keep AI search allowed, test with curl.

On this page 7 sections

To block an AI crawler on your own server, match its user agent and answer 403: a map and return 403 in nginx, mod_rewrite in Apache 2.4. Do it after robots.txt and only for bots you really want to stop, because a user agent rule is easy to dodge and easy to overdo.

Start with robots.txt, then enforce

  1. robots.txt asks a bot to stay away. Bots that honour it will. See robots.txt and AI bots for a file to copy.
  2. A server rule enforces your choice for bots that ignore robots.txt.
  3. Verification tells you whether the traffic is who it says it is. See how to verify AI crawlers.

A server rule has two limits. A user agent is a header the client writes itself, so a bot that lies passes. Apache's own access control guide warns that "access control by User-Agent is an unreliable technique, since the User-Agent header can be set to anything at all". And a wide rule can block bots you want, such as AI search; see whether to block AI crawlers.

Match training bots, not search or user fetchers

Most operators run separate bots for training, search and user requests. Block the first and leave the others to stay in AI answers. Tokens come from each operator's documentation.

Operator Training AI search Fetches for a person
OpenAI GPTBot OAI-SearchBot ChatGPT-User
Anthropic ClaudeBot Claude-SearchBot Claude-User
Perplexity none documented PerplexityBot Perplexity-User
Meta meta-externalagent Meta-WebIndexer meta-externalfetcher
Common Crawl CCBot none none

Sources: OpenAI, Anthropic, Perplexity, Meta and Common Crawl. Meta says meta-externalagent crawls "to train foundation AI models or to improve products by indexing content directly", so it is not training only. Perplexity says PerplexityBot "is not used to crawl content for AI foundation models".

Two tokens never appear as a user agent, so a server rule for them does nothing:

  • Google-Extended. Google says it "doesn't have a separate HTTP request user agent string". It is a robots.txt token only. See Google-Extended.
  • Applebot-Extended. Apple says it "does not crawl webpages". It only governs how data crawled by Applebot is used. Use robots.txt.

Do not match "bot" alone: that also blocks Googlebot and Bingbot.

Fetchers that act for a person may not follow robots.txt. OpenAI says "robots.txt rules may not apply" to ChatGPT-User, and Meta says meta-externalfetcher may bypass it. A server rule is the only block that reaches them, and it also stops people who asked an assistant to read your page.

nginx

Put the map in the http block and the check in the server block.

# http block
map $http_user_agent $ai_agent {
    default 0;
    "~*(GPTBot|ClaudeBot|CCBot|meta-externalagent)" 1;
}

# Let every bot read robots.txt, so it can see your rules.
map "$ai_agent:$uri" $block_ai {
    default 0;
    "1:/robots.txt" 0;
    "~^1:" 1;
}

# server block
server {
    # ... your existing listen, server_name, root and locations

    if ($block_ai) {
        return 403;
    }
}

The ~* prefix makes the match case-insensitive (nginx map documentation). nginx checks the exact string 1:/robots.txt before the regular expression, so robots.txt stays readable. The if sits at server level so it covers every location; inside location / alone it would miss requests a more specific location handles. A return inside if is the safe use. Run nginx -t, then reload.

Apache 2.4

Use mod_rewrite, case-insensitive. The Apache documentation says to put such rules in the main server configuration rather than .htaccess, which is read on every request. Put this inside your VirtualHost:

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} (GPTBot|ClaudeBot|CCBot|meta-externalagent) [NC]
RewriteCond %{REQUEST_URI} !^/robots\.txt$
RewriteRule .* - [F,L]

[F] answers 403 and [L] stops further rules. The second condition keeps robots.txt readable.

We do not suggest a Require block in a site-wide <Location>: Apache merges <Location> sections after directory and .htaccess rules, so its Require lines can replace the access rules that protect private folders.

On shared hosting, put the same rewrite lines in .htaccess where the host allows it. It costs more per request, which is why the Apache documentation prefers the main file.

We tested the rewrite rules on Apache 2.4: GPTBot and claudebot got 403, OAI-SearchBot and a browser string got 200, and robots.txt answered 200 to GPTBot. We did not run the nginx config on a live server, so use nginx -t before you reload.

Test with curl and read the log

Send a request with the bot's name and one without, using your own domain.

curl -s -o /dev/null -w "%{http_code}\n" -A "GPTBot" https://example.com/
curl -s -o /dev/null -w "%{http_code}\n" -A "OAI-SearchBot" https://example.com/
curl -s -o /dev/null -w "%{http_code}\n" -A "GPTBot" https://example.com/robots.txt

You want 403, 200 and 200. The test sends the name only, so it shows what your rule does, not what the real bot receives.

To see real traffic, count status codes for a bot in your access log (the ninth field in the combined format).

grep -i "GPTBot" /var/log/nginx/access.log | awk '{print $9}' | sort | uniq -c

A blocked bot shows 403. A 200 for a name you meant to block means the rule is not matching, or a proxy changed the header; behind a CDN, also check the CDN's log. A name in a log line can be spoofed, so verify it first.

How Lundot handles this

Lundot reads requests at Cloudflare, so verified AI requests need a site behind Cloudflare; it does not read server logs. The free AI crawler check works for any site and reads your live robots.txt for each bot, but it will not show your server rule. See Connect Cloudflare and the AI bots report.

Sources

Put it into practice

  • DocsConnect Cloudflare

    Record verified AI bot requests at Cloudflare: what is added, what access is asked for, how it is checked.

  • DocsAI bots

    Verified AI crawler, assistant and agent requests, per page and company.

  • ProductSee which AI bots request your pages, verified

    Lundot records AI crawler, assistant and agent requests at Cloudflare, per page and company, and shows how each was verified. Unverified claims never count.

See what your robots.txt tells AI crawlers

The AI crawler check is free and needs no account. It reads your robots.txt for each AI crawler, checks whether your home page has text without JavaScript, and looks for llms.txt.