In my vhost I have:

root   /var/www/$server_name/public_html;
index  index.php;

access_log /var/log/nginx/$server_name.access.log;
error_log  /var/log/nginx/$server_name.error.log debug;

For the root and the access log the $server_name work. It does not work for the error log.

enter image description here

I see it's set by root instead of pi (I use pi instead of data-www) How do I fix this?



As far as I know variables are still not allowed in error_log even though you can use them in access_log (as your screenshot reveals properly working in your scenario).

Besides that, you should almost always use $host as it is the only guaranteed variable to say something sensible regardless of the user-agent's behavior.

You can check the difference between $host and $server_name on nginx documentation:

  • $host: in this order of precedence: host name from the request line, or host name from the “Host” request header field, or the server name matching a request.

  • $server_name: name of the server which accepted a request. It contains the server_name of the virtual host which processed the request, as it was defined in the nginx configuration. If a server contains multiple server_names, only the first one will be present in this variable.

As you have to account for all the possible things that a user-agent may do then forming an HTTP request (ancient HTTP/1.0 requests and modern badly-written software can happen all the time), you should consider using only the $host variable for your intended purpose.

Worth mentioning that access_log is handled by ngx_http_log_module, while error_log is handled a little bit differently as a ngx_core_module.

  • Thanks for the difference of $host. For my setup I'd like $server_name As each vhost is ` domain +www. and this way I'll never have separate logs for www.
    – janw
    Oct 31 '17 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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