nginx 日誌蒐集解決方案

# nginx 日誌蒐集解決方案## 系統環境描述``` java8 logstash  --監控nginx日誌文件```    ## 技術描述```  經過logstash監控nginx access.log 日誌文件,根據過濾規則和輸出適配進行日誌轉發。```   ## 提供方案### nginx 日誌 format 配置```log_format main '{"@timestamp":"$time_iso8601",''"host":"$server_addr",''"clientip":"$remote_addr",''"http_x_forwarded_for":"$http_x_forwarded_for",''"size":$body_bytes_sent,''"responsetime":$request_time,''"upstreamtime":"$upstream_response_time",''"upstreamhost":"$upstream_addr",''"http_host":"$host",''"request":"$request",''"url":"$uri",''"xff":"$http_x_forwarded_for",''"referer":"$http_referer",''"agent":"$http_user_agent",''"status":"$status"}';```### nginx 日誌 字段描述```訪問日誌        訪問日誌主要記錄客戶端訪問Nginx的每個請求,格式能夠自定義。經過訪問日誌,你能夠獲得用戶地域來源、跳轉來源、使用終端、某個URL訪問量等相關信息。Nginx中訪問日誌相關指令主要有兩條:        (1)log_format        log_format用來設置日誌格式,也就是日誌文件中每條日誌的格式,具體以下:        log_format name(格式名稱) type(格式樣式)        舉例說明以下:        log_format  main  '$server_name $remote_addr - $remote_user [$time_local] "$request" '                        '$status $uptream_status $body_bytes_sent "$http_referer" '                        '"$http_user_agent" "$http_x_forwarded_for" '                        '$ssl_protocol $ssl_cipher $upstream_addr $request_time $upstream_response_time';        上面部分爲Nginx默認指定的格式樣式,每一個樣式的含義以下:        $server_name:虛擬主機名稱。        $remote_addr:遠程客戶端的IP地址。        -:空白,用一個「-」佔位符替代,歷史緣由致使還存在。        $remote_user:遠程客戶端用戶名稱,用於記錄瀏覽者進行身份驗證時提供的名字,如登陸百度的用戶名scq2099yt,若是沒有登陸就是空白。        [$time_local]:訪問的時間與時區,好比18/Jul/2012:17:00:01 +0800,時間信息最後的"+0800"表示服務器所處時區位於UTC以後的8小時。        $request:請求的URI和HTTP協議,這是整個PV日誌記錄中最有用的信息,記錄服務器收到一個什麼樣的請求        $status:記錄請求返回的http狀態碼,好比成功是200。        $uptream_status:upstream狀態,好比成功是200.        $body_bytes_sent:發送給客戶端的文件主體內容的大小,好比899,能夠將日誌每條記錄中的這個值累加起來以粗略估計服務器吞吐量。        $http_referer:記錄從哪一個頁面連接訪問過來的。         $http_user_agent:客戶端瀏覽器信息        $http_x_forwarded_for:客戶端的真實ip,一般web服務器放在反向代理的後面,這樣就不能獲取到客戶的IP地址了,經過$remote_add拿到的IP地址是反向代理服務器的iP地址。反向代理服務器在轉發請求的http頭信息中,能夠增長x_forwarded_for信息,用以記錄原有客戶端的IP地址和原來客戶端的請求的服務器地址。        $ssl_protocol:SSL協議版本,好比TLSv1。        $ssl_cipher:交換數據中的算法,好比RC4-SHA。         $upstream_addr:upstream的地址,即真正提供服務的主機地址。         $request_time:整個請求的總時間。         $upstream_response_time:請求過程當中,upstream的響應時間。```    ### logstash 輸入輸出規則```input{        file{                path => "/usr/local/nginx/logs/access.log"                start_position => "beginning"                codec => json         }}filter{        mutate{                split => [ "upstreamtime", "," ]        }        mutate {                convert => [ "upstreamtime", "float" ]        }}output{        http {                codec => json                http_method => post                content_type => "application/json"                url => "http://192.168.100.203:7006/ITS_oa/log"       }}```### logstash 輸入,過濾,輸出規則配置[官方指南](https://www.elastic.co/guide/en/logstash/current/index.html) ### centos7 logstash 安裝,啓動命令```1. wget https://download.elastic.co/logstash/logstash/packages/centos/logstash-1.5.3-1.noarch.rpm2. yum localinstall logstash-1.5.3-1.noarch.rpm3. cd /etc/logstash/conf.d/4. 01-logstash-initial.conf  建立文件,寫入輸入,過濾,輸出規則5. systemctl start logstash && systemctl status logstash```### 提供RestApi 接口```   1. http://192.168.100.203:7006/ITS_oa/log   2. 參數爲hash類型```
相關文章
相關標籤/搜索