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

**URL:** <https://www.ruby-forum.com/t/same-config-for-two-virtual-hosts-except-hostname-certificate/214181>\
**Category:** NGINX\
**Created:** [November 23, 2011, 11:24pm UTC](https://www.ruby-forum.com/t/same-config-for-two-virtual-hosts-except-hostname-certificate/214181 "2011-11-23T23:24:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [November 23, 2011, 11:24pm UTC](https://www.ruby-forum.com/t/same-config-for-two-virtual-hosts-except-hostname-certificate/214181/1 "2011-11-23T23:24:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Gelonida\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/65b543/32.png) [@Gelonida\_N](https://www.ruby-forum.com/u/Gelonida_N)\
**Post date:** [November 23, 2011, 11:51pm UTC](https://www.ruby-forum.com/t/same-config-for-two-virtual-hosts-except-hostname-certificate/214181/2 "2011-11-23T23:51:50Z")

</div>

On 23 Nov 2011 22h23 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

> **[GitHub - perusio/nginx\_ensite: A script to enable or disable a site in nginx.](https://github.com/perusio/nginx_ensite)**
>
> A script to enable or disable a site in nginx.

— appa

---

<div class="post-metadata">

**Author:** ![Gelonida\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/65b543/32.png) [@Gelonida\_N](https://www.ruby-forum.com/u/Gelonida_N)\
**Post date:** [November 24, 2011, 12:22am UTC](https://www.ruby-forum.com/t/same-config-for-two-virtual-hosts-except-hostname-certificate/214181/3 "2011-11-24T00:22:09Z")

</div>

Thanks for your reply,

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

> On 23 Nov 2011 22h23 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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. · GitHub](https://github.com/perusio/nginx_ensite)

That looks nice 🙂 WIll try it.

---

<div class="post-metadata">

**Author:** ![Gelonida\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/65b543/32.png) [@Gelonida\_N](https://www.ruby-forum.com/u/Gelonida_N)\
**Post date:** [November 24, 2011, 12:30am UTC](https://www.ruby-forum.com/t/same-config-for-two-virtual-hosts-except-hostname-certificate/214181/4 "2011-11-24T00:30:47Z")

</div>

On 23 Nov 2011 23h21 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![Gelonida\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/65b543/32.png) [@Gelonida\_N](https://www.ruby-forum.com/u/Gelonida_N)\
**Post date:** [November 24, 2011, 12:36am UTC](https://www.ruby-forum.com/t/same-config-for-two-virtual-hosts-except-hostname-certificate/214181/5 "2011-11-24T00:36:21Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Gelonida\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/65b543/32.png) [@Gelonida\_N](https://www.ruby-forum.com/u/Gelonida_N)\
**Post date:** [November 24, 2011, 12:43am UTC](https://www.ruby-forum.com/t/same-config-for-two-virtual-hosts-except-hostname-certificate/214181/6 "2011-11-24T00:43:48Z")

</div>

On 23 Nov 2011 23h35 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
