Same config for two virtual hosts (except hostname / certificate)

Hi,

I’m still a little shaky with how to best structure my nginx
configurations and where one can / should use includes and where one
couldn’t.

somehow I like the idea of a directory structure like

/etc/nginx/ # for basic files / certificates / . . .
/etc/nginx/sites-available # the available configs
/etc/nginx/sites-enabled # the sites, that shall really be started

Now I have two sites (ssl which should share exactly the same config
(except the host name and the server certificate)
the first one would be for example server_1.mysite.net
the other server_2.mysite.net:4433

Of course both would need their own certificate and host name.

What would be the recommended way for such a setup?

thanks in advance for any feedback

On 23 Nov 2011 22h23 WET, [email protected] wrote:

/etc/nginx/sites-enabled # the sites, that shall really be started
What would be the recommended way for such a setup?

My suggestion is for you to place the common things in both vhosts in
a file, e.g., my_common_config.conf and do an include in each vhost:

server_1.mysite.net.conf
server_2.mysite.net.conf

You can try this script for enabling each site and disabling using the
sites_enabled/sites-available layout.

— appa

Thanks for your reply,

On 11/23/2011 11:51 PM, Antnio P. P. Almeida wrote:

On 23 Nov 2011 22h23 WET, [email protected] wrote:

somehow I like the idea of a directory structure like
. . .
/etc/nginx/ # for basic files / certificates / . . .
/etc/nginx/sites-available # the available configs
/etc/nginx/sites-enabled # the sites, that shall really be started

Now I have two sites (ssl which should share exactly the same config
(except the host name and the server certificate)
the first one would be for example server_1.mysite.net
the other server_2.mysite.net:4433

. . .

What would be the recommended way for such a setup?

My suggestion is for you to place the common things in both vhosts in
a file, e.g., my_common_config.conf and do an include in each vhost:

server_1.mysite.net.conf
server_2.mysite.net.conf

Where would you place my_common_config.conf?
would you clutter /etc/nginx with it or create a subdir like
/etf/nginx/sites-baseconfig
would you then just do an include with a relative path like
include …/my_common_config.conf ?

You can try this script for enabling each site and disabling using the
sites_enabled/sites-available layout.

GitHub - perusio/nginx_ensite: A script to enable or disable a site in nginx.

That looks nice :slight_smile: WIll try it.

On 23 Nov 2011 23h21 WET, [email protected] wrote:

My suggestion is for you to place the common things in both vhosts
include …/my_common_config.conf ?
I prefer to have it under the sites-available directory. Because is
something specific to one or more of the enabled sites.

There’s in Debian a /etc/nginx/conf.d dir but I never use it. I put
all generic stuff in /etc/nginx and site specific stuff under
/etc/nginx/sites-available.

I always name the vhost config files with names that identify it
explicitly, .e.g, www.example.com.conf. This way there’s no risk in
confusing vhost config with other files that have directives that can
be used in several vhosts.

— appa

On 11/24/2011 12:30 AM, António P. P. Almeida wrote:

would you clutter /etc/nginx with it or create a subdir like

I always name the vhost config files with names that identify it
explicitly, .e.g, www.example.com.conf. This way there’s no risk in
confusing vhost config with other files that have directives that can
be used in several vhosts.

Yes combined with your naming style this should be clear enough.
I assume you will just symlink the vhosts file to sites available.
and the include will thus be something like
include …/sites-available/common_settings.conf, right?

On 23 Nov 2011 23h35 WET, [email protected] wrote:

Where would you place my_common_config.conf?
/etc/nginx/sites-available.

I always name the vhost config files with names that identify it
explicitly, .e.g, www.example.com.conf. This way there’s no risk in
confusing vhost config with other files that have directives that
can be used in several vhosts.

Yes combined with your naming style this should be clear enough.
I assume you will just symlink the vhosts file to sites available.
and the include will thus be something like
include …/sites-available/common_settings.conf, right?

Nope. I do nginx_ensite www.example.com.conf

This creates a symlink:

ls -la /etc/sites-enabled

www.example.com.conf -->
/etc/nginx/sites-available/www.example.com.conf

Of course you must have a line like:

include /etc/nginx/sites-enabled/*;

in your nginx.conf

The common_settings.conf is included in the www.example.com.conf file:

include sites-available/common_settings.conf;

It only makes sense to symlink vhost configs.

— appa