How to Use Nginx Map to Block Referrer Spam on Your VPS

Open your analytics after a quiet weekend and you might find visits from domains you have never heard of, linking to pages that do not exist on your site. That is referrer spam: junk traffic sent by bots that forge the HTTP Referer header so their domain shows up in your reports. It brings no real visitors, skews your numbers, and wastes log space and a little bandwidth. The fix is simple and lives entirely in your web server. An nginx map to block referrer spam lets you match incoming requests against a list of bad domains and drop them with a 403 before they ever reach WordPress or your application. This guide sets it up on your VPS, tests it safely, and keeps the list current.

What referrer spam actually does to your site

Referrer spam comes in two shapes. Ghost spam never touches your server: bots send fake hits straight to your analytics service, leaving only a strange domain in your referral report. Crawler spam is the opposite: bots really request your pages and send a spammy Referer header with each request, so the domain lands in both your analytics and your access log.

The crawler kind is the one you can fight at the server level. Each junk request costs a little CPU, writes lines to your log, and can trigger cache misses or PHP workers for pages no human will ever read. On a small VPS those requests add up, especially when a spam network hammers the same site for days. You can spot the pattern quickly once you know where to find your nginx access and error logs and how to read them: the same few odd domains repeat across hundreds of lines, often hitting the homepage or random old posts.

How the nginx map to block referrer spam works

The nginx map directive turns one variable into another based on matching rules. Here the input is $http_referer, the Referer header each browser or bot sends, and the output is a simple flag: 0 for a normal visitor, 1 for a known spam domain. Your server block then checks that flag and refuses the request.

A map beats a long chain of if statements for three reasons. First, nginx evaluates it once per request, so the cost stays tiny even with hundreds of domains on the list. Second, one map can serve every site on the server, so you maintain a single list instead of copying rules into each server block. Third, the list lives in its own file, which keeps your main config readable and makes updates a one-line edit. The nginx map to block referrer spam setup in this guide uses exactly that shape: one map file, one flag, one rule in the server block.

Step 1: Create the map file

Create a file at /etc/nginx/referrer-spam.map and fill it with the domains you want to block:

map $http_referer $bad_referer {
    default 0;
    ~*spamdomain-one.com 1;
    ~*free-traffic.xyz 1;
    ~*cheap-seo-services.net 1;
    ~*buttons-for-website.com 1;
}

The ~* prefix means a case-insensitive regular expression, so SpamDomain-One.COM matches the same rule as the lowercase version. Dots are escaped with a backslash, because an unescaped dot in a regex matches any character. The match is a substring match, so spamdomain-one.com also catches www.spamdomain-one.com and its subdomains. The default 0 line means every referrer not on the list passes through untouched: you block only what you name.

Start your referrer spam nginx list with the domains you have actually seen in your own logs rather than copying a giant blocklist from the internet. A list you built from your own traffic is shorter, easier to audit, and far less likely to block a legitimate referrer by mistake.

Step 2: Load the map and block the matches

A map must live in the http context, so it cannot sit inside a server block. Open /etc/nginx/nginx.conf, find the http { section, and add one include line near the top:

http {
    include /etc/nginx/referrer-spam.map;
    ...
}

Then, inside the server block of each site you want to protect, add the rule that will block fake referrers:

server {
    listen 443 ssl;
    server_name example.com;

    if ($bad_referer) {
        return 403;
    }

    ...
}

You may have read that if in nginx is risky. That warning applies to if inside location blocks, where it can behave in surprising ways. An if at server level that only does a return is safe and is the standard pattern for this job. If you want to understand why the placement matters, read up on how nginx location block priority and request matching really work before you rearrange anything.

Returning 444 instead of 403 closes the connection silently and saves a little bandwidth, but blocked requests get harder to spot in your logs. Stick with 403 for a first setup: you can see it working.

Step 3: Test the config and reload without downtime

A typo in the map file can stop nginx from starting, so always test before you reload. Run:

sudo nginx -t
sudo systemctl reload nginx

If the test passes, reload. Then prove the block works with two curl requests. The first pretends to come from a spam domain, the second from a normal referrer:

curl -I -e "https://spamdomain-one.com/" https://example.com/
curl -I -e "https://www.google.com/" https://example.com/

The first should answer HTTP/2 403 and the second HTTP/2 200. If both return 200, the map is not loaded: check that the include line sits inside the http block and that you reloaded after editing. Keep the same test-first routine whenever you test a new nginx config without taking your site down.

Finding the domains worth blocking

Your own access log is the best source for the blocklist. This one-liner pulls the most common referrers from the last day of traffic:

grep -oP '"https?://K[^"/ ]+' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -30

Scan the output for domains you do not recognize, then check one or two in a search engine before adding them. A real spam domain usually has no genuine website behind it, or it leads to a page selling traffic, SEO services, or free backlinks. Cross-check the same names in your analytics referral report: domains that appear there but never convert are strong candidates.

Never block a domain just because it looks odd. Payment gateways, social networks, and translation services all send legitimate referrers, and blocking one can break checkouts or hide real traffic. When in doubt, leave it off the list and watch it for another week.

Keeping the list current

Spam networks rotate domains, so a list written once slowly goes stale. Repeat the log scan monthly, add the new names, and remove domains that have not appeared in months. A shorter nginx spam referrer list is faster to review and less likely to hold a stale mistake.

Keep the map file in version control or in your regular server backups: rebuilding it from memory after a server move wastes an afternoon. The map is one layer of defence, not the whole wall. Pair it with nginx rules that block bad bots and scrapers and, if one network keeps hammering you, rate limiting so repeated requests from one source slow to a crawl instead of filling your logs.

What this setup will not stop

A referrer map only acts on requests that reach your server, so ghost spam that goes straight to your analytics provider is untouched by it. That kind needs filters inside your analytics tool, not in nginx. Bots that send no Referer header slip past too, since there is nothing for the map to match, and a spammer can always forge the referrer of a legitimate site.

Think of the map as a filter for the noisy, obvious junk. For anything heavier, a flood of requests from many IPs or an attack that ignores the referrer entirely, you need rate limiting, a firewall, or a web application firewall in front of the server. Clean reports and short logs make those bigger problems easier to spot when they arrive.

Frequently asked questions

Will blocking referrer spam hurt my SEO?

No. Search engines do not send spam referrers, and refusing junk requests does not affect rankings. Real visitors and Googlebot send normal headers, so they pass the map untouched.

Should I return 403 or 444?

Use 403 while you are learning the setup: it shows up clearly in logs and curl tests. Switch to 444 later to save bandwidth on high-volume spam, once you trust the list.

Can I block by IP address instead?

You can, but referrer spam rotates through many IPs, so an IP blocklist grows fast and goes stale faster. A domain map stays short because the spam domains change far less often than the addresses behind them.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.