Nginx: can I use $ server_name when specifying the location of the access log?

I want to write a configuration file for the nginx virtual host that looks like this:

server { listen 80; server_name www.my-domain-name.com; access_log /home/me/sites/$server_name/logs/access.log; error_log /home/me/sites/$server_name/logs/error.log; location /static { alias /home/me/sites/$server_name/static; } location / { proxy_pass http://localhost:8000; } } 

Using $server_name seems to work for location /static , but it doesn't seem to work for access_log and error_log - am I doing something wrong? Or is it simply impossible? Can I do it differently?

[update] - this is an error message when restarting nginx:

nginx: [emerg] open() "/home/me/sites/$server_name/logs/error.log" failed (2: No such file or directory)

+7
source share
1 answer

I also wanted to do this, but apparently by design nginx cannot expand the variables in the error_log command if there are errors, and it cannot get the log file name to write them.

Their suggestion is to use some program to generate your configuration files. You can use sed to do this, to automatically search for and replace your own variables and place the output in the nginx configuration directory.

+6
source

All Articles