Nginx – multiple servers configuration

clusterconfigurationnginx

I have a cluster of 8 independent web servers, each of them running nginx.
They are all hosting the same websites and are being synced with rsync.

Also, each server has 8 different IP addresses attached to it.

My hosted websites are being directed to different IP's according to some business logic.

Is there some way i can sync the configuration file between all server, but change only the IP's on each one?

For example, have a nginx.conf on each server with:

@IP1 = '1.1.1.1';
@IP2 = '2.2.2.2';

include 'sites.conf';

and then sync the sites.conf and let it be something like:

server {
  listen @IP1:80;
  ... //more configurations here
}

Is it possible to do something like that?

Best Answer

As others have noted, nginx doesn't support that.

On possibility might be to make use of separate config files for each of the individual hosts and have the nginx startup script reference that config file directly.

Method 1: multiple config files

/etc/init.d/nginx would do something like:

MyIP=`# some code to get my ip address or local ID`
nginx -c $PATH_TO_NGINX/${MyIP}.conf

In this manner, you are rsync'ing out the same files out to everyone, but each server will only reference their own config file.

Method 2: dynamically update the config file on service start/restart

MyIP=`# some code to get my ip address or local ID`
updateconfig($MyIP,$TEMPLATECONFIG,$LOCALCONFIG) # Some function which will take a template config distributed out and replace the IP-PLACEHOLDERS with the box's actual IP address
nginx -c $PATH_TO_NGINX/$LOCALCONFIG

In this manner, you only manage one config file template which self-updates on a restart/reload.

If you don't need the additional complexity of Puppet/config management yet, this is a low-overhead way to go, and because the config is dynamic, still allows you to scale out without worrying about the box's ip address. But if your architecture changes, then config management will be very helpful.