khinester
khinester

Reputation: 3520

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

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...)?

Upvotes: 5

Views: 14336

Answers (1)

lifeisfoo
lifeisfoo

Reputation: 16294

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.

Upvotes: 16

Related Questions