Some urls of nginx can only be accessed by specific network segments

In Nginx, only specific network segments are allowed for certain URLs, and the following configuration can be used:

location /restricted {
    allow 192.168.1.0/24;
    deny all;
}

The above configuration will deny all IP addresses to access the /restricted URL path, except all IP addresses in the IP address range of 192.168.1.0/24.

If you want to allow access from multiple network segments, you can add multiple network segments after the allow command, for example:

location /restricted {
    allow 192.168.1.0/24;
    allow 10.0.0.0/8;
    allow 172.16.0.0/12;
    deny all;
}

The above configuration indicates that only all IP addresses within the above three IP address ranges are allowed to access the /restricted URL path.

It should be noted that the allow and deny commands of Nginx are executed in order. Therefore, when using multiple allow and deny commands, you need to pay attention to their position and sequence. At the same time, it should be noted that when using the subnet mask to restrict access, ensure that the matching relationship between the subnet mask and the IP address is correct, otherwise unexpected problems may occur.

Guess you like

Origin blog.csdn.net/m0_50758217/article/details/130369757