#访问日志
access.log
#错误日志
error.log
#所在位置
[root@web01 conf.d]# ls /var/log/nginx/
access.log access.log-20240522 error.log-20240521.gz error.log-20240523
access.log-20240521.gz error.log error.log-20240522.gz
# /etc/nginx/nginx.conf http标签下
#指定访问日志的格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#访问日志的位置及使用上面定义的以main名字定义的格式记录日志
access_log /var/log/nginx/access.log main;
log_format的使用说明
# 语法格式
Syntax: log_format name [escape=default|json|none] string ...;
# 默认格式
Default: log_format combined "...";
# 在哪个标签下使用
Context: http
参数变量说明(nginx内置变量)
变量名 | 说明 |
$remote_addr | 客户端IP地址 |
$remote_user | 用户名(开启nginx认证模式的用户名) |
$time_local | 时间 |
$request | 请求报文的起始行 |
$status | 状态码 |
$body_bytes_sent | 服务端给用户响应的大小 |
$http_referer | 用户从哪里跳转访问网站的(如果直接访问一般为空) |
$http_user_agent | 用户的客户端(浏览器) |
$http_x_forwarded_for | 使用代理或者负载均衡时用于记录用户真实的IP地址 |
access_log的使用说明
Syntax: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
access_log off;
Default: access_log logs/access.log combined;
Context: http, server, location, if in location, limit_except
# 关闭访问日志
access_log off;
access_log选项
参数 | 说明 |
buffer | 缓冲用户访问日志 |
gzip | 是否要压缩 |
flush | 多久把日志写入到磁盘中 |
if | 添加一些条件,如果条件计算结果为0或空字符串时,则不会记录日志信息 |
官方示例