Nginx variable expansion in error_log for virtual hosts/servers

If you’re hoping to use an include for logging of virtual hosts (servers) in Nginx :

include global/logging.conf

so you can just do this in your server blocks, don’t keep your hopes up.

    access_log /home/me/sites/$server_name/logs/access.log;
    error_log /home/me/sites/$server_name/logs/error.log;

According to this email thread, variable expansion for the error_log directive is not supported. And according to this thread (and many others, too), variable usage in Nginx is generally bad because they need to be evaluated for each request. Nginx doesn’t just suck the state into global memory, evaluate/expand variables and keep that as the running config, probably because many of the variables of request-dependent. Makes sense but sucks.

But then again, how lazy am I? Yes, variables allow me to write one configuration to represent 30 sites, but those 30 sites creates millions of requests. I think I can take an extra 5 seconds to hardcode the log location into the configuration path. And if I ever change anything, I guess I can spend an extra 5 minutes updating all of those files. If I ever get dozen upon dozens of configurations, I guess I can spend an extra 30 minutes writing a bash script to manually expand these variables.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.