Note that there are some explanatory texts on larger screens.

plurals
  1. POusing vhost templates
    text
    copied!<p>I use nginx on Debian. So besides the main configuration file /etc/nginx/nginx.conf there are folders /etc/nginx/sites-available/ with the vhost config files and /etc/nginx/sites-enabled/ with the links to active vhosts.</p> <p>So let me ask my question first. Because the explanation is long and maybe you don't need to read it...</p> <hr> <p><em>I want to be able to use several vhost templates like this:</em></p> <pre><code>server { listen 80; server_name ~^(?&lt;project&gt;.+)\.(?&lt;area&gt;.+)\.loc$; if ($host ~ ^(?&lt;project&gt;.+)\.(?&lt;area&gt;.+)\.loc$) { set $folder "$area/$project"; } ... access_log /var/log/nginx/$area/$project.access.log; error_log /var/log/nginx/error.log; ... root /var/www/$folder/; ... } </code></pre> <p><em>and to define which vhost is based on which template. Furthermore it would be great to have a possibility to "extend" the template, so that I can add new settings to my vhosts and redefine the settings inherited from the template.</em></p> <p><em>How can I achieve it?</em></p> <hr> <p>My current vhost file structure looks like this:</p> <p>/etc/nginx/sites-available contains following files:</p> <blockquote> <p>default (default vhost) ax-common-vhost (vhost template) test.sandbox.loc (vhost based on the template ax-common-vhost; it includes thatwith the <code>include</code> rule) ...and some further ones...</p> </blockquote> <p>/etc/nginx/sites-available contains following files:</p> <blockquote> <p>default -> /etc/nginx/sites-available/default test.sandbox.loc -> /etc/nginx/sites-available/test.sandbox.loc ...and some further ones...</p> </blockquote> <p>The template ax-common-vhost defines some options like root folder dynamically, using the server name:</p> <pre><code>server { listen 80; server_name ~^(?&lt;project&gt;.+)\.(?&lt;area&gt;.+)\.loc$; if ($host ~ ^(?&lt;project&gt;.+)\.(?&lt;area&gt;.+)\.loc$) { set $folder "$area/$project"; } ... access_log /var/log/nginx/$area/$project.access.log; error_log /var/log/nginx/error.log; ... root /var/www/$folder/; ... } </code></pre> <p>So when I want to create a new vhost, I just create a new vhost file and a link to it -- and don't need to copy&amp;paste the settings and to set the paths manually. I just need to follow the convention, that a host %project%.%area%.loc hast to be placed in [my webroot]/%area%/%project%</p> <p>I thought, it works over the include rule: The server gets a request x.y.loc, looks for a file named so, openes the file, and finally processes the directives in it (so the include directive and the the whole content of the included template).</p> <p>But it's not so. Nginx seems just to scan the whole folder /etc/nginx/sites-available/ (alphabetically?) and to hold on the first file / <code>server</code> directive the host name in the request equals/maches to.</p> <p>That means, the include</p> <pre><code>include /etc/nginx/sites-available/ax-common-vhost; </code></pre> <p>is not used. Actually, I've removed the include directives from the vhost files -- and nothing has changed!</p> <p>And it's a problem. Because when I add a new template, e.g. for my Zend Framework projects (with [project root]/public/ as root):</p> <p>file ax-zf-vhost</p> <pre><code>server { listen 80; server_name ~^(?&lt;project&gt;.+)\.(?&lt;area&gt;.+)\.loc$; if ($host ~ ^(?&lt;project&gt;.+)\.(?&lt;area&gt;.+)\.loc$) { set $folder "$area/$project"; } ... access_log /var/log/nginx/$area/$project.access.log; error_log /var/log/nginx/error.log; ... root /var/www/$folder/public/; ... } </code></pre> <p>..., it is ignored, since the server doesn't get any information about, that the vhost myzf.sandbox.loc is based on ax-zf-vhost. Instead of this it just loops the /etc/nginx/sites-available/ folder, finds ax-common-vhost, myzf.sandbox.loc maches to the pattern ^(?.+).(?.+).loc$, and nginx uses ax-common-vhost for myzf.sandbox.loc.</p> <p>How can this problem be solved?</p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload