How to use Zenfirewall with nginx

The steps to add ZenFirewall IPs to your Nginx configuration are as follows:

  1. Download the blocklist file and generate a configuration file suitable for Nginx. This command can be added to a cron job and executed once or twice per day:

    curl -sf https://zenfirewall.com/zenblock.txt | sed 's/^/deny /; s/$/;/' > /etc/nginx/blocked_ips.conf

  2. Add the following configuration to /etc/nginx/nginx.conf inside the http block:

    geo $blocked_ip {
        default 0;
        include /etc/nginx/blocked_ips.conf;
    }

  3. In each virtual host where you want to apply this list:

    server {
        listen 80;
        server_name test.com;
    
        if ($blocked_ip) {
            return 403;
        }
    
        location / {
            # xxxxx
        }
    }