I have a list of IP addresses to whitelist in my Nginx configuration. But the requirement is that it should be applied only when the key=123 query parameter specified. I already know that if directive is not applicable for my case due to this. So I tried to use conditional includes like this:
map $arg_key $allowed_ips_config_file {
123 "includes/1.conf";
default "includes/2.conf";
}
location /some/path {
set $backend http://localhost:8080;
proxy_pass $backend;
proxy_set_header Host $http_host;
proxy_buffering off;
proxy_read_timeout 15m;
expires -1;
include $allowed_ips_config_file;
}
includes/1.conf:
allow <my-ip-range>;
deny all;
includes/2.conf:
allow all;
It didn't work either because apparently Nginx does not allow variables in include directive.
I found one option how to solve this problem but it doesn't seem right to me. Is there a right way to do this?