nginx - how to use PCRE match in access_log path -


i read article http://wiki.nginx.org/httplogmodule , seems possible. i'd have in config directives below doesn't seem work. can provide guidance?

location ~ /foo/(.*) {     access_log /var/log/$1.access.log;   } 

there few things should keep in mind, must knowing of them ;).

  1. nginx has one parent process (master process) runs root.
  2. the default logs created (if doesn't exists) when nginx gets started/restarted/reloaded , root user.
  3. so obvious owner of /var/log/nginx root.

    drwxr-xr-x  2 root root   4096 aug 14 01:35 nginx/ 

    or generalize, owner /var/log root, , root can write in it.

  4. any request processed the worker process ( owned & run 'nginx/nobody' user), not by the master process ( owned & run 'root' user).

    if no nginx user there worker processes run user nobody*.

    root      1272  0.0  0.1  12080  3932 ?        ss   00:27   0:00 nginx: master process         /usr/sbin/nginx -c /etc/nginx/nginx.conf nobody    4226  0.0  0.0  12240  2236 ?        s    01:43   0:00 nginx: worker process 
  5. and in scenario while processing location block if comes write log in /var/log/nginx permission error :'( (as nginx or nobody user, not having permission write in directory).

    this applicable case log in /var/log

    2013/08/14 02:00:39 [crit] 4226#0: *20 open() "/var/log/nginx/adad?qwerty.access.log" failed (13: permission denied) while logging request, client: 127.0.0.1, server: localhost, request: "get /adad?qwerty http/1.1", host: "localhost" 

    in above example writing log as

    access_log /var/log/nginx/$request_uri.access.log; 

    which got failed.

so make things working, follow way.

  1. create user nginx, advised --

    useradd -s /sbin/nologin -m nginx 
  2. update nginx config /etc/nginx/nginx.conf ( add in first line of config) , reload (service nginx reload)

    user    nginx; 

    now worker process (child process) run user - 'nginx'

  3. make common directory keeping logs /var/log/nginx/ , update nginx config point location.

    make nginx reload after this.

  4. and important thing, make directory owned nginx user.

    chown -r nginx:nginx /var/log/nginx  
  5. finally, can use @vbart's method in location block.

and if goes wrong, please enable error_log in debug mode, , tell problems if any.

and considering @troycheng's comment below,

and if have started nginx service i.e. master process, other user 'work' or (nginx case) , child process run privileges, , in scenario have (tough start process must have done this) allow user write logs in logging directory /var/log/nginx


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -