Include file with "if" statements

I have several different “templates” for location and most of templates
have
common part like this:


/etc/nginx/nginx.conf:
http {
include /etc/nginx/conf.d/*.conf;
}

/etc/nginx/conf.d/config12345.conf:
server {
listen 12345;
location / {
some configuration;
}
location /special {
include template12345.txt;
}

/etc/nginx/template12345.txt:
some configuration;
if ($variable1 = “value1”) { return 403; }
if ($variable2 = “value2”) { return 403; }
if ($variable3 = “value3”) { return 403; }
some other configuration;

When I try to separate this “if” part into separate file like this:

/etc/nginx/conditions.txt
if ( $variable1 = “value1” ) { return 403; }
if ( $variable2 = “value2” ) { return 403; }
if ( $variable3 = “value3” ) { return 403; }

and include it in the template like “include conditions.txt” instead of
repeating this part in every template, I get error message:
“nginx: [emerg] “if” directive is not allowed here in
/etc/nginx/conditions.txt:1”

So my question is: is there a limit to “include” directive depth ? Why
am I
getting this error ?

Posted at Nginx Forum:

Thank you for the direction where to look.

You were right. It was too wide of a wildcard in http{} section outside
of
server/location. Fixed now.

Posted at Nginx Forum:

Hello!

On Wed, May 18, 2016 at 11:35:48AM -0400, w_boba wrote:

server {
if ($variable1 = “value1”) { return 403; }
if ( $variable3 = “value3” ) { return 403; }

and include it in the template like “include conditions.txt” instead of
repeating this part in every template, I get error message:
“nginx: [emerg] “if” directive is not allowed here in
/etc/nginx/conditions.txt:1”

So my question is: is there a limit to “include” directive depth ?

No.

Why am I getting this error ?

Likely you’ve uninintentionally included conditions.txt into a
place where “if” directives cannot be used. In particular, this
can easily happen if you have various wildcard includes in your
configuration.

If unsure, please provide full minimal configuration which
triggers the error.


Maxim D.
http://nginx.org/