Deny connections from bots/attackers using Varnish (TM)

Before running the commands shown on this page, you should load the Bitnami stack environment by executing the installdir/use_APPNAME script (Linux and MacOS) or by clicking the shortcut in the Start Menu under “Start -> Bitnami APPNAME Stack -> Application console” (Windows). On OS X VMs, the installation directory is /opt/bitnami and OS X VM users can click the “Open Terminal” button to run commands. Learn more about the Bitnami stack environment and about OS X VMs.

Sometimes, if you are experiencing poor performance, it is because you are being attacked by Internet bots. The reason for these attacks is that they are trying to find a security bug in your application code or in the software itself.

An example of a bot attack is attempting to check if the php.cgi binary is disabled. As this is disabled by default, attackers won’t be able to exploit your system, but you will have hundreds or even thousands of connections from the same IP address (or even different IP addresses) trying to “check” every few hours if those binaries or scripts are available.

Our stacks and cloud images come with the latest versions of their components but, even though you are safe from those attacks, your server could experience poor performance because of the traffic they generate.

To know if you are being attacked, run the command below:

$ cd installdir/apache2/logs/
$ tail -n 10000 access_log | awk '{print $1}'| sort| uniq -c| sort -nr| head -n 10

This will show you the number of times that an IP address connected to your Web server. If you see that some IP addresses have many more connections than others, run the following command (remember to modify ATTACKER_IP with the correct IP):

$ cd installdir/apache2/logs/
$ grep "ATTACKER_IP" access_log

If you see that the IP address is always attempting to connect to the same location, if it is a URL that you don’t know, or if it is trying to run binaries or scripts directly, it is likely that IP address is a bot.

To deny connections to these attackers, the easiest way is with your Varnish ™ configuration file. Follow these steps:

  • Edit the file at installdir/varnish/etc/varnish/default.vcl and add the IP addresses to the access control list (ACL). The example below shows how to reject the IP address:

    acl forbidden {

    You can block multiple IP addresses by adding each on a separate line following the format shown above.

  • Add the following to the vcl_recv routine:

    sub vcl_recv {
      if (client.ip ~ forbidden) {
        return(synth(403, "Forbidden"));
  • Restart Varnish ™:

    $ sudo installdir/ restart varnish
  • Restart the Apache web server:

    $ sudo installdir/ restart apache

Varnish is a registered trademark of Varnish Software AB and its affiliates.

Last modification June 9, 2020