Skip to content

访问日志配置 ​

配置访问日志可以记录用户访问信息,用于分析和监控。

基本配置 ​

默认日志格式 ​

nginx
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for"';

启用访问日志 ​

nginx
access_log /var/log/nginx/access.log main;

完整配置 ​

生产环境配置 ​

nginx
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for" '
                '$request_time $upstream_response_time';

server {
    listen 80;
    server_name www.example.com;

    access_log /var/log/nginx/access.log main;

    root /var/www/html;
}

日志变量 ​

常用变量 ​

$remote_addr

  • 客户端IP地址

$remote_user

  • 认证用户名

$time_local

  • 本地时间

$request

  • 完整请求

$status

  • 响应状态码

$body_bytes_sent

  • 发送的字节数

$http_referer

  • 来源页面

$http_user_agent

  • 用户代理

$request_time

  • 请求处理时间

$upstream_response_time

  • 上游响应时间

自定义日志格式 ​

简洁格式 ​

nginx
log_format simple '$remote_addr - $request - $status';

详细格式 ​

nginx
log_format detailed '$remote_addr - $remote_user [$time_local] "$request" '
                   '$status $body_bytes_sent "$http_referer" '
                   '"$http_user_agent" "$http_x_forwarded_for" '
                   '$request_time $upstream_response_time '
                   '$upstream_addr $upstream_status';

JSON格式 ​

nginx
log_format json escape=json '{'
    '"time": "$time_iso8601",'
    '"remote_addr": "$remote_addr",'
    '"remote_user": "$remote_user",'
    '"request": "$request",'
    '"status": $status,'
    '"body_bytes_sent": $body_bytes_sent,'
    '"request_time": $request_time,'
    '"upstream_response_time": "$upstream_response_time"'
'}';

条件日志 ​

基于状态码 ​

nginx
map $status $loggable {
    ~^[23]  0;
    default 1;
}

access_log /var/log/nginx/access.log main if=$loggable;

基于URL ​

nginx
map $request_uri $loggable {
    ~* \.(jpg|jpeg|png|gif|css|js)$  0;
    default 1;
}

access_log /var/log/nginx/access.log main if=$loggable;

基于IP ​

nginx
map $remote_addr $loggable {
    192.168.1.0/24  0;
    default 1;
}

access_log /var/log/nginx/access.log main if=$loggable;

多日志文件 ​

主日志 ​

nginx
access_log /var/log/nginx/access.log main;

错误日志 ​

nginx
access_log /var/log/nginx/error.log main;

API日志 ​

nginx
access_log /var/log/nginx/api.log main;

完整示例 ​

生产环境配置 ​

nginx
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for" '
                '$request_time $upstream_response_time';

log_format json escape=json '{'
    '"time": "$time_iso8601",'
    '"remote_addr": "$remote_addr",'
    '"remote_user": "$remote_user",'
    '"request": "$request",'
    '"status": $status,'
    '"body_bytes_sent": $body_bytes_sent,'
    '"request_time": $request_time,'
    '"upstream_response_time": "$upstream_response_time"'
'}';

map $request_uri $loggable {
    ~* \.(jpg|jpeg|png|gif|css|js)$  0;
    default 1;
}

server {
    listen 80;
    server_name www.example.com;

    access_log /var/log/nginx/access.log main if=$loggable;
    access_log /var/log/nginx/access.json json;

    root /var/www/html;
}

日志分析 ​

统计访问量 ​

bash
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr

统计状态码 ​

bash
awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c | sort -nr

统计访问URL ​

bash
awk '{print $7}' /var/log/nginx/access.log | sort | uniq -c | sort -nr

常见问题 ​

日志文件过大 ​

原因: 日志记录过多

解决: 使用条件日志或日志轮转

nginx
map $request_uri $loggable {
    ~* \.(jpg|jpeg|png|gif|css|js)$  0;
    default 1;
}

access_log /var/log/nginx/access.log main if=$loggable;

日志格式错误 ​

原因: 日志格式定义错误

解决: 检查日志格式定义

nginx
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for"';

总结 ​

访问日志配置的关键点:

  • log_format:定义日志格式
  • access_log:启用访问日志
  • 日志变量:使用变量记录详细信息
  • 自定义格式:创建自定义日志格式
  • 条件日志:基于条件记录日志
  • 日志分析:分析日志获取有用信息

合理配置访问日志,记录用户访问信息,用于分析和监控。