Nginx隨記

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;
}  
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容