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 / {
    root   domain.com/public;
    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/domain.com/public;
    expires 30d;

  charset utf-8;

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

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

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/mydomain.com/access.log or /var/log/nginx/mydomain.com.access.log

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

  • What version are you running? Variables in log filenames are only allowed since 0.7.4. wiki.nginx.org/HttpLogModule – Frank Farmer Jun 21 '11 at 23:13
  • 3
    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
  • 1
    is this not possible nowadays in latest versions? – snh_nl Apr 11 '19 at 12:22

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

  • 2
    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
  • 3
    can't use $host variable in the access_log neighter – superhero Jun 10 '16 at 9:20

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.