Nginx随记

Nginx根据http请求返回状态输出日志

Require Nginx 1.7+

由于search项目的nginx的请求日志输出太大,需要根据返回的状态切换不同的log_formate,想了个笨方法,根据nginx的status变量和两种log_formate组合输出到一个文件里实现这个需求

状态200的日志不用答应$request

状态其他的日志打印$request

/etc/nginx/nginx.conf

 http {
    map $status $loggable0 {
        ~^2  1;
        default 0;
    }
    map $status $loggable1 {
        ~^[1345]  1;
        default 0;
    }

    log_format monitor0   'OWL NGINX $remote_addr $http_x_forwarded_for $host $time_local $status '
                          '$request_time $request_length $bytes_sent $http_referer $jumei_sid '
                          ''
                          '$http_user_agent $jumei_uid';
    log_format monitor1   'OWL NGINX $remote_addr $http_x_forwarded_for $host $time_local $status '
                          '$request_time $request_length $bytes_sent $http_referer $jumei_sid '
                          '$request $http_user_agent $jumei_uid';
  }

站点配置

server {

    access_log /home/logs/nginx/search.access.log monitor0 buffer=32k flush=5s if=$loggable0;
    access_log /home/logs/nginx/search.access.log monitor1 buffer=32k flush=5s if=$loggable1;
}  

你可能感兴趣的:(Nginx随记)