How to Block Spammers based on keywords in NGINX

How to Block Spammers based on keywords in NGINX

It seems I spend most of my time moderating comments on my websites just to delete spam.  It is still a daily task which sometimes is amazing how much the Wordpress Askimet plugin catches.  It works amazingly well but it still requires time to sort through all the junk mail to ensure nothing was caught by accident.

I stumbled across a feature in NGINX that helps you block website visitors based on the keywords they have in their HTTP header.  For example, if a spammer posts a comment about Viagra.  NGINX catches the keyword Viagra in the HTTP header and redirects the user to another page.

Brilliant right?

Steps for blocking Spam with NGINX

  • Navigate to your NGINX sites-enabled config file.  Usually located in: /etc/nginx/sites-enabled/<config file name>
  • Edit the config file with your preferred editor
  • Copy the below code to your config file:
`# Block HTTP Headers based on keywords and redirect to 403 if ($http_referer ~* (viagra¦cialis¦levitra¦mulberry¦laurent) ) { return 403; }`
  • Modify the keywords to block specific keywords which are giving you trouble. Be sure to separate the keywords with a pipe ¦
  • Save your changes
  • Run the command nginx -t to test the config file for errors.  Only if it passes successfully should you continue to the next step.  If not go back to the config and fix the problems first
  • Reload NGINX by running : service nginx reload

Follow me

If you liked this article be sure to Follow Me on Twitter to stay updated!