Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Is the order in which Nginx includes configuration files fixed, or random? Apache explicitly states wildcard characters are expanded in alphabetical order. With Nginx it seems this does not apply, and the manual says nothing about it.

In my setup, 20_example.com was included before 00_default, which defeats my purpose of defining shared directives (like log formats) there.

share|improve this question

4 Answers 4

up vote 9 down vote accepted

According to nginx source code it uses glob() function with GLOB_NOSORT parameter, so, the order of file inclusion could not be clearly established.

This was changed in November 2012, first released in 1.3.10. From the changefile:

now if the "include" directive with mask is used on Unix systems, included files are sorted in alphabetical order.

share|improve this answer

AD7six's answer is not wholly correct. Order also matters when two server blocks have the same "specificity", for example when both use regexes in server_name, and the incoming request matches both:

server {
    server_name ~^(www\.)?(?<domain>foo\.com|bar\.com|baz\.com)$;
    ...
}

and

server {
    server_name ~^(www\.)?(?<domain>.+)$;
    ...
}

In this scenario, the two server blocks cannot be spread over two files because their order could not then be guaranteed.

share|improve this answer

As OP points out log_format order it's indeed very important, in the case of custom log_formats, if you place in the same dir both types files, some files with log_formats, some files with servers blocks that make use of those log_formats, result will be a highly unrepeatable configuration.

At work when we first moved our hand made nginx farm into puppet, we started to see some nginx installations from scratch to fail in an identical hardware/setup, after a lot of debugging bizarre situations like having two /etc/nginx/* (-r) with an md5match and same binary producing different results (invalid log_format error on one and working on other), we learned the hard way that order it's indeed very important for some directives like log_format.

We fixed the issue by just moving server blocks generation out of conf.d/* into another folder included after conf.d/*.

share|improve this answer

Order is not important to nginx. Rules are processed in terms of specificity - the most specific rule that matches is applied.

Also, nginx doesn't permit rule inheritance, so anything you define in a default server config isn't going to apply to a subsequently (re)defined server config

You can however do this:

server {
    listen 80;
    server_name _;
    ...
    root /var/www/default/public;
    include common.conf; # <--
}

server {
    listen 80;
    server_name foo.com;
    ...
    root /var/www/foo.com/public;
    include common.conf; # <--
}

Note that in-keeping with the first sentence of this answer - it is irrelevant in what order the server directives are defined/loaded.

share|improve this answer
2  
Indeed, it does not matter for server blocks, but it does matter for other directives, like the log format: I would like to define it in 00_default, and use it in 10_site, 20_site. But that does not work if 20_site is included first: if fails with an unknown log format. –  Jan Fabry Feb 17 '12 at 14:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.