Nginx根據(jù)http請(qǐng)求返回狀態(tài)輸出日志
Require Nginx 1.7+
由于search項(xiàng)目的nginx的請(qǐng)求日志輸出太大,需要根據(jù)返回的狀態(tài)切換不同的log_formate,想了個(gè)笨方法,根據(jù)nginx的status變量和兩種log_formate組合輸出到一個(gè)文件里實(shí)現(xiàn)這個(gè)需求
狀態(tài)200的日志不用答應(yīng)$request
狀態(tài)其他的日志打印$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';
}
站點(diǎn)配置
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;
}