Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the following default server setup in nginx:

# Default HTTP Server
server {
  listen 80 default;
  server_name _;
  access_log /var/log/nginx/$server_name.access.log;
  error_log /var/log/nginx/$server_name.error.log;

  server_name_in_redirect off;

  location / {
    index  index.php;
    try_files $uri index.php;

  location ~     \.(html|jpg|jpeg|gif|png|ico|css2|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ {
    root /path/to/;
    expires 30d;

  charset utf-8;

  location ~ \.php$ {
    include /opt/nginx/conf/fastcgi_params;
    fastcgi_index index.php;
    fastcgi_param  SCRIPT_FILENAME  /path/to/;   

  location ~ \.(js|ico|gif|jpg|png|css)$ {
    root /path/to/;

I have several domains pointing to the server. What I'm trying to accomplish here is to have logs in the format of either /var/log/nginx/ or /var/log/nginx/

Instead, I am getting /var/log/nginx/$server_name.access.log.

If I try the directory method I get an error when checking the config nginx: [emerg] open() "/var/log/nginx/$server_name/access.log" failed (2: No such file or directory)

Why isn't nginx passing the variable to the file name?

Using nginx/1.0.0

share|improve this question

migrated from Jun 22 '11 at 9:49

This question came from our site for professional and enthusiast programmers.

What version are you running? Variables in log filenames are only allowed since 0.7.4. – Frank Farmer Jun 21 '11 at 23:13
and even then, only in access_log. Variables are still not allowed in error_log. $server_name is also probably not the variable you're really looking for, as it will always expand to '_' in your case. You probably were really looking for $host. – kolbyjack Jun 22 '11 at 13:15
I'm using 1.0.0, and thanks for the pointer on $host - that's what I was trying to accomplish. Also, didn't know Server Fault existed - mind blown, thanks for the migration. – Mahdi.Montgomery Jun 22 '11 at 19:59
up vote 6 down vote accepted

You need to use the $host variable – only allowed for access_log directives.

share|improve this answer
I'll assume there is no work-around for passing a variable for the error_log.. Which is kind of too bad. – Mahdi.Montgomery Jun 22 '11 at 20:51
can't use $host variable in the access_log neighter – Erik Landvall Jun 10 at 9:20

Your Answer


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.