LearningNginxlogconfiguration, log格式, loganalysis and log轮转, Masterlogmanagement coretechniques
log is Nginxserver important 组成部分, 它记录了server runstatus, requestprocessingcircumstances and errorinformation. throughlogmanagement, 可以monitorserver runstatus, analysis访问模式, 排查error and securityissues, for server optimization and maintenanceproviding important 依据.
访问log记录了客户端 for server 所 has request, includingrequest时间, requestmethod, requestURL, status码, response big small , response时间etc.information. throughanalysis访问log, 可以Understand网站 访问circumstances, userbehavior and serverperformance.
# basic访问logconfiguration
http {
# 定义log格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# 全局访问logconfiguration
access_log /var/log/nginx/access.log main;
server {
listen 80;
server_name example.com;
# server级别 访问logconfiguration
access_log /var/log/nginx/example.access.log main;
location / {
root /var/www/example.com;
index index.html;
}
}
}
# 自定义log格式
http {
# 标准log格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# package含response时间 log格式
log_format extended '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'rt=$request_time ut=$upstream_response_time cs=$upstream_cache_status';
# package含request体 big small log格式
log_format detailed '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent $request_length "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'rt=$request_time ut=$upstream_response_time';
# using自定义log格式
access_log /var/log/nginx/access.log extended;
server {
listen 80;
server_name example.com;
access_log /var/log/nginx/example.access.log detailed;
location / {
root /var/www/example.com;
index index.html;
}
}
}
# 按虚拟主机configuration访问log
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server {
listen 80;
server_name example.com;
access_log /var/log/nginx/example.access.log main;
error_log /var/log/nginx/example.error.log warn;
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 80;
server_name api.example.com;
access_log /var/log/nginx/api.access.log main;
error_log /var/log/nginx/api.error.log warn;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
errorlog记录了server errorinformation, includingconfigurationerror, run时error, 连接erroretc.. througherrorlog, 可以 fast 速定位 and 解决server issues, 确保server 正常run.
# basicerrorlogconfiguration
http {
# 全局errorlogconfiguration
error_log /var/log/nginx/error.log warn;
server {
listen 80;
server_name example.com;
# server级别 errorlogconfiguration
error_log /var/log/nginx/example.error.log error;
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
}
Nginxerrorlogsupport以 under 级别 ( from low to high ) :
建议: in produceenvironmentin, 建议using warn or error 级别, 避免logfile过 big .
# 按environmentconfigurationerrorlog
http {
# Developmentenvironment: 详细log
error_log /var/log/nginx/error.log debug;
# produceenvironment: 只记录warning and error
# error_log /var/log/nginx/error.log warn;
server {
listen 80;
server_name example.com;
error_log /var/log/nginx/example.error.log error;
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 80;
server_name api.example.com;
# APIserver: 更详细 errorlog
error_log /var/log/nginx/api.error.log info;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
随着网站traffic 增 long , logfile会变得越来越 big , 手动analysislog变得非常 difficult . using专业 loganalysistool, 可以更 high 效地analysislogdata, 提取 has value information, for server optimization and maintenanceproviding依据.
# installationGoAccess (Ubuntu/Debian) sudo apt update sudo apt install goaccess # installationGoAccess (CentOS/RHEL) sudo yum install epel-release sudo yum install goaccess # analysis访问log并生成HTML报告 goaccess /var/log/nginx/access.log -o /var/www/html/report.html --log-format=COMBINED # 实时analysislog goaccess /var/log/nginx/access.log -o /var/www/html/report.html --log-format=COMBINED --real-time-html # analysis压缩 logfile gunzip -c /var/log/nginx/access.log.gz | goaccess -o /var/www/html/report.html --log-format=COMBINED
# installationELK Stack (usingDocker)
docker pull docker.elastic.co/elasticsearch/elasticsearch:7.14.0
docker pull docker.elastic.co/kibana/kibana:7.14.0
docker pull docker.elastic.co/logstash/logstash:7.14.0
# configurationLogstashprocessingNginxlog
# /etc/logstash/conf.d/nginx.conf
input {
file {
path => "/var/log/nginx/access.log"
start_position => "beginning"
sincedb_path => "/dev/null"
}
}
filter {
grok {
match => {
"message" => "%{IPORHOST:remote_addr} - %{DATA:remote_user} \[%{HTTPDATE:time_local}\] \"%{WORD:method} %{URIPATHPARAM:uri} HTTP/%{NUMBER:http_version}\" %{NUMBER:status} %{NUMBER:body_bytes_sent} \"%{DATA:http_referer}\" \"%{DATA:http_user_agent}\""
}
}
date {
match => ["time_local", "dd/MMM/yyyy:HH:mm:ss Z"]
target => "@timestamp"
}
}
output {
elasticsearch {
hosts => ["localhost:9200"]
index => "nginx-access-%{+YYYY.MM.dd}"
}
}
# 启动ELK Stack
docker-compose up -d
# 查看访问量最 high IP
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | head -10
# 查看访问量最 high URL
awk '{print $7}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | head -10
# 查看status码statistics
awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c | sort -nr
# 查看response时间较 long request
awk '{if($NF>1) print $0}' /var/log/nginx/access.log | sort -kNF -nr | head -10
# 查看specificstatus码 request
grep " 404 " /var/log/nginx/access.log | head -20
# 查看specificIP request
grep "192.168.1.100" /var/log/nginx/access.log | head -20
# 查看specific时间段 request
grep "10/Oct/2023:14:" /var/log/nginx/access.log | head -20
随着时间 推移, logfile会变得越来越 big , 占用 big 量 disk空间, 并且会影响loganalysis efficiency. log轮转 is amanagementlogfile method, 它会定期将logfilerename, 压缩 and delete, 以保持logfile 合理 big small .
big many 数Linuxsystem都 in 置了 logrotate tool, 用于managementlogfile 轮转.
# Nginxlog轮转configuration
# /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 nginx nginx
sharedscripts
postrotate
if [ -f /var/run/nginx.pid ]; then
kill -USR1 $(cat /var/run/nginx.pid)
fi
endscript
}
# 手动执行log轮转 sudo logrotate /etc/logrotate.d/nginx # 强制执行log轮转 (即使条件不满足) sudo logrotate -f /etc/logrotate.d/nginx # testlog轮转configuration sudo logrotate -d /etc/logrotate.d/nginx
delaycompress, 确保loganalysistoolable to读取最 new logfile# logsecurityconfiguration
http {
# 设置logfilepermission
# in logrotateconfigurationin设置: create 0640 nginx nginx
# 避免记录敏感information
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# 不记录request体in 敏感information
# 注意: 默认circumstances under , Nginx不会记录request体 in 容
# 限制logfile 访问permission
sudo chmod 0640 /var/log/nginx/*.log
sudo chown nginx:nginx /var/log/nginx/*.log
# encryptionstorelog (such as果需要)
# 可以usingfilesystemencryption or 专门 logencryptiontool
}
# logperformanceoptimization
http {
# 关闭不需要 访问log
location /images/ {
access_log off;
root /var/www/example.com;
}
# using缓冲写入log
access_log /var/log/nginx/access.log main buffer=32k;
# asynchronous写入log (Nginx 1.7.10+)
access_log /var/log/nginx/access.log main buffer=32k flush=1m;
# 合理设置log格式, 避免记录不必要 information
log_format minimal '$remote_addr - [$time_local] "$request" $status $body_bytes_sent';
# for 静态fileusing简化 log格式
location /static/ {
access_log /var/log/nginx/static.access.log minimal;
root /var/www/example.com;
}
}
fake设我们需要 for 一个produceenvironment Nginxserverconfiguration完整 logmanagementsystem, 要求:
# 1. Nginxlogconfiguration
# /etc/nginx/nginx.conf
http {
# 定义详细 log格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'rt=$request_time ut=$upstream_response_time';
# 定义简化 log格式
log_format minimal '$remote_addr - [$time_local] "$request" $status $body_bytes_sent';
# 全局logconfiguration
access_log /var/log/nginx/access.log main buffer=32k flush=1m;
error_log /var/log/nginx/error.log warn;
server {
listen 80;
server_name example.com;
# server级别 logconfiguration
access_log /var/log/nginx/example.access.log main buffer=32k flush=1m;
error_log /var/log/nginx/example.error.log error;
root /var/www/example.com;
index index.html;
# 静态fileusing简化 log格式
location /static/ {
access_log /var/log/nginx/static.access.log minimal buffer=32k flush=1m;
}
# graph片 and graph标关闭访问log
location ~* \.(jpg|jpeg|png|gif|ico|svg)$ {
access_log off;
}
location / {
try_files $uri $uri/ =404;
}
}
}
# 2. log轮转configuration
# /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily
missingok
rotate 30
compress
delaycompress
notifempty
create 0640 nginx nginx
sharedscripts
postrotate
if [ -f /var/run/nginx.pid ]; then
kill -USR1 $(cat /var/run/nginx.pid)
fi
endscript
}
# 3. GoAccessconfiguration
# installationGoAccess
sudo apt update
sudo apt install goaccess
# creationloganalysis脚本
# /usr/local/bin/analyze-nginx-logs.sh
#!/bin/bash
mkdir -p /var/www/html/reports
chown nginx:nginx /var/www/html/reports
goaccess /var/log/nginx/access.log -o /var/www/html/reports/access.html --log-format=COMBINED --real-time-html
# 设置定时task
# /etc/crontab
0 * * * * root /usr/local/bin/analyze-nginx-logs.sh
# 4. logsecurityconfiguration
sudo chmod 0640 /var/log/nginx/*.log
sudo chown nginx:nginx /var/log/nginx/*.log
# 5. monitorlogfile big small
# /usr/local/bin/check-log-size.sh
#!/bin/bash
LOG_DIR="/var/log/nginx"
MAX_SIZE=104857600 # 100MB
for log_file in "$LOG_DIR"/*.log; do
if [ -f "$log_file" ]; then
SIZE=$(stat -c %s "$log_file")
if [ "$SIZE" -gt "$MAX_SIZE" ]; then
echo "Warning: Log file $log_file is too large: $(($SIZE / 1024 / 1024))MB"
fi
fi
done
# 设置定时task
# /etc/crontab
*/30 * * * * root /usr/local/bin/check-log-size.sh
这个configurationpackage含了以 under logmanagement措施:
1. 以 under 哪些 is Nginxlogmanagement important 措施? ( many 选)
2. 以 under 哪个指令用于configuration访问log?
3. 实践练习: configurationNginxlogmanagementsystem
请configuration一个Nginxlogmanagementsystem, 要求:
through本tutorial, 您已经Learning了Nginxlogmanagement coreknowledge, includinglogconfiguration, log格式, loganalysis and log轮转etc.. logmanagement is Nginxservermanagement important 组成部分, 它可以:
in after 续 tutorialin, 我们将深入LearningNginx advanced主题, includingadvancedfunctions and 企业级application, helping您全面MasterNginx usingtechniques.