4

I try to configure vhosts with and without additional per-vhost config with nginx. I think of something like this:

server {
    listen 81; 
    server_name ~^(www\.)?(?<sname>.+?)$;
    root /var/www/$sname;
    include /etc/nginx/sites/$sname;
    access_log /var/log/nginx/$sname/access.log;
    error_log /var/log/nginx/$sname/error.log;
}

Then i could just touch /etc/nginx/mysite.example.com to add a new site with static html, while i can edit the file for the vhost to add for example a reverse proxy directive or some rewrite rules.

The problem is, nginx seems to include the config when starting. And it would be more clean to have something like

for $config in /etc/nginx/sites:
    {
        server_name $config
        root /var/www/$config
        include $config
        [...]
    }

which should run at the start and not on the first request to the vhost.

2
  • Why do you need this?
    – 030
    Jul 5, 2015 at 16:14
  • For easy non-redundant vhost configs. Currently i have a lighttpd with script includes, which does a vhost for sites/domain.example, including this file. Look at the example above, i want to have everything around the include as template generated for each vhost. The regex for log, wwwroot and so on works with nginx, but the "include additional config" function does not work this way.
    – allo
    Jul 5, 2015 at 20:00

1 Answer 1

1

I still do not know, if a script include is possible, but i am using a script to generate the static config now using jinja2 as template system.

An alternative to using an own script is to use the ansible template module. When using ansible it can also be much faster to create one large configuration instead of many snippets, which won't hurt as the templates used by ansible can still be small snippets that are concatenated when creating the autogenerated config.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .