How do I stop spam attack on my website?

Within few hours of launching my website ( on a droplet, my Nginx access log is filled with access requests like this:

** - - [22/Feb/2016:11:22:13 -0500] “GET HTTP/1.0” 404 1564 “-” “-” **

First question is: how the domain “” is mapping to my public IP address? When I ping “”, it gives an IP address that is registered in China. It appears that many others on the internet are getting spammed by these kind of attack.

Second question is: How can I stop these requests at the firewall, before it hits my application server (Rails). It seems harmless as all these requests are returned with a “404 Not found” error code. Still it is filling up my log files and creating unnecessary traffic.

Anyone else is seeing this problem?

Thanks for your help in advance.

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

This is where iptables and a little cron job can be useful.

If you’re regularly being hit by the same IP addresses, to block them out make a rule using iptables. With spam, it’s best to simply drop the traffic so try :

iptables -I INPUT -s xx.xx.xx.xx -j DROP

This will block IP address xx.xx.xx.xx accessing your droplet. You can also use CIDR format to block a range of IP Addresses if necessary.

This is not, however, persistent across a reboot so I’d recommend making a little script and run it at boot time as a cron job.

Assuming you’re logged in as root (if not then su as required)

  1. Create a file called in /root/ :
nano ~/ 
  1. On each line place a rule using iptables. Don’t forget to add the path to iptables!
/sbin/iptables -I INPUT -s xx.xx.xx.xx -j DROP
/sbin/iptables -I INPUT -s yy.yy.yy.yy -j DROP
  1. Save the file and make it executable :
chmod 700 ~/
  1. Test your script by running it :
  1. If all is good, set it as a cron job on reboot :
crontab -e

Then add the following to the crontab that opens :

@reboot    /root/

This will then load and run your script as root each time you restart your server. To add new addresses to block, just add a new line to your script.

This is a basic way of dealing with spam. If you are comfortable with linux it’s definitely worth looking at something like fail2ban which is a much nicer automated way of snipping spam attacks in the bud :)

I hope that helps!

Regards, Mike