How to collect Nginx logs using Filebeat

How to collect Nginx logs using Filebeat

Nginx logs can be used to analyze user address locations, behavior profiles, etc. How can we use Elastic Stack to perform one-stop data collection, data cleaning, data landing, and data visualization to make the data truly valuable?

Architecture Design

In the Elastic Stack, Filebeat is used to collect Nginx-related logs, Elasticsearch is an engine for data storage and search, and Kibana is a tool for data visualization.

In Nginx, the relevant logs are stored in the /var/log/nginx directory, namely the access log access.log and the error log error.log.

insert image description here

If it is a bare metal environment, you can directly install Filebeat on the same host to collect log files.
If it is a Docker environment, it is recommended that Nginx use Volume to share log files for Filebeat collection.
If it is a Kubernetes environment, it is recommended to add Filebeat Container to the Pod to collect PV.

There are different collection solutions for different scenarios. Some can use Daemonset to collect logs on the host, while others can use Sidecar to collect logs, depending on the business scenario.

Implementation Methods

Take Docker environment as an example

Nginx

Create a storage volume to facilitate the joint mounting of Nginx and Filebeat containers
docker volume create nginx-log-volume

Start the Nginx container and map the storage volume to the log directory
docker run -d --name nginx -p 80:80 -v nginx-log-volume:/var/log/nginx nginx:latest

Enter the container to modify the configuration
docker exec -it nginx /bin/bash

Since the default log in the container environment is output to stdout, cancel this setting and specify a file
unlink /var/log/nginx/access.log
unlink /var/log/nginx/error.log
touch /var/log/nginx/access.log /var/log/nginx/error.log
nginx -s reload

Filebeat

Start the Filebeat container and map the storage volume to the data directory
docker run -d --name filebeat --user=root -v nginx-log-volume:/data elastic/filebeat:7.9.2

Enter the container to modify the configuration
docker exec -it filebeat /bin/bash

Modify the configuration and add the hosts for Elasticsearch and Kibana
vi filebeat.yml

filebeat.config:
 modules:
  path: ${path.config}/modules.d/*.yml
  reload.enabled: false

processors:
 - add_cloud_metadata: ~
 - add_docker_metadata: ~

output.elasticsearch:
 hosts: 'elasticsearch:9200'
 username: "elastic"
 password: "xxx"
setup.kibana:
 host: "kibana:5601"

Enable Nginx collection module

filebeat modules enable nginx

Edit Nginx collection configuration
vi modules.d/nginx.yml

- module: nginx
 access:
  enabled: true
  var.paths: ["/data/access.log*"]
 error:
  enabled: true
  var.paths: ["/data/error.log*"]

Set up Filebeat to create an Index Pattern and Dashboard on Kibana
filebeat setup

Restart Filebeat to take effect
docker restart filebeat

Visualization

Use the Dashboard function in Kibana to display Nginx's access to logs, user address location, and browser information

insert image description here

Displays Nginx's specific request information for access logs and error logs

insert image description here

You may also be interested in:
  • Detailed explanation of Nginx log customization and enabling log buffer
  • Detailed explanation of the idea of ​​rolling nginx logs in docker
  • Add request response log to nginx log (recommended)
  • Detailed explanation of nginx access log format
  • How to set a more detailed log format for Nginx server using log_format

<<:  Summary of the pitfalls you may not have encountered in WeChat applet development

>>:  A problem with MySQL 5.5 deployment

Recommend

Several ways to change MySQL password

Preface: In the daily use of the database, it is ...

Comprehensive understanding of line-height and vertical-align

Previous words Line-height, font-size, and vertica...

Float and Clear Float in Overview Page

1. Float: The main purpose is to achieve the effe...

How to use Samba to build a shared file service on a Linux server

Recently, our small team needs to share a shared ...

How to install kibana tokenizer inside docker container

step: 1. Create a new docker-compose.yml file in ...

Vue storage contains a solution for Boolean values

Vue stores storage with Boolean values I encounte...

CSS3 achieves cool sliced ​​image carousel effect

Today we will learn how to use CSS to create a co...

Install CentOS 7 on VMware14 Graphic Tutorial

Introduction to CentOS CentOS is an enterprise-cl...

Solve the problem of docker's tls (ssl) certificate expiration

Problem phenomenon: [root@localhost ~]# docker im...

mysql obtains statistical data within a specified time period

mysql obtains statistical data within a specified...

The specific use and difference between attribute and property in Vue

Table of contents As attribute and property value...

Introduction to using Unicode characters in web pages (&#,\u, etc.)

The earliest computers could only use ASCII chara...

How to implement page screenshot function in JS

"Page screenshot" is a requirement ofte...