首頁 > 軟體

ELK收集Nginx紀錄檔的專案實戰

2022-05-13 21:51:10

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選項表示並行請求結果數,-n選項表示請求次數。下面命令表示進行100次請求,10個並行請求壓力測試結果。另外,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組態檔
# 採集紀錄檔資料設定
filebeat.inputs: 
- type: log
  enabled: true
  paths:
    - /var/log/nginx/access.log

2.2 使用 Kibana 展示 Nginx 紀錄檔資料

採集紀錄檔資料

設定好filebeat採集Nginx紀錄檔資料之後,在ES-head中可以看到如下紀錄檔內容

建立紀錄檔資料索引

然後在伺服器啟動kibana並使用瀏覽器存取http://115.156.128.172:5601/進入kibana。在該頁面中選擇新增資料Add your data

然後,選擇建立索引create index pattern

在提示欄中,選擇對應filebeat採集的紀錄檔資料建立索引,並選擇時間戳@timestamp

檢視紀錄檔資料

完成索引建立之後,使用Discovery檢視紀錄檔資料

在紀錄檔資料通過設定紀錄檔範圍和紀錄檔欄位檢視指定紀錄檔內容,也可以通過全文搜尋和新增過濾器的功能檢視指定資料

03 採集 JSON 格式的 Nginx 紀錄檔

預設情況下Nginx的紀錄檔資料是一條文字條目,紀錄檔條目中的欄位無法拆分顯示。採用怎樣的方式可以將這種非結構化的紀錄檔內容轉化成格式化的內容呢?

# 將如下紀錄檔條目
message:172.16.255.131 - - [28/Jul/2021:07:19:53 +0000] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3"
# 轉換成JSON半結構化資料如下
{   "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產生的紀錄檔採用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原有的紀錄檔資料,重新啟動Nginx並使用ab工具重新對其進行壓測,產生新的紀錄檔資料

> /var/log/nginx/access.log # 清空Nginx原有的紀錄檔資料
systemctl restart nginx # 重新啟動Nginx
ab -c 10 -n 100 172.16.255.131/ # 使用ab工具重新進行壓測

檢視Nginx紀錄檔,可以看到Nginx紀錄檔已經被重新以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監控Nginx紀錄檔後,採用ab壓測工具生成紀錄檔並採集。

> /var/log/nginx/access.log
systemctl restart filebeat
ab -c 10 -n 100 172.16.255.131/

採用ES-head檢視採集的紀錄檔資料可以看到紀錄檔資料以Json格式儲存在ES中

使用Kibana檢視Json格式的紀錄檔條目

建立新的Kibana索引後,使用Discovery檢視紀錄檔資料,並可以通過紀錄檔條目級的欄位到達更有效的紀錄檔分析目的

3.3 自定義儲存 Filebeat 採集紀錄檔的 ES 索引

之前使用Filebeat採集Nginx紀錄檔都是採用預設的索引建立方式形如filebeat-7.13.2-2021.07.30-000001,為了更好的識別索引和擴大紀錄檔採集的時間跨度,需要自定義儲存索引名稱。

自定義儲存索引通過設定Filebeat實現,在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 多節點採集 Nginx 紀錄檔資料

4.1 在其他節點上安裝 Filebeat 和 Nginx

從已經安裝好的節點上將filebeat安裝包拷貝到其他節點上

# 拷貝安裝包
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/
# 在其他節點上安裝filebeat
cd /opt/es/
sudo dpkg -i filebeat-7.13.2-amd64.deb # 安裝deb包
# 安裝Nginx
sudo apt-get install nginx -y

4.2 在其他節點上設定 Filebeat 和 Nginx

從已經安裝好的節點上將filebeat的組態檔拷貝到其他節點上

# 拷貝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/
# 在對應節點上將組態檔移動到對應目錄覆蓋原始組態檔
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 在其他節點上啟動 Filebeat 和 Nginx

啟動Filebeat和Nginx並使用master節點的ab工具進行壓測產生紀錄檔資料

# 啟動Filebeat和Nginx
systemctl start nginx
systemctl start filebeat
# 使用master節點的ab工具進行壓測產生紀錄檔資料
ab -n 100 -c 20 http://172.16.255.132/node1.html
ab -n 100 -c 20 http://172.16.255.139/node2.html
# 檢視產生的紀錄檔資料是否為Json格式
tail -f /var/log/nginx/access.log

Filebeat的組態檔將紀錄檔資料採集並儲存在ES中,多個節點的紀錄檔資料被聚合在一個ES索引中儲存。

05 收集 Nginx 錯誤紀錄檔

收集錯誤紀錄檔的需求:要能夠區分錯誤紀錄檔和正常紀錄檔,要能夠是使用單獨索引儲存錯誤紀錄檔

5.1 設定 Filebeat 採集 Nginx 錯誤紀錄檔

在filebeat組態檔etc/filebeat/filebeat.ymlinputs選項中新增如下內容

- type: log
  enabled: true
  paths:
    - /var/log/nginx/error.log
  tags: ["error"]

5.2 設定 Filebeat 採集紀錄檔時拆分錯誤紀錄檔和正常紀錄檔

在filebeat組態檔etc/filebeat/filebeat.yml的輸入中加入tags標識採集的不同型別紀錄檔資料,然後在索引設定中設定如下對紀錄檔進行拆分

# 在`output`中設定通過tags區分紀錄檔
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"

讓其他節點上採集Nginx紀錄檔的Filebeat的組態檔於上述設定一致,直接將該filebeat的組態檔拷貝到其他節點上覆蓋

# 拷貝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/
# 在對應節點上將組態檔移動到對應目錄覆蓋原始組態檔
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紀錄檔的專案實戰的文章就介紹到這了,更多相關ELK Nginx紀錄檔內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com