Analyzing Nginx logs and shielding the collector IP (Nginx shielding IP configuration instance) _nginx

Source: Internet
Author: User

Collection and prevention of collection is a prolonged topic, on the one hand want to engage in other people's things, on the other hand do not want their own things to get away.

This article describes how to use Nginx shielding IP to achieve the prevention of collection, of course, can also be achieved through iptable.

1. Find the IP to be screened

Copy Code code as follows:

awk ' {print $} ' Nginx.access.log |sort |uniq-c|sort-n

Nginx.access.log is a log file,

will be to the following results, the previous IP access number, followed by IP, it is obvious that we need to visit the number of IP and not the spider's IP shielding, in this case we shielded off 165.91.122.67

Copy Code code as follows:

...
13610 202.112.113.192
95772 180.169.22.135
337418 219.220.141.2
558378 165.91.122.67

2. Under the Nginx installation directory, create a new screened IP file, named Blockip.conf, after the new increase shielding IP only need to edit this file can be. Add the following content
Copy Code code as follows:

Deny 165.91.122.67;

Save it for a second.

3. Add the following configuration to the Nginx configuration file nginx.conf , which can be placed in HTTP, server, location, limit_except statement block, and need to pay attention to relative path, In this case, nginx.conf,blocksip.conf is in the same directory.

Copy Code code as follows:

Include blockip.conf;

4. Restart the Nginx service:/usr/local/nginx/nginx-s reload can be effective.

Advanced usage:

A configuration file that masks IP can either mask a single IP or block IP segments, or only allow an IP or an IP segment to be accessed.

Copy Code code as follows:

# Masks Single IP access

Deny IP;
# Allow single IP access

Allow IP;
# Masks All IP access

Deny all;
# Allow all IP access

Allow all;
#屏蔽整个段即从123.0.0.1 to 123.255.255.254 Access command

Deny 123.0.0.0/8
#屏蔽IP段即从123.45.0.1 to 123.45.255.254 Access command

Deny 124.45.0.0/16
#屏蔽IP段即从123.45.6.1 to 123.45.6.254 Access command

Deny 123.45.6.0/24


If you want to implement such an application, except for a few IP, all the other Rejects,
That would require you to write that in blockip.conf.
Copy Code code as follows:

Allow 1.1.1.1;
Allow 1.1.1.2;
Deny all;

Individual website shielding IP method, include blocksip.conf; Put to the URL corresponding in the server{} statement block,
All Web site shielding IP methods, include blocksip.conf; to the HTTP {} statement block.

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.