Automatically Blocking IPs with Nginx Using AWK, Shell Scripts, and Crontab

This guide explains how to create an Nginx block file, use AWK to identify IPs that exceed 60 requests per minute, write a shell script to generate deny rules, and schedule the script with crontab to automatically block abusive IPs and return a 403 response.

Top Architect
Top Architect
Top Architect
Automatically Blocking IPs with Nginx Using AWK, Shell Scripts, and Crontab

First, create a blockip.conf file in the Nginx conf directory and list the IPs to be denied, each line like deny 1.2.3.4;.

Then add include blockip.conf; to the HTTP block of the main Nginx configuration and reload Nginx.

To automate detection of abusive IPs, use AWK to parse access.log, count requests per minute, and output IPs with more than 60 requests.

Example AWK command:

awk '{print $1}' access.log | sort | uniq -cd | awk '{if($1>60)print $0}'

.

Write a shell script that clears the previous block file, runs the AWK pipeline, writes the resulting IPs in deny format to blockip.conf, and reloads Nginx; also clears the log after processing.

Schedule the script with a crontab entry such as * * * * * cd /usr/local/nginx/logs/ && sh ip_test.sh and restart the cron service.

After these steps, any IP that exceeds 60 requests per minute will be automatically blocked, returning a 403 response.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendAutomationshell scriptcrontabIP blocking
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.