How to Collect Nginx Access & Error Logs with Filebeat, Logstash, and Rsyslog
This guide demonstrates multiple methods for gathering Nginx access and error logs—directly with Filebeat to Elasticsearch, via Logstash for preprocessing, and through rsyslog forwarding—detailing configuration steps, required code snippets, and verification using Kibana and Elasticsearch-head.
Because Nginx is widely used as a high‑performance HTTP and reverse‑proxy server, its access and error logs are valuable for user behavior analysis and security monitoring.
1. Directly collect logs with Filebeat to Elasticsearch
Configure filebeat.yml to specify the log file paths and the Elasticsearch output.
- 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/*.log
- /usr/local/nginx/logs/*.log
# - c:\programdata\elasticsearch\logs\*Start Filebeat: ./filebeat -e -c filebeat.yml -d "publish" After Filebeat runs, the Nginx access.log and error.log appear in Elasticsearch and can be visualized in Kibana or with the Elasticsearch‑head plugin.
2. Collect logs via Filebeat → Logstash → Elasticsearch
Create a Logstash pipeline file filebeat-pipeline.conf that receives beats on port 5044 and forwards them to Elasticsearch.
input {
beats {
port => "5044"
}
}
output {
elasticsearch { hosts => ["172.28.65.24:9200"] }
stdout { codec => rubydebug }
}Start Logstash with automatic config reload:
bin/logstash -f filebeat-pipeline.conf --config.reload.automaticModify filebeat.yml to output to Logstash instead of Elasticsearch:
#output.elasticsearch:
# hosts: ["172.28.65.24:9200"]
output.logstash:
hosts: ["172.28.65.24:5044"]Run Filebeat again; logs flow through Logstash and appear in Elasticsearch, visible in Kibana and Elasticsearch‑head.
3. Collect logs via rsyslog → Logstash → Elasticsearch
When the target web server cannot install Filebeat, forward Nginx logs using syslog. Add syslog output in nginx.conf:
access_log syslog:server=172.28.65.32:514,facility=local7,tag=nginx_access_log,severity=info;
error_log syslog:server=172.28.65.32:514,facility=local7,tag=nginx_error_log,severity=info;Create a Logstash pipeline syslog-pipeline.conf to receive syslog on port 514 and store it in Elasticsearch.
input {
syslog {
type => "system-syslog"
port => 514
}
}
output {
elasticsearch {
hosts => ["172.28.65.24:9200"]
index => "system-syslog-%{+YYYY.MM}"
}
stdout { codec => rubydebug }
}Start Logstash:
bin/logstash -f syslog-pipeline.conf --config.reload.automaticIf Nginx cannot emit syslog directly, configure rsyslog on the Nginx host to monitor the log files and forward them:
$ModLoad imfile
$InputFilePollInterval 1
$WorkDirectory /var/spool/rsyslog
$PrivDropToGroup adm
# Nginx access log
$InputFileName /usr/local/nginx/logs/access.log
$InputFileTag nginx-access:
$InputFileStateFile stat-nginx-access
$InputFileSeverity info
$InputFilePersistStateInterval 25000
$InputRunFileMonitor
# Nginx error log
$InputFileName /usr/local/nginx/logs/error.log
$InputFileTag nginx-error:
$InputFileStateFile stat-nginx-error
$InputFileSeverity error
$InputFilePersistStateInterval 25000
$InputRunFileMonitor
*.* @172.28.65:514Restart rsyslog: systemctl restart rsyslog After sending requests to the Nginx service (e.g., http://172.28.65.32/), the logs appear in Logstash’s console and are indexed in Elasticsearch, where they can be inspected with Kibana or Elasticsearch‑head.
The article summarizes several practical ways to collect Nginx access and error logs using Filebeat, Logstash, and rsyslog, allowing you to choose the most suitable method for your environment.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
