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

Detailed steps to build an NFS file sharing server in Linux

Linux builds NFS server In order to achieve data ...

Linux operation and maintenance basic swap partition and lvm management tutorial

Table of contents 1. Swap partition SWAP 1.1 Crea...

Minimalistic website design examples

Web Application Class 1. DownForEveryoneOrJustMe ...

What you need to know about msyql transaction isolation

What is a transaction? A transaction is a logical...

Detailed explanation of Mencached cache configuration based on Nginx

Introduction Memcached is a distributed caching s...

Detailed explanation of how to use JavaScript paging component

The pagination component is a common component in...

CSS implements the web component function of sliding the message panel

Hello everyone, I wonder if you have the same con...

A brief discussion on the efficiency of MySQL subquery union and in

Recent product testing found a problem that when ...

Vue realizes screen adaptation of large screen pages

This article shares the specific code of Vue to a...

Each time Docker starts a container, the IP and hosts specified operations

Preface Every time you use Docker to start a Hado...

Example code for using @media in CSS3 to achieve web page adaptation

Nowadays, the screen resolution of computer monit...

CSS draw a lollipop example code

Background: Make a little progress every day, acc...

Installation method of mysql-8.0.17-winx64 under windows 10

1. Download from the official website and unzip h...

MySQL 8.0 New Features - Introduction to Check Constraints

Table of contents Preface Check Constraints Creat...

Win7 installation MySQL 5.6 tutorial diagram

Table of contents 1. Download 2. Installation 3. ...