Allow directive with variables

I am trying to use a variable with the allow directive, i.e.

set $home_ip 1.2.3.4;

location ^~ /apc/ {
  # Allow home
  allow $home_ip;

  deny all;

  include /etc/nginx/php.conf;
}

But I am getting an error. Does the allow directive allow variables?

Posted at Nginx Forum:

Hello!

On Mon, Dec 31, 2012 at 07:51:21PM -0500, justin wrote:

  include /etc/nginx/php.conf;
}

But I am getting an error. Does the allow directive allow variables?

No, variables are not allowed within the “allow” directive
parameters. If a parameter can contain variables, it’s indicated
in a directive description.

Note well: it’s bad idea to use variables to shorten configs, see
Is there a proper way to use nginx variables to make sections of the configuration shorter, using them as macros for making parts of configuration work as templates?.


Maxim D.

An easy way to solve this issue is to create a “allowmyip.conf” file and
include it anywhere you wish.

allowmyip.conf file :

allow 11.22.33.44
deny all;

Then in your server block :location ^~ /apc/ {
# Allow home

include /etc/nginx/allowmyip.conf;

}

This way it will be real easy to manage if your IP address changes. Just
update your IP Address in the allowmyip.conf file and restart the
server.

Posted at Nginx Forum: