# Include file with "if" statements

**URL:** <https://www.ruby-forum.com/t/include-file-with-if-statements/244643>\
**Category:** NGINX\
**Created:** [May 18, 2016, 5:36pm UTC](https://www.ruby-forum.com/t/include-file-with-if-statements/244643 "2016-05-18T17:36:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [May 18, 2016, 5:36pm UTC](https://www.ruby-forum.com/t/include-file-with-if-statements/244643/1 "2016-05-18T17:36:18Z")

</div>

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:

> **[include file with "if" statements](https://forum.nginx.org/read.php?2,266986,266986#msg-266986)**
>
> 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

---

<div class="post-metadata">

**Author:** ![w\_boba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/aeb1de/32.png) [@w\_boba](https://www.ruby-forum.com/u/w_boba)\
**Post date:** [May 18, 2016, 10:45pm UTC](https://www.ruby-forum.com/t/include-file-with-if-statements/244643/2 "2016-05-18T22:45:19Z")

</div>

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:

> **[Re: include file with "if" statements](https://forum.nginx.org/read.php?2,266986,266991#msg-266991)**
>
> 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

---

<div class="post-metadata">

**Author:** ![w\_boba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/aeb1de/32.png) [@w\_boba](https://www.ruby-forum.com/u/w_boba)\
**Post date:** [May 18, 2016, 8:29pm UTC](https://www.ruby-forum.com/t/include-file-with-if-statements/244643/3 "2016-05-18T20:29:12Z")

</div>

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.

> **[nginx](http://nginx.org/)**
