Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set a range of remote IP addresses without passing a list?

Tags:

nginx

I have this server block:

server {
    server_name doamin.tld;

    set $maintenance on;
    if ($remote_addr ~ (127.0.0.1|10.1.1.10)) {
        set $maintenance off;
    }
    if ($maintenance = on) {
        return 503;
    }
    error_page 503 @maintenance;

    location @maintenance {
        root /var/www/html/global;
        rewrite ^(.*)$ /holding-page.html break;
    }

    root         html;
    access_log logs/doamin.tld.access.log;
    error_log logs/doamin.tld.error.log;

    include ../conf/default.d/location.conf;

}

What is the correct way to pass a list to the $remote_addr instead of coding it like (127.0.0.1| etc...)?

like image 249
khinester Avatar asked Sep 29 '17 15:09

khinester


People also ask

How do I designate a range of IP addresses?

Click IP Address Manager > IP Addresses > Manage Subnets & IP Addresses. In the network tree pane on the left, click the subnet to which you want to add your new IP address range. Click Add IP Range. Enter the starting IP address and the ending IP address of your IP address range.

How can I tell if two IP addresses are on the same network?

The most common subnet you will see is 255.255. 255.0. So if two addresses match in the first three sections (reading left to right), and the subnet is 255.255. 255.0 for both addresses, they are in the same subnet.

How do you calculate IP range from CIDR?

The formula to calculate the number of assignable IP address to CIDR networks is similar to classful networking. Subtract the number of network bits from 32. Raise 2 to that power and subtract 2 for the network and broadcast addresses. For example, a /24 network has 232-24 - 2 addresses available for host assignment.


1 Answers

Use the nginx map directive to set the $maintenance value according to the $remote_addr:

map $remote_addr $maintenance {
    default       on;

    127.0.0.1     off;
    10.1.1.10     off;
    10.*.1.*     off;
}

server {
    server_name doamin.tld;

    if ($maintenance = on) {
        return 503;
    }
    # ... your code ...
}

Take a look at the include directive if you want to take the IPs list in a separate file.

like image 108
lifeisfoo Avatar answered Oct 15 '22 21:10

lifeisfoo