Nginx Disable IP access or IP network segment access method

Source: Internet
Author: User

nginx prohibit IP access to prevent the specified IP access to our site, this example can be implemented to prevent single IP access or IP network segment access, very useful let's have a look.

Common Linux Practices

Iptables Reference Rules

The code is as follows Copy Code

Iptables-i input-p Tcp–dport 80-m–mac-soruce$mac-j DROP based on MAC address
Iptables-i input-p tcp–dport 80-s $IP-j DROP based on IP address

Method One,

First set up the following configuration file under the Nginx conf directory, named Blocksip.conf:

Add the following code:

The code is as follows Copy Code

#屏蔽soso蜘蛛IP
Deny 113.108.12.154; #此为搜搜蜘蛛IP
Deny 124.115.0.0/24; #此为屏蔽搜搜蜘蛛124.115.0.1 ~ 124.115.0.255 Entire Network segment IP
Deny 124.115.4.0/24; #此为屏蔽搜搜蜘蛛124.115.4.1 ~ 124.115.4.255 Entire Network segment IP
Shielding the IP of the entire network segment may result in a false kill, but the odds are low.


Save it for a moment.

Added to Nginx configuration file nginx.conf: include blocksip.conf;

Restart the Nginx service:/usr/local/nginx/sbin/nginx-s reload can take effect.

Method two, using Nginx's Ngx_http_access_module


The Ngx_http_access_module module can be used to set which IP or IP segment access is allowed/disabled, and the contents of a file can be set similar to the following:

The code is as follows Copy Code
Deny IP;
Deny subnet;
Allow IP;
Allow subnet;
# Block all IPs
Deny all;
# Allow all IPs
Allow all;

The way the Web segment is written is this: 192.168.1.0/24 such a form.
Then edit nginx.conf and add a line:
Include blockips.conf;
Once this is set up, all sites on that server will follow this setting to deny or allow access. If you want to target a site only, you can include it in the configuration of the specific site:

The code is as follows Copy Code
Location/{
Allow 192.168.0.0/24;
Deny all;
}

This allows only IP access to the 192.168.0.0 network segment, and other IP accesses return a 403 error.
You can also customize a 403 error page, you can create a new error403.html file under/usr/local/nginx/html, in accordance with the HTML syntax to write a document, write some explanatory text.
Then edit nginx.conf, add:

The code is as follows Copy Code
Error_page 403/error403.html;
Location =/error403.html {
root HTML;
}

Nginx Disable IP access or IP network segment access method

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.