ELK收集Nginx日志的項目實戰(zhàn)
01 安裝 Nginx 和 ab 工具
1.1 安裝 nginx
sudo apt-get install nginx -y # 安裝Nginx sudo apt-get install apache2-utils -y # Ubuntu安裝ab工具 sudo yum -y install httpd-tools 0y # CentOS安裝ab工具
在線安裝完成后,Nginx主要文件目錄構成如下
/etc/nginx # 配置文件 /etc/nginx/sites-available # 虛擬主機 /usr/sbin/nginx # 啟動程序文件 /var/log/nginx # 日志目錄,包含access.log和error.log
1.2 啟動 Nginx 并測試
啟動Nginx,并使用netstat命令查看端口
systemctl start nginx # 啟動nginx netstat -lntup|grep nginx # 查看nginx是否啟動成功
使用壓力測試工具測試Nginx,其中-c選項表示并發(fā)請求結果數(shù),-n選項表示請求次數(shù)。下面命令表示進行100次請求,10個并發(fā)請求壓力測試結果。另外,ab壓測工具的一個缺陷是需要在壓測URL后加上/符號。
ab -c 10 -n 100 172.16.255.131/ ab -c 10 -n 100 172.16.255.131/test.html/
壓力測試完成之后,查看Nginx日志可以得到如下結果
root@master:/etc/nginx# tail -f /var/log/nginx/access.log 172.16.255.131 - - [28/Jul/2021:07:19:53 +0000] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3" 172.16.255.131 - - [28/Jul/2021:07:19:53 +0000] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3" 172.16.255.131 - - [28/Jul/2021:07:19:53 +0000] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3" 172.16.255.131 - - [28/Jul/2021:07:19:53 +0000] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3" 172.16.255.131 - - [28/Jul/2021:07:19:53 +0000] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3" 172.16.255.131 - - [28/Jul/2021:07:19:53 +0000] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3" 172.16.255.131 - - [28/Jul/2021:07:19:53 +0000] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3" 172.16.255.131 - - [28/Jul/2021:07:19:53 +0000] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3" 172.16.255.131 - - [28/Jul/2021:07:19:53 +0000] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3" 172.16.255.131 - - [28/Jul/2021:07:19:53 +0000] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3"
02 使用 Filebeat 采集日志并展示
2.1 配置 filebeat 采集 Nginx 日志
在/etc/filebeat/filebeat.yml配置文件中對filebeat進行配置,將nginx的日志路徑/var/log/nginx/access.log添加到filebeat的輸入配置中
vim /etc/filebeat/filebeat.yml # 打開filebeat配置文件
# 采集日志數(shù)據(jù)配置
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/access.log
2.2 使用 Kibana 展示 Nginx 日志數(shù)據(jù)
采集日志數(shù)據(jù)
配置好filebeat采集Nginx日志數(shù)據(jù)之后,在ES-head中可以看到如下日志內容

創(chuàng)建日志數(shù)據(jù)索引
然后在服務器啟動kibana并使用瀏覽器訪問http://115.156.128.172:5601/進入kibana。在該頁面中選擇添加數(shù)據(jù)Add your data

然后,選擇創(chuàng)建索引create index pattern

在提示欄中,選擇對應filebeat采集的日志數(shù)據(jù)創(chuàng)建索引,并選擇時間戳@timestamp


查看日志數(shù)據(jù)
完成索引創(chuàng)建之后,使用Discovery查看日志數(shù)據(jù)

在日志數(shù)據(jù)通過設置日志范圍和日志字段查看指定日志內容,也可以通過全文搜索和添加過濾器的功能查看指定數(shù)據(jù)

03 采集 JSON 格式的 Nginx 日志
默認情況下Nginx的日志數(shù)據(jù)是一條文本條目,日志條目中的字段無法拆分顯示。采用怎樣的方式可以將這種非結構化的日志內容轉化成格式化的內容呢?
# 將如下日志條目
message:172.16.255.131 - - [28/Jul/2021:07:19:53 +0000] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3"
# 轉換成JSON半結構化數(shù)據(jù)如下
{ "IP Address": 172.16.255.131,
"Time": [28/Jul/2021:07:19:53 +0000],
"HTTP Request": GET / HTTP/1.0 200 612,
"Agent": ApacheBench/2.3 }
3.1 修改 Nginx 日志為 Json 格式
一種方法是直接將Nginx產(chǎn)生的日志采用Json格式保存,編輯Nginx的配置文件/etc/nginx/nginx.conf,添加日志保存樣式
vim /etc/nginx/nginx.conf # 找到http中的logging settings
# 添加如下內容
log_format log_json '{ "@time_local": "$time_local", '
'"remote_addr": "$remote_addr", '
'"referer": "$http_referer", '
'"request": "$request", '
'"status": $status, '
'"bytes": $body_bytes_sent, '
'"agent": "$http_user_agent", '
'"x_forwarded": "$http_x_forwarded_for", '
'"up_addr": "$upstream_addr",'
'"up_host": "$upstream_http_host",'
'"up_resp_time": "$upstream_response_time",'
'"request_time": "$request_time"'
' }';

測試nginx配置文件修改是否有效,得到如下輸出說明成功
root@master:/home/wang# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
清空Nginx原有的日志數(shù)據(jù),重新啟動Nginx并使用ab工具重新對其進行壓測,產(chǎn)生新的日志數(shù)據(jù)
> /var/log/nginx/access.log # 清空Nginx原有的日志數(shù)據(jù) systemctl restart nginx # 重新啟動Nginx ab -c 10 -n 100 172.16.255.131/ # 使用ab工具重新進行壓測
查看Nginx日志,可以看到Nginx日志已經(jīng)被重新以Json格式存儲
root@master:/home/wang# tail -f /var/log/nginx/access.log
{ "@timestamp": "30/Jul/2021:03:17:09 +0000", "remote_addr": "172.16.255.131", "referer": "-", "request": "GET / HTTP/1.0", "status": 200, "bytes": 612, "agent": "ApacheBench/2.3", "x_forwarded": "-", "up_addr": "-","up_host": "-","up_resp_time": "-","request_time": "0.000" }
{ "@timestamp": "30/Jul/2021:03:17:09 +0000", "remote_addr": "172.16.255.131", "referer": "-", "request": "GET / HTTP/1.0", "status": 200, "bytes": 612, "agent": "ApacheBench/2.3", "x_forwarded": "-", "up_addr": "-","up_host": "-","up_resp_time": "-","request_time": "0.000" }
{ "@timestamp": "30/Jul/2021:03:17:09 +0000", "remote_addr": "172.16.255.131", "referer": "-", "request": "GET / HTTP/1.0", "status": 200, "bytes": 612, "agent": "ApacheBench/2.3", "x_forwarded": "-", "up_addr": "-","up_host": "-","up_resp_time": "-","request_time": "0.000" }
{ "@timestamp": "30/Jul/2021:03:17:09 +0000", "remote_addr": "172.16.255.131", "referer": "-", "request": "GET / HTTP/1.0", "status": 200, "bytes": 612, "agent": "ApacheBench/2.3", "x_forwarded": "-", "up_addr": "-","up_host": "-","up_resp_time": "-","request_time": "0.000" }
{ "@timestamp": "30/Jul/2021:03:17:09 +0000", "remote_addr": "172.16.255.131", "referer": "-", "request": "GET / HTTP/1.0", "status": 200, "bytes": 612, "agent": "ApacheBench/2.3", "x_forwarded": "-", "up_addr": "-","up_host": "-","up_resp_time": "-","request_time": "0.000" }
{ "@timestamp": "30/Jul/2021:03:17:09 +0000", "remote_addr": "172.16.255.131", "referer": "-", "request": "GET / HTTP/1.0", "status": 200, "bytes": 612, "agent": "ApacheBench/2.3", "x_forwarded": "-", "up_addr": "-","up_host": "-","up_resp_time": "-","request_time": "0.000" }
{ "@timestamp": "30/Jul/2021:03:17:09 +0000", "remote_addr": "172.16.255.131", "referer": "-", "request": "GET / HTTP/1.0", "status": 200, "bytes": 612, "agent": "ApacheBench/2.3", "x_forwarded": "-", "up_addr": "-","up_host": "-","up_resp_time": "-","request_time": "0.000" }
{ "@timestamp": "30/Jul/2021:03:17:09 +0000", "remote_addr": "172.16.255.131", "referer": "-", "request": "GET / HTTP/1.0", "status": 200, "bytes": 612, "agent": "ApacheBench/2.3", "x_forwarded": "-", "up_addr": "-","up_host": "-","up_resp_time": "-","request_time": "0.000" }
{ "@timestamp": "30/Jul/2021:03:17:09 +0000", "remote_addr": "172.16.255.131", "referer": "-", "request": "GET / HTTP/1.0", "status": 200, "bytes": 612, "agent": "ApacheBench/2.3", "x_forwarded": "-", "up_addr": "-","up_host": "-","up_resp_time": "-","request_time": "0.000" }
{ "@timestamp": "30/Jul/2021:03:17:09 +0000", "remote_addr": "172.16.255.131", "referer": "-", "request": "GET / HTTP/1.0", "status": 200, "bytes": 612, "agent": "ApacheBench/2.3", "x_forwarded": "-", "up_addr": "-","up_host": "-","up_resp_time": "-","request_time": "0.000" }
3.2 Filebeat 重新采集 Nginx 日志
配置Filebeat識別Json格式日志
修改Nginx保存日志格式為Json之后,還需要對采集日志的Filebeat進行重新配置,如果不對其進行配置識別Json格式日志,被采集的日志仍然會以文本條目的形式被采集。
# 打開filebeat配置文件
vim /etc/filebeat/filebeat.yml
# 添加配置內容,配置內容可以參考官方手冊
- type: log
# Change to true to enable this input configuration.
enabled: true
# Paths that should be crawled and fetched. Glob based paths.
paths:
- /var/log/nginx/access.log
# 添加如下三行配置內容,識別Json格式日志文件,將日志條目以Json格式解析
json.keys_under_root: true
json.overwrite_keys: true
tags: ["access"]
清空Nginx日志,并重新啟動filebeat;filebeat監(jiān)控Nginx日志后,采用ab壓測工具生成日志并采集。
> /var/log/nginx/access.log systemctl restart filebeat ab -c 10 -n 100 172.16.255.131/
采用ES-head查看采集的日志數(shù)據(jù)可以看到日志數(shù)據(jù)以Json格式保存在ES中

使用Kibana查看Json格式的日志條目
創(chuàng)建新的Kibana索引后,使用Discovery查看日志數(shù)據(jù),并可以通過日志條目級的字段到達更有效的日志分析目的

3.3 自定義存儲 Filebeat 采集日志的 ES 索引
之前使用Filebeat采集Nginx日志都是采用默認的索引創(chuàng)建方式形如filebeat-7.13.2-2021.07.30-000001,為了更好的識別索引和擴大日志采集的時間跨度,需要自定義存儲索引名稱。
自定義存儲索引通過配置Filebeat實現(xiàn),在Filebeat的配置文件中對輸出進行配置如下:
# 打開filebeat配置文件
vim /etc/filebeat/filebeat.yml
# 添加配置內容,配置內容可以參考官方手冊
# ---------------------------- Elasticsearch template setting ----------------------
setup.template.settings:
index.number_of_shards: 1
#index.codec: best_compression
#_source.enabled: false
setup.template.name: "nginx" # 名字和index中的名字一致
setup.template.pattern: "nginx-*"
setup.template.enabled: false
setup.template.overwrite: true
setup.ilm.enabled: false
# ---------------------------- Elasticsearch Output ----------------------------
output.elasticsearch:
# Array of hosts to connect to.
hosts: ["172.16.255.131:9200"]
# 添加如下五行內容
# add index settings by wanghaihua at 2021.07.30
index: "nginx-%{[agent.version]}-%{+yyyy.MM}" # 限定日期形式{+yyyy.MM}
采用這種配置可以自定義索引樣式,并自定義Kibana中搜索的字段,刪除冗余的解析字段

04 使用 Filebeat 多節(jié)點采集 Nginx 日志數(shù)據(jù)
4.1 在其他節(jié)點上安裝 Filebeat 和 Nginx
從已經(jīng)安裝好的節(jié)點上將filebeat安裝包拷貝到其他節(jié)點上
# 拷貝安裝包 scp filebeat-7.13.2-amd64.deb wang@172.16.255.132:/opt/es/ scp filebeat-7.13.2-amd64.deb wang@172.16.255.139:/opt/es/ # 在其他節(jié)點上安裝filebeat cd /opt/es/ sudo dpkg -i filebeat-7.13.2-amd64.deb # 安裝deb包 # 安裝Nginx sudo apt-get install nginx -y
4.2 在其他節(jié)點上配置 Filebeat 和 Nginx
從已經(jīng)安裝好的節(jié)點上將filebeat的配置文件拷貝到其他節(jié)點上
# 拷貝filebeat配置文件到一個暫存目錄(直接拷貝到etc目錄下可能存在權限問題) scp /etc/filebeat/filebeat.yml wang@172.16.255.132:/opt/es/ scp /etc/filebeat/filebeat.yml wang@172.16.255.139:/opt/es/ scp /etc/nginx/nginx.conf wang@172.16.255.132:/opt/es/ scp /etc/nginx/nginx.conf wang@172.16.255.139:/opt/es/ # 在對應節(jié)點上將配置文件移動到對應目錄覆蓋原始配置文件 mv /opt/es/filebeat.yml /etc/filebeat/ mv /opt/es/nginx.conf /etc/nginx/ # 修改用戶權限 chown -R root:root /etc/nginx/nginx.conf chown -R root:root /etc/filebeat/filebeat.yml
4.3 在其他節(jié)點上啟動 Filebeat 和 Nginx
啟動Filebeat和Nginx并使用master節(jié)點的ab工具進行壓測產(chǎn)生日志數(shù)據(jù)
# 啟動Filebeat和Nginx systemctl start nginx systemctl start filebeat # 使用master節(jié)點的ab工具進行壓測產(chǎn)生日志數(shù)據(jù) ab -n 100 -c 20 http://172.16.255.132/node1.html ab -n 100 -c 20 http://172.16.255.139/node2.html # 查看產(chǎn)生的日志數(shù)據(jù)是否為Json格式 tail -f /var/log/nginx/access.log
Filebeat的配置文件將日志數(shù)據(jù)采集并存儲在ES中,多個節(jié)點的日志數(shù)據(jù)被聚合在一個ES索引中保存。
05 收集 Nginx 錯誤日志
收集錯誤日志的需求:要能夠區(qū)分錯誤日志和正常日志,要能夠是使用單獨索引存儲錯誤日志
5.1 配置 Filebeat 采集 Nginx 錯誤日志
在filebeat配置文件etc/filebeat/filebeat.yml的inputs選項中添加如下內容
- type: log
enabled: true
paths:
- /var/log/nginx/error.log
tags: ["error"]
5.2 配置 Filebeat 采集日志時拆分錯誤日志和正常日志
在filebeat配置文件etc/filebeat/filebeat.yml的輸入中加入tags標識采集的不同類型日志數(shù)據(jù),然后在索引設置中配置如下對日志進行拆分
# 在`output`中配置通過tags區(qū)分日志
output.elasticsearch:
hosts: ["172.16.255.131:9200"]
indices:
- index: "nginx-access-%{[agent.version]}-%{+yyyy.MM}"
when.contains:
tags: "access"
- index: "nginx-error-%{[agent.version]}-%{+yyyy.MM}"
when.contains:
tags: "error"
讓其他節(jié)點上采集Nginx日志的Filebeat的配置文件于上述配置一致,直接將該filebeat的配置文件拷貝到其他節(jié)點上覆蓋
# 拷貝filebeat配置文件到一個暫存目錄(直接拷貝到etc目錄下可能存在權限問題) scp /etc/filebeat/filebeat.yml wang@172.16.255.132:/opt/es/ scp /etc/filebeat/filebeat.yml wang@172.16.255.139:/opt/es/ # 在對應節(jié)點上將配置文件移動到對應目錄覆蓋原始配置文件 mv /opt/es/filebeat.yml /etc/filebeat/ # 修改用戶權限 chown -R root:root /etc/filebeat/filebeat.yml
06 Filebeat 采集 Nginx 日志的最終配置文件
# ============================== Filebeat inputs ===============================
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/access.log
json.keys_under_root: true
json.overwrite_keys: true
tags: ["access"]
- type: log
enabled: true
paths:
- /var/log/nginx/error.log
tags: ["error"]
# ======================= Elasticsearch template setting =======================
setup.template.settings:
index.number_of_shards: 1
setup.template.name: "nginx"
setup.template.pattern: "nginx-*"
setup.template.enabled: false
setup.template.overwrite: true
setup.ilm.enabled: false
# ================================== Outputs ===========================
# ---------------------------- Elasticsearch Output ----------------------------
output.elasticsearch:
hosts: ["172.16.255.131:9200"]
indices:
- index: "nginx-access-%{[agent.version]}-%{+yyyy.MM}"
when.contains:
tags: "access"
- index: "nginx-error-%{[agent.version]}-%{+yyyy.MM}"
when.contains:
tags: "error"
# ================================== Logging ===================================
logging.level: info
logging.to_files: true
logging.files:
path: /var/log/filebeat
name: filebeat
keepfiles: 7
permissions: 0644
到此這篇關于ELK收集Nginx日志的項目實戰(zhàn)的文章就介紹到這了,更多相關ELK Nginx日志內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
nginx快速部署一個網(wǎng)站服務(多域名+多端口)
本文主要介紹了nginx快速部署一個網(wǎng)站服務,并實現(xiàn)多域名和多端口,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-10-10
借用nginx.vim工具進行語法高亮和格式化配置nginx.conf文件
今天小編就為大家分享一篇關于借用nginx.vim工具進行語法高亮和格式化配置nginx.conf文件,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-02-02
Apache Nginx 禁止目錄執(zhí)行PHP腳本文件的方法
這篇文章主要介紹了Apache Nginx 禁止目錄執(zhí)行PHP腳本文件的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06
Windows環(huán)境實現(xiàn)Nginx配置及開機自啟動
本文主要介紹了Windows環(huán)境實現(xiàn)Nginx配置及開機自啟動,通過兩種方式可以實現(xiàn)nginx的開機自啟動,具有一定的參考價值,感興趣的可以了解一下2024-03-03
Nginx listen 監(jiān)聽端口的實現(xiàn)配置
本文將介紹Nginx的listen指令及其在配置文件中的應用,通過了解listen指令,我們可以知道Nginx如何監(jiān)聽端口,并配置相應的服務器塊來處理進入的請求2023-12-12
nginx配置keepalive長連接的實現(xiàn)方法
長連接允許客戶端在同一個TCP連接上發(fā)送多個請求,以減少連接握手的開銷,提高網(wǎng)站性能,本文主要介紹了nginx配置keepalive長連接的實現(xiàn)方法,感興趣的可以了解一下2023-08-08
Nginx服務器中l(wèi)ocation配置的一些基本要點解析
這篇文章主要介紹了Nginx服務器中l(wèi)ocation配置的一些基本要點解析,特別對管理以及查找匹配作出了詳細的講解,需要的朋友可以參考下2015-12-12

