Analyze nginx diaries and use iptables to block IP addresses to prevent CC attacks

Source: Internet
Author: User

This method is used to passively analyze the nginx diary to find the IP address with a large number of requests and seal it with iptables. To actively limit the number of ip connections, refer to using nginx to limit the number of IP connections to prevent CC attacks.

#! /Bin/bash
# Created by http://www.onovps.com www.2cto.com
Num = 100 # Upper Limit
Cd/home/wwwlogs
For I in 'Tail access. log-n 1000 | awk '{print $1}' | sort | uniq-c | sort-rn | awk '{if ($1> $ num) {print $2 }}''
# Read the latest 1000 records. If a single IP address exceeds 100 records, it will be blocked.
Do
Iptables-I input-p tcp-s $ I -- dport 80-j DROP
Done

Join crontab to schedule tasks

Crontab-e
*/5 * sh/path/file. sh # Run the command once every 5 minutes.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.