Variables in access_log

M
  • 3 Jul '09
>From http://wiki.nginx.org/NginxHttpLogModule

The log file path can contain variables (version >=0.7.4) ..

 nginx -v
nginx version: nginx/0.7.59

Config snippet:

  set $client $host;
  if ($client ~ "^(*).admin.mysite.com"){
    set $client $1;
  }
  root /var/www/wordpress/$client/admin;

  location / {
  ...
    access_log /var/log/nginx/wordpress/admin/$client/generic-access.log;
    error_log /var/log/nginx/wordpress/admin/$client/generic-error.log;
  }

Fails on test:

nginx -t
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
: open() "/var/log/nginx/wordpress/admin/$client/generic-error.log" failed (2: No such file or directory)
configuration file /usr/local/nginx/conf/nginx.conf test failed

Have I misunderstood?

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,3722,3722#msg-3722
B
  • 3 Jul '09
miradev wrote:
> Fails on test:
> 
> nginx -t
> the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
> : open() "/var/log/nginx/wordpress/admin/$client/generic-error.log" failed (2: No such file or directory)
> configuration file /usr/local/nginx/conf/nginx.conf test failed
> 
> 
> Have I misunderstood?
> 
>

Yes, you assumed error_log was the same as access_log. They are not. 
access_log is part of the Log module while error_log is part of the main 
module. error_log (at least in 0.7.59) does not support variables in the 
path.
B
  • 5 Jul '09
I have run into a similar problem. I wanted to set up a number of virtual hosts that each share the same configuration except, of course, for the domain name and path. So my plan was to set some environment variables, and then include the template configuration file.

Unfortunately, the plan faltered somewhat because error_log and server_name do not seem to support variables.

Does anybody know of a documentation which directives support variables and which do not?

Kind regards, 

batrick

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,3722,3763#msg-3763
M
  • 7 Jul '09
I believe server_name now supports some variables but maybe this is
just in development branch.  The official documentation is in Russian
and located at http://sysoev.ru/nginx/docs/ .  There is also a wiki at
http://wiki.nginx.org with English and Chinese translations of some of
the documentation, but it is not always up to date or complete.
Google translate can do a readable job in many cases, and the
configuration language is always the same in any language, plus there
is the common language C that nginx is written in.  These are the
sources you should look through.

Completely separately regarding configurations, I am under the
understanding that nginx performs better with fully specified server
names.  It should be easy enough to put common configuration into
files which you include in each server { } block as necessary.  This
can easily be generated by a script so you don't have to do it by hand
if you have a lot of domains.  The benefits are you get the common
configuration, separate error logging, separate access logging, and
flexibility to make individual host changes while efficiently
selecting proper server blocks.

-- Merlin

On 7/5/09, batrick <nginx-forum at nginx.us> wrote:
> I have run into a similar problem. I wanted to set up a number of virtual
> hosts that each share the same configuration except, of course, for the
> domain name and path. So my plan was to set some environment variables, and
> then include the template configuration file.
>
> Unfortunately, the plan faltered somewhat because error_log and server_name
> do not seem to support variables.
>
> Does anybody know of a documentation which directives support variables and
> which do not?
>
> Kind regards,
>
> batrick
>
> Posted at Nginx Forum: http://forum.nginx.org/read.php?2,3722,3763#msg-3763
>
>
>
M
  • 7 Jul '09
batrick Wrote:
-------------------------------------------------------
> I have run into a similar problem. I wanted to set
> up a number of virtual hosts that each share the
> same configuration except, of course, for the
> domain name and path. So my plan was to set some
> environment variables, and then include the
> template configuration file.

My particular set up is not for 'live' sites (in the typical manner), and I would support Merlin's suggestion to build config files for each one. I do infact do exactly that. A simple template with some variables replaced, and you are away. You never know when you need to add just that one custom setting for that client. For the dev sites, I would just build a completely new site if something specific was wanted, but to unravel a live site from such a fixed structure might be troublesome.

> Unfortunately, the plan faltered somewhat because
> error_log and server_name do not seem to support
> variables.
> 
> Does anybody know of a documentation which
> directives support variables and which do not?

I'm guessing with the pace at which nginx is moving, that if you want such a list, you will have to head to the wiki and compile it yourself.

However, server_name does take wild cards, and you can do regular expression matching on $host. As I have done in my original post.

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,3722,3807#msg-3807