Is there an elegant way to block a bunch of referrers at once?

Solution 1:

I would try a map:

map $http_referer $bad_referer {
    default                  0;
    "~spamdomain1.com"       1;
    "~spamdomain2.com"       1;
    "~spamdomain3.com"       1;
}

Then use it like so:

if ($bad_referer) {
    return 444;
}

Solution 2:

You could use logical OR to craft one multi match statement e.g.

if ($http_referer ~ "spamdomain1\.com|spamdomain2\.com|spamdomain3\.com")  { 
  return 444;
}

EDIT per comment; removing break; from the block


Solution 3:

ngx_http_referer_module is another way to do it. Example from Referer Spam Blocking:

location / {
  valid_referers none blocked *.badreferer1.com badreferer2.com *.badreferer3.com badreferer4.net;

  if ($invalid_referer) {
    return   403;
  }
}

Tags:

Nginx