# Shared nginx configuration for multiple servers

**URL:** <https://www.ruby-forum.com/t/shared-nginx-configuration-for-multiple-servers/205411>\
**Category:** NGINX\
**Created:** [April 6, 2011, 1:01am UTC](https://www.ruby-forum.com/t/shared-nginx-configuration-for-multiple-servers/205411 "2011-04-06T01:01:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [April 6, 2011, 1:01am UTC](https://www.ruby-forum.com/t/shared-nginx-configuration-for-multiple-servers/205411/1 "2011-04-06T01:01:13Z")

</div>

We have bunch of servers, where we want to share configuration via SVN  
or GIT (or whatever). So on all server there will be the same  
configuration files set - this is easy to maintain.

Although the most of the configuration is the same on all servers, there  
is a small set of differences for certain servers.

I would like to separate this per-server configuration differences into  
files called $hostname.conf in the conf.d directory.

However this is not working for me:

```
 include /etc/nginx/conf.d/$hostname.conf;

```

Is this supposed to work, or not? If not, what is prefered way to  
achieve my goal?

Thanks

Ondrej

–  
Ondrej JOMBIK  
Platon Technologies Ltd., Hlavna 3, Sala SK-92701  
+421 903 PLATON - removed\_email\_address@domain.invalid - [http://platon.org](http://platon.org)

---

<div class="post-metadata">

**Author:** ![Ondrej\_Jombik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/a698b9/32.png) [@Ondrej\_Jombik](https://www.ruby-forum.com/u/Ondrej_Jombik)\
**Post date:** [April 6, 2011, 5:57pm UTC](https://www.ruby-forum.com/t/shared-nginx-configuration-for-multiple-servers/205411/2 "2011-04-06T17:57:58Z")

</div>

Hello,

I’m no expert but from what I can see in the code (ngx\_conf\_file.c),  
the argument to include directive is not “evaluated”. So, I would say  
the behavior you are seeing is “as expected” although probably not the  
one you would like…  
May be you can confirm by enabling debug on your install. In the logs  
you should see a line like "include ".

Good luck.

Antoine.

---

<div class="post-metadata">

**Author:** ![Ondrej\_Jombik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/a698b9/32.png) [@Ondrej\_Jombik](https://www.ruby-forum.com/u/Ondrej_Jombik)\
**Post date:** [April 6, 2011, 6:51pm UTC](https://www.ruby-forum.com/t/shared-nginx-configuration-for-multiple-servers/205411/3 "2011-04-06T18:51:43Z")

</div>

Yes, I can confirm this, it is not evaluated.

So probably I need to do:

```
 if ($hostname eq 'servername1') {
     include /etc/nginx/conf.d/servername1.conf;
 }

```

But I read on MANY places that using if() is evil.

Now I do not know what to do.  
Can some expert suggest a prefered way?

Thanks

Ondrej

---

<div class="post-metadata">

**Author:** ![Ondrej\_Jombik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/a698b9/32.png) [@Ondrej\_Jombik](https://www.ruby-forum.com/u/Ondrej_Jombik)\
**Post date:** [April 6, 2011, 6:54pm UTC](https://www.ruby-forum.com/t/shared-nginx-configuration-for-multiple-servers/205411/4 "2011-04-06T18:54:48Z")

</div>

include isn’t supported inside of “if” either, I believe.

I wanted to do conditional includes before - but ran into a wall.  
Someone I believe replied to me off list and said they were trying to  
add support for that though.

(I think it might be how the configuration parser works. $hostname is  
unknown when the server starts up, and it includes all the includes at  
the time of start?) I don’t know.

---

<div class="post-metadata">

**Author:** ![Ondrej\_Jombik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/a698b9/32.png) [@Ondrej\_Jombik](https://www.ruby-forum.com/u/Ondrej_Jombik)\
**Post date:** [April 6, 2011, 7:10pm UTC](https://www.ruby-forum.com/t/shared-nginx-configuration-for-multiple-servers/205411/5 "2011-04-06T19:10:51Z")

</div>

I see no technical problems with this. Variable $hostname is known when  
the server starts, because with $hostname I mean the name of server  
where nginx is running (see gethostname(2) call), not the name of  
virtual host (which is unknown on the server start, indeed)

If include is not supported inside if(), I’m pretty sure we are lost.  
I see no other way how to accomplish this. Maybe Igor, who knows every  
byte of code knows:-)

I think this requirement is a desirable feature. I know guys who  
maintain hundreds or thousands of nginx servers. Currently they are  
copying configuration files back and forth, but if they were able to  
make some per-server configuration alternations based on the same  
configuration files set, they would be able to put whole nginx  
configuration into SVN and do just “svn update” on certain webservers.

Then every webserver will load its specific configuration (in fact main  
configuration with some small per-server differences).

Anyway, thank you for your reply.

Ondrej

---

<div class="post-metadata">

**Author:** ![Ondrej\_Jombik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/a698b9/32.png) [@Ondrej\_Jombik](https://www.ruby-forum.com/u/Ondrej_Jombik)\
**Post date:** [May 2, 2011, 9:05pm UTC](https://www.ruby-forum.com/t/shared-nginx-configuration-for-multiple-servers/205411/6 "2011-05-02T21:05:19Z")

</div>

I’m facing a related issue, although my attempt at implementation is  
different from the OP.

ip.conf:  
set $server\_ip 10.11.1.50;

nginx.conf:  
…  
server {  
include ip.conf; #adds the $server\_ip variable which is  
machine-specific  
listen $server\_ip:80 default\_server;  
…  
}

This fails with: nginx: [emerg] host not found in “$server\_ip:80” of  
the “listen” directive in /opt/nginx/conf/nginx.conf:48

What is the best way to include a hard-coded IP address from another  
file in order to allow nginx.conf to be universal across deployed  
machines?

Cheers,

Dean

Posted at Nginx Forum:

> **[Re: Shared nginx configuration for multiple servers](https://forum.nginx.org/read.php?2,188621,195032#msg-195032)**
>
> We have bunch of servers, where we want to share configuration via SVN or GIT (or whatever). So on all server there will be the same configuration files set - this is easy to maintain. Although the most of the configuration is the same on all...

---

<div class="post-metadata">

**Author:** ![Ondrej\_Jombik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/a698b9/32.png) [@Ondrej\_Jombik](https://www.ruby-forum.com/u/Ondrej_Jombik)\
**Post date:** [April 6, 2011, 8:48pm UTC](https://www.ruby-forum.com/t/shared-nginx-configuration-for-multiple-servers/205411/7 "2011-04-06T20:48:26Z")

</div>

> From what I can see, include can go anywhere (which includes the evil  
> if). The problem is with the directives that are includED. They all  
> have to be supported inside if, which is unlikely.

Now, regarding your main problem, what about using symbolic links to  
provide the desired functionality.

Something like :  
include /etc/conf/local.nginx.conf

with /etc/conf/local.nginx.conf being a symbolic link to the file that  
is specific for this server.

Antoine.

–  
Join me on my nginx cruise : [http://www.nginx-discovery.com](http://www.nginx-discovery.com)

---

<div class="post-metadata">

**Author:** ![Ondrej\_Jombik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/a698b9/32.png) [@Ondrej\_Jombik](https://www.ruby-forum.com/u/Ondrej_Jombik)\
**Post date:** [May 2, 2011, 9:26pm UTC](https://www.ruby-forum.com/t/shared-nginx-configuration-for-multiple-servers/205411/8 "2011-05-02T21:26:56Z")

</div>

Hello!

On Mon, May 02, 2011 at 03:04:48PM -0400, dbanks wrote:

> machine-specific  
> listen $server\_ip:80 default\_server;  
> …  
> }
> 
> This fails with: nginx: [emerg] host not found in “$server\_ip:80” of  
> the “listen” directive in /opt/nginx/conf/nginx.conf:48
> 
> What is the best way to include a hard-coded IP address from another  
> file in order to allow nginx.conf to be universal across deployed  
> machines?

Variable are evaluated at runtime during request processing, you  
can’t use them to configure listening sockets.

Either place full listen directive in included file or use some  
external scripting to produce actual config.

Maxim D.
