E
E
Ergil Osin2016-02-09 17:45:52
Nginx
Ergil Osin, 2016-02-09 17:45:52

Using variables in the nginx config. Why doesn't $server_name work in error_log and some other directives? What other options?

Actually the whole question is in the title.
We have nginx, it has 100% typical pieces of configs for different hosts on the server, like

location /.well-known/acme-challenge {
             alias /srv/www/.well-known/acme-challenge;
}
    
location / {
        return 301 https://$server_name$request_uri;
}
        access_log /var/log/nginx/$server_name.access.log;

But it error_log /var/log/nginx/$server_name.error.log;
won’t work for us, but it will create the /var/log/nginx/$server_name.error.log file for us, instead of the file with the server name.
The same problem with ssl_certificate, ssl_certificate_key and ssl_trusted_certificate, you cannot specify $server_name in the file path, so as not to write the path to the file with the certificate in each config, but to put this piece into a separate include.
What is the reason? Is it just that the developers didn’t bother with the processing of variables in these config parameters, or are there reasons why they didn’t care? Who can use what workarounds for these places of configs.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lynn "Coffee Man", 2016-02-09
@Lynn

Everything is simple. The error_log directive does not support variables in file paths.
https://forum.nginx.org/read.php?2,178665,178680#m...

There is no variables support in error_log directive (and it's
unlikely to appear), so "...error_log" file is expected.

At the time when you need to write to the error_log, this should be done quickly and at minimal cost. Variable interpolation is an extra step. In addition, if the file name is known in advance, then it is opened for writing by the master process at the time of (re-)starting the application, and if it had to be calculated based on the values ​​of some variables, then it would have to be opened already in the worker and right before writing, which is never fast and, besides, the worker usually has fewer rights and a good chance that he simply cannot open the file.
If you really have a need for a large number of different servers, then no one bothers you to take any of the existing template engines (or write your own bicycle) and generate at least thousands of the same type of config files.

S
sim3x, 2016-02-09
@sim3x

Where to look for beautiful nginx configs or how to make mine beautiful?
VBart has the opinion of the team changed on this issue?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question