-
2019-06-25 13:40:04
转:https://www.cnblogs.com/Mr-Ding/p/9539867.html
一、nginx访问日志介绍
nginx软件会把每个用户访问网站的日志信息记录到指定的日志文件里,供网站提供者分析用户的浏览行为等,此功能由ngx_http_log_module模块负责,对应的官方地址为:http://nginx.org/en/docs/http/ngx_http_log_module.html.
二、访问日志参数
nginx的访问日志主要有以下2个参数控制
1
2
log_format 用来定义记录日志的格式(可以定义多种日志格式,取不同的名字即可)
access_log 用来指定日志文件的路径及使用何种日志格式记录日志
nginx日志格式中默认的参数配置如下:
1
2
3
log_format main
'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
;
nginx记录日志的默认参数配置如下:
1
access_log logs/access.log main;
三、访问日志配置说明
日志格式的定义说明
语法如下:
1
定义语法 log_format name
string
...;
其配置位置在http标签内。
日志格式说明如下:
1
2
3
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为日志格式关键参数,不能变。
main是为日志格式指定的标签,记录日志时通过这个main标签选择指定的格式。其后所接的所有内容都是可以记录的日志信息,所有的日志段以空格分割,一行可以记录多个。不同列的意义如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$remote_addr 记录访问网站的客户端地址;
$http_x_forwarded_for 当前端有代理服务器时,设置web节点记录客户端地址的配置,此参数生效的前提是代理服务器上也进行了相关的x_forwarded_for设置;
$remote_user 远程客户端用户名称;
$time_local 记录访问时间与时区;
$request 用户的http请求起始行信息;
$status http状态码,记录请求返回的状态,例如200、404、301等;
$body_bytes_sent 服务器发给客户端的响应body字节数;
$http_referer 记录此次请求是从哪个链接访问呢过来的,可以根据referer进行防盗链设置;
$http_user_agent 记录客户端访问信息,例如:浏览器、手机客户端等;
在没有特殊要求的情况下,采用默认的配置即可,更多可以设置的记录日志信息的变量见:http://nginx.org/en/docs/http/ngx_http_log_module.html
记录日志的access_log参数说明
下面是有关access_log参数的官方说明:
语法如下:
1
2
3
4
5
access_log path [format [buffer=size] [gzip[=level]] [flush=time] [
if
=condition]];
access_log path format gzip[=level] [buffer=size] [flush=time] [
if
=condition];
access_log syslog:server=address[,parameter=value] [format [
if
=condition]];
buffer=size 为存放日志的缓冲区大小,flush=time 为将缓冲区的日志刷到磁盘的时间,gzip[=level] 表示压缩级别,[if=condition] 表示其他条件,一般场景这些参数都无需配置,极端优化时才可能考虑这些参数。
access_log off中的off,表示不记录访问日志
默认配置:access_log logs/access.log combined;
放置位置在:http, server, location, if in location, limit_except中。
四、访问日志配置实战
编辑主配置文件nginx.conf,配置日志的格式如下:
1
2
3
4
[root@nginx conf]# sed -n
'21,23 s/#//gp'
nginx.conf.
default
log_format main
'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
;
把上述内容放到nginx.conf 的 http 标签的首部,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@nginx conf]# cat nginx.conf
worker_processes 1;
error_log logs/error.log error;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main
'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
;
sendfile
on
;
keepalive_timeout 65;
include extra/dmtest1.conf;
include extra/dmtest2.conf;
include extra/dmtest3.conf;
}
然后在每个虚拟主机里进行配置,使其使用上述格式记录用户访问日志。以 www.dmtest1.com 站点为例,命令如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@nginx conf]# cat extra/dmtest1.conf
server {
listen 80;
server_name www.dmtest1.com dmtest1.com;
location / {
root html/dmtest1;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
access_log logs/access_dmtest1.log main;
}
如果不指定日志格式就会采用默认的combined格式记录日志。
检查语法重新加载配置文件:
1
2
3
4
5
[root@nginx conf]# ../sbin/nginx -t
nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax
is
ok
nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test
is
successful
[root@nginx conf]# systemctl reload nginx
用浏览器模拟用户访问生成日志,在服务器上查看日志结果,如下:
1
2
3
4
5
6
7
8
[root@nginx conf]# curl www.dmtest1.com
www.dmtest1.com
[root@nginx conf]# ls -l ../logs/access_dmtest1.log
-rw-r--r-- 1 root root 95 8月 27 06:24 ../logs/access_dmtest1.log
[root@nginx conf]# tail -1 ../logs/access_dmtest1.log
192.168.200.102 - - [27/Aug/2018:06:24:36 +0800]
"GET / HTTP/1.1"
200 16
"-"
"curl/7.29.0"
"-"
使用谷歌浏览器访问的日志结果如下:
1
2
[root@nginx conf]# tail -10 ../logs/access_dmtest1.log
192.168.200.1 - - [27/Aug/2018:06:36:58 +0800]
"GET / HTTP/1.1"
200 16
"-"
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
"-"
日志格式和日志内容对应说明如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$remote_addr 对应的是真实日志里的192.168.200.1,即客户端的IP。
$remote_user 对应的是第二个中杠
"-"
,没有远程用户,所以用
"-"
填充。
[$time_local 对应的是[27/Aug/2018:06:36:58 +0800]。
"$request"
对应的是
"GET/HTTP/1.1"
。
$tatus 对应的是200状态码,200表示正常访问。
$body_bytes_sent 对应的是16字节,即响应body的大小。
"$http_referer"
对应的是
"-"
,由于是直接打开域名浏览的,因此,referer没有值。
$http_user_agent 对应的是
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
"$http_x_forwarded_for"
对应的是
"-"
,因为web服务没有使用代理,因此此处为
"-"
下面针对日志配置进行深入说明。
可以在记录日志参数中加上buffer和flush选项,这样可以在高并发场景下提升网站访问性能。加该选项的命令如下:
1
2
3
4
5
6
7
access_log path [format [buffer=size] [gzip[=level]] [flush=time] [
if
=condition]];
access_log path format gzip[=level] [buffer=size] [flush=time] [
if
=condition];
access_log syslog:server=address[,parameter=value] [format [
if
=condition]];
access_log off;
具体配置如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@nginx conf]# cat extra/dmtest1.conf
server {
listen 80;
server_name www.dmtest1.com dmtest1.com;
location / {
root html/dmtest1;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#access_log logs/access_dmtest1.log main;
access_log logs/access_dmtest1.log main gzip buffer=32k flush=5s;
#access_log off;
}
五、nginx访问日志轮训切割
默认情况下nginx会把所有的访问日志生成到一个指定的访问日志文件access_log里,但这样一来,时间长了就会导致日志文件很大,不利于日志的分析和处理,因此,有必要对nginx日志按天或小时进行切割,使其分成不同的文件保存。这里使用按天切割的方法。
具体切割脚本如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@nginx shell]# cat cut_nginx_log.sh
#!/bin/bash
#Author:Mr.Ding
#Created Time:2018-08-27 07:19:30
#Name:cut_nginx_log.sh
#Description:
Dateformat=`date +%Y%m%d`
Basedir=
"/application/nginx"
Nginxlogdir=
"$Basedir/logs"
Logname=
"access_dmtest1"
[ -d $Nginxlogdir ] && cd $Nginxlogdir || exit 1
[ -f ${Logname}.log ] || exit 1
/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log
#$Basedir/sbin/nginx -s reload
systemctl reload nginx
脚本实现切割nginx的思想为将正在写入的nginx日志(access_dmtest1.log)改名为带日期的格式文件(20180827_access_dmtest1.log),然后平滑重新加载nginx,生成新的nginx日志(access_dmtest1.log)。
把脚本加入计划任务:
1
2
3
4
5
6
7
8
9
[root@nginx shell]# cat >>/
var
/spool/cron/root << EOF
> #cut ngixn access log
by
dm 2018-8-27
> 00 00 * * * /bin/sh /server/scripts/shell/cut_nginx_log.sh >/dev/
null
2&1
> EOF
[root@nginx shell]# crontab -l
#time sync by dm at 2018-8-20
*/5 * * * * /usr/sbin/ntpdate -u ntp.api.bz >/dev/
null
2>$1
#cut ngixn access log by dm 2018-8-27
00 00 * * * /bin/sh /server/scripts/shell/cut_nginx_log.sh >/dev/
null
2&1
最终日志切割效果如下:
1
2
3
4
5
6
7
[root@nginx logs]# ll
总用量 32
-rw-r--r-- 1 root root 0 8月 27 07:35 20180827_access_dmtest1.log
-rw-r--r-- 1 root root 0 8月 27 07:35 access_dmtest1.log
-rw-r--r-- 1 root root 14076 8月 27 04:41 access.log
-rw-r--r-- 1 root root 10098 8月 27 06:36 error.log
-rw-r--r-- 1 root root 5 8月 26 21:56 nginx.pid
更多相关内容 -
nginx服务器中access_log日志分析与配置详解
2020-09-09 12:00:59通过访问日志,可以知晓用户的地址,网站的哪些部分最受欢迎,用户的浏览时间,对大多数用户用的的...下面这篇文章主要给大家介绍了关于nginx服务器中access_log日志分析与配置的相关资料,需要的朋友可以参考下。 -
access_log日志分析
2018-01-16 20:29:32apache的默认日志文件分析,用于IP统计访问量,查看某一时间段的ip连接数 -
access_log.sql
2021-05-21 17:13:54Mysql导入数据表access_log,在Mysqlworkbench里对表进行查询操作 -
Nginx访问日志(access_log)配置及信息详解
2020-12-03 19:33:44log_format #用来定义记录日志的格式(可以定义多种日志格式,取不同名字即可) access_log #用来指定日至文件的路径及使用的何种日志格式记录日志 # log_format main '$remote_addr - $remote_user [$time_local] ...Nginx访问日志主要有两个参数控制:
log_format #用来定义记录日志的格式(可以定义多种日志格式,取不同名字即可)
access_log #用来指定日至文件的路径及使用的何种日志格式记录日志# log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"';
access_log的默认值:
#access_log logs/access.log main;
log_format
log_format语法格式及参数语法说明如下:
log_format <NAME> <String>; 关键字 格式标签 日志格式 关键字:其中关键字error_log不能改变 格式标签:格式标签是给一套日志格式设置一个独特的名字 日志格式:给日志设置格式 作用域 : http
eg:
log_format compression '$remote_addr - $remote_user [$time_local] ' '"$request" $status $bytes_sent ' '"$http_referer" "$http_user_agent" "$gzip_ratio"'; access_log /spool/logs/nginx-access.log compression buffer=32k;
log_format格式变量:
参数 说明 示例 $remote_addr 客户端地址 211.28.65.253 $remote_user 客户端用户名称 -- $time_local 访问时间和时区 18/Jul/2012:17:00:01 +0800 $request 请求的URI和HTTP协议 "GET /article-10000.html HTTP/1.1" $http_host 请求地址,即浏览器中你输入的地址(IP或域名) www.wang.com 192.168.100.100 $status HTTP请求状态 200 $upstream_status upstream状态 200 $body_bytes_sent 发送给客户端文件内容大小 1547 $http_referer url跳转来源 https://www.baidu.com/ $http_user_agent 用户终端浏览器等信息 "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SV1; GTB7.0; .NET4.0C; $ssl_protocol SSL协议版本 TLSv1 $ssl_cipher 交换数据中的算法 RC4-SHA $upstream_addr 后台upstream的地址,即真正提供服务的主机地址 10.10.10.100:80 $request_time 整个请求的总时间 0.205 $upstream_response_time 请求过程中,upstream响应时间 0.002
access_log
语法格式及参数语法说明如下:
access_log <FILE> <NAME>; 关键字 日志文件 格式标签 关键字:其中关键字error_log不能改变 日志文件:可以指定任意存放日志的目录 格式标签:给日志文件套用指定的日志格式 其他语法: access_log off; #关闭access_log,即不记录访问日志 access_log path [format [buffer=size [flush=time]] [if=condition]]; access_log path format gzip[=level] [buffer=size] [flush=time] [if=condition]; access_log syslog:server=address[,parameter=value] [format [if=condition]]; 说明: buffer=size #为存放访问日志的缓冲区大小 flush=time #为缓冲区的日志刷到磁盘的时间 gzip[=level] #表示压缩级别 [if = condition] #表示其他条件 作用域(参数的标签段位置) : http, server, location, if in location, limit_except
eg:
access_log /spool/logs/nginx-access.log compression buffer=32k;
open_log_file_cache
使用open_log_file_cache来设置日志文件缓存(默认是off)。
max:设置缓存中的最大文件描述符数量,如果缓存被占满,采用LRU算法将描述符关闭。
inactive:设置存活时间,默认是10s
min_uses:设置在inactive时间段内,日志文件最少使用多少次后,该日志文件描述符记入缓存中,默认是1次
valid:设置检查频率,默认60s
off:禁用缓存语法格式: open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time]; open_log_file_cache off; 默认值: open_log_file_cache off; 作用域: http, server, location
eg:
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;
参考资料:http://nginx.org/en/docs/http/ngx_http_log_module.html
nginx日志调试技巧
设置 Nginx 仅记录来自于你的 IP 的错误
当你设置日志级别成 debug,如果你在调试一个在线的高流量网站的话,你的错误日志可能会记录每个请求的很多消息,这样会变得毫无意义。
在events{…}中配置如下内容,可以使 Nginx 记录仅仅来自于你的 IP 的错误日志。
events { debug_connection 1.2.3.4; }
调试 nginx rewrite 规则
调试rewrite规则时,如果规则写错只会看见一个404页面,可以在配置文件中开启nginx rewrite日志,进行调试。
server { error_log /var/logs/nginx/example.com.error.log; rewrite_log on; }
rewrite_log on; 开启后,它将发送所有的 rewrite 相关的日志信息到 error_log 文件中,使用 [notice] 级别。随后就可以在error_log 查看rewrite信息了。
使用location记录指定URL的日志
server { error_log /var/logs/nginx/example.com.error.log; location /static/ { error_log /var/logs/nginx/static-error.log debug; } }
Nginx配置访问日志过程:
(1)创建log_format语句
worker_processes 1; error_log logs/error.log error; events { worker_connections 1024; } http { include status.conf; include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; server { listen 80; server_name localhost; rewrite ^/.* http://www.wl.com permanent; } include vhost/*.conf; }
(2)插入access_log语句
server { access_log /data/log/www; listen 80; server_name abc.com www.wl.com; location / { root /data/www/www; index index.html index.htm; } error_log logs/error_www.wl.com.log error; access_log logs/access_www.wl.com.log main; #新增内容↑ }
(3)重启服务
nginx -t nginx -s reload
常用例子
main格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"' '$upstream_addr $upstream_response_time $request_time '; access_log logs/access.log main;
json格式
log_format logstash_json '{"@timestamp":"$time_iso8601",' '"host": "$server_addr",' '"client": "$remote_addr",' '"size": $body_bytes_sent,' '"responsetime": $request_time,' '"domain": "$host",' '"url":"$request_uri",' '"referer": "$http_referer",' '"agent": "$http_user_agent",' '"status":"$status",' '"x_forwarded_for":"$http_x_forwarded_for"}';
解释:
u r i 请 求 中 的 当 前 U R I ( 不 带 请 求 参 数 , 参 数 位 于 uri 请求中的当前URI(不带请求参数,参数位于 uri请求中的当前URI(不带请求参数,参数位于args),不同于浏览器传递的$request_uri的值,它可以通过内部重定向,或者使用index指令进行修改。不包括协议和主机名,例如/foo/bar.html。
r e q u e s t u r i 这 个 变 量 等 于 包 含 一 些 客 户 端 请 求 参 数 的 原 始 U R I , 它 无 法 修 改 , 请 查 看 request_uri 这个变量等于包含一些客户端请求参数的原始URI,它无法修改,请查看 requesturi这个变量等于包含一些客户端请求参数的原始URI,它无法修改,请查看uri更改或重写URI。
也就是说: r e q u e s t u r i 是 原 始 请 求 U R L , request_uri是原始请求URL, requesturi是原始请求URL,uri则是经过nginx处理请求后剔除参数的URL,所以会将汉字表现为union。
坑点:
使用 u r i 可 以 在 n g i n x 对 U R L 进 行 更 改 或 重 写 , 但 是 用 于 日 志 输 出 可 以 使 用 uri 可以在nginx对URL进行更改或重写,但是用于日志输出可以使用 uri可以在nginx对URL进行更改或重写,但是用于日志输出可以使用request_uri代替,如无特殊业务需求,完全可以替换。压缩格式
日志中增加了压缩的信息。
http { log_format compression '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent" "$gzip_ratio"'; server { gzip on; access_log /spool/logs/nginx-access.log compression; ... } }
upstream格式
增加upstream消耗的时间。
http { log_format upstream_time '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent"' 'rt=$request_time uct="$upstream_connect_time" uht="$upstream_header_time" urt="$upstream_response_time"'; server { access_log /spool/logs/nginx-access.log upstream_time; ... } }
统计信息
统计status 出现的次数
awk '{print $9}' access.log | sort | uniq -c | sort -rn 36461 200 483 500 87 404 9 400 3 302 1 499 1 403 1 301
显示返回302状态码的URL。
awk '($9 ~ /302/)' access.log | awk '{print $7}' | sort | uniq -c | sort -rn 1 /wp-login.php 1 /wp-admin/plugins.php?action=activate&plugin=ewww-image-optimizer%2Fewww-image-optimizer.php&_wpnonce=cc4a379131 1 /wp-admin/
-
Nginx的error_log和Access_log分析.docx
2021-09-26 23:54:37Nginx的error_log和Access_log分析.docx -
Linux服务器access_log日志分析及配置详解(二)
2021-05-12 09:51:36默认nginx / Linux日志在哪个文件夹?一般在 xxx.xxx.xxxx.com/home/admin 路径下面的error.log... #错误日志access_log logs/access.log; #访问日志1. access_log 访问日志access_log为访问日志,记录所有对apache服...默认nginx / Linux日志在哪个文件夹?
一般在 xxx.xxx.xxxx.com/home/admin 路径下面的error.log文件和access.log文件
error_log logs/error.log; #错误日志
access_log logs/access.log; #访问日志
1. access_log 访问日志
access_log为访问日志,记录所有对apache服务器进行请求的访问,它的位置和内容由CustomLog指令控制,LogFormat指令可以用来简化该日志的内容和格式
2. error_log 错误日志
error_log为错误日志,记录下任何错误的处理请求,它的位置和内容由ErrorLog指令控制,通常服务器出现什么错误,首先对它进行查阅,是一个最重要的日志文件。
通过apache配置文件,找到日志存放地址:
find / -name "httpd.conf"
找到配置文件地址,打开它,在里边找到apache的【访问日志】与【错误日志】存放地址
sudo vi /private/etc/apache2/httpd.conf
我的存放地址分别在:
/private/var/log/apache2/error_log
/private/var/log/apache2/access_log
一。查看apache错误日志
tail -f -30 "/private/var/log/apache2/error_log"
[Fri Jan 13 14:32:52 2017] [error] [client 127.0.0.1] client denied by server configuration: /export/home/live/ap/htdocs/test
第一项是错误发生的日期和时间;
第二项是错误的严重性,LogLevel指令使只有高于指定严重性级别的错误才会被记录;
第三项是导致错误的IP地址;
此后是信息本身,在此例中,服务器拒绝了这个客户的访问。服务器在记录被访问文件时,用的是文件系统路径,而不是Web路径。
错误日志中会包含类似上述例子的多种类型的信息。此外,CGI脚本中任何输出到stderr(标准错误)的信息会作为调试信息原封不动地记录到错误日志中。
二。同样的,在apache配置文件里找到access_log存放地址
tail -f -30 "/private/var/log/apache2/access_log"
看一条典型的access_log的日志记录:
61.155.149.20 - - [13/Jan/2017:15:42:47 +0800] "GET /category/db/ HTTP/1.1" 200 23225
1).61.155.149.20
这是一个请求到apache服务器的客户端ip,默认的情况下,第一项信息只是远程主机的ip地址,但我们如果需要apache查出主机的名字,可以将 HostnameLookups设置为on,不推荐使用,会大大降低网站速度。
2). -
这一项是空白,使用"-"来代替,用于记录浏览者的标识,对于大多数浏览器,这项都是空。
3). -
也为空,记录浏览者进行身份验证时提供的名字,大多数这项也为空。
4). [13/Jan/2017:15:42:47 +0800]
第四项是记录请求的时间,格式为[day/month/year:hour:minute:second zone],最后的+0800表示服务器所处的时区为东八区
5). "GET /category/db/ HTTP/1.1"
这一项最有用,首先,它告诉我们的服务器收到的是一个GET请求,其次,是客户端请求的资源路径,第三,客户端使用的协议时HTTP/1.1,整个格式为"%m %U%q %H",即"请求方法/访问路径/协议"
6). 200
这是一个状态码,由服务器端发送回客户端,它告诉我们客户端的请求是否成功,或者是重定向,或者是碰到了什么样的错误,这项值为200,表示服务器已经成 功的响应了客户端的请求,一般来说,这项值以2开头的表示请求成功,以3开头的表示重定向,以4开头的标示客户端存在某些的错误,以5开头的标示服务器端
存在某些错误。
7).23225
这项表示服务器向客户端发送了多少的字节,在日志分析统计的时侯,把这些字节加起来就可以得知服务器在某点时间内总的发送数据量是多少
本文出处:http://blog.csdn.net/ty_hf/article/details/55504719
原文:http://www.cnblogs.com/111testing/p/7119137.html
-
Nginx配置日志(error_log/access_log)
2020-12-01 21:18:06Nginx中的日志分两种,一种是error_log,一种是access_log。 “error_log”名字虽然叫error,但它可以配置成任意级别,默认级别是error,用来记录Nginx运行期间的处理流程相关的信息; “access_log”指的是“访问...Nginx中的日志分两种,一种是error_log,一种是access_log。
“error_log”名字虽然叫error,但它可以配置成任意级别,默认级别是error,用来记录Nginx运行期间的处理流程相关的信息;
“access_log”指的是“访问日志”,用来记录服务器的接入信息(包括记录用户的IP、请求处理时间、浏览器信息等)。
1. error_log:
error_log是Nginx中用来配置日志打印的指令,在Nginx源码中的 “ngx_http_core_module.c” 文件中,“ngx_http_core_commands” 中就有 “error_log” 这个指令:
Nginx的error_log分级如下:
级别越高信息越少,默认error。
可以配置在main、http、server、location里面,按需要的模块进行打印。
如何配置error_log:
# 举例: error_log /var/log/nginx/error.log crit # 格式: error_log [log文件存储路径] [日志级别]
如果想彻底关闭error_log:
error_log /dev/null;
使用举例:
http { server { location / { root /usr/local/nginx/html/; } error_log /usr/local/nginx/my_log.log warn; } }
对某一个IP的用户输出debug级别的日志,其他终端仍按error_log中的配置进行日志打印:
# 举例: events { debug_connections 127.0.0.1; } # 格式: debug_connections IP; debug_connections CIDR;
这个配置必须放在
events{}
下面,因为这是处理客户连接相关的配置。2. access_log:
如何配置access_log:
access_log [path] [format [buffer=size] [gzip=level] [flush=time] [if=confition] ]; access_log off; #关闭日志
其中,path用于指定log的存放路径;
format用于配置log格式:buffer指示日志的缓存大小,默认64k;gzip指示压缩比,从1到9,数字越大压缩比越大,压缩速度也越慢,默认为1;flush设置清空缓存的时间;if为日志写入判断条件,为0或为空语句则不写入。使用举例:
access_log /var/logs/my_access.log
-
Linux运维Nginx访问日志(access_log)配置实战
2020-02-20 09:41:03Nginx软件会把每个用户访问网站的日志信息记录到指定的日志文件里,供网站提供者分析用户的浏览行为等,此功能由ngx_http_log_module模块负责。对应的官方地址为:... -
localhost_access_log.*.txt文件含义及内容配置
2019-02-19 15:25:51官方文档上说了:This MUST be set to org.apache.catalina.valves.AccessLogValve to use the default access log valve。 directory 日志文件存放的目录。通常设置为tomcat下已有的那个logs文件。 ... -
nginx access_log日志
2019-08-14 20:07:06access_log:用来指定日志文件的存放路径、格式(把定义的log_format 跟在后面)和缓存大小;如果不想启用日志则access_log off ; log_format 日志格式 语法: log_format name(格式名字) 格式样式(即想要得到... -
Tomcat的访问日志-localhost_access_log和记录Post请求参数
2017-11-07 20:10:22tomcat的日志分类 ... tomcat产生的访问日志数据 【localhost_access_log.Y-M-D.txt】 它记录的访问的时间,IP,访问的资料等相关信息 首先是配置tomcat访问日志数据,配置的方式如下 ... -
nacos access_log占用磁盘空间,如何关闭?
2019-09-09 14:12:16找到nacos的conf目录下的application.properties文件,将server.tomcat.accesslog.enabled设置为false即可,重启nacos。 server.tomcat.accesslog.enabled=false server.tomcat.accesslog.pattern=%h %l %u %t "%r... -
tomcat的localhost_access_log日志文件
2020-01-08 10:18:04tomcat的logs目录下除了有catalina.out日志文件外,还有个localhost_access_log.yyyy-MM-dd.txt的日志文件。生成这个文件的配置在tomcat的安装目录conf文件夹下server.xml里配置。 这个日志文件可记录所有http... -
nginx服务器access_log日志详解
2019-07-22 11:28:41前言: nginx的log日志分为:access log 和 error log 其中access log 记录了哪些用户,哪些页面以及用户...log_format name(格式名字) 格式样式(即想要得到什么样的日志内容) 示例: log_format main '$re... -
apache 访问日志access_log 配置和解析 rotatelogs分割日志
2017-10-15 13:27:06一、解析访问日志 apache 的访问日志记载着大量的信息,学会高效快捷的读出其中关键信息对我们的工作有极大帮助。 如果Apache的安装方式... 这两个文件是 access_log(在Windows上是access.log) error_log -
Apache服务器access_log日志详解
2018-05-22 11:29:21访问日志中会记录服务器...通用日志格式(Common Log Format)组合日志格式(Combined Log Format)多文件访问日志(条件日志此处不作介绍)Common Log Format:LogFormat "%h %l %u %t "%r" %>s ... -
Tomcat access_log
2019-03-24 20:50:40Tomcat access_log 统计接口平均相应时间 配置:Tomcat {tomcat_path}/conf/server.xml 中关于 access_log 的输出有一端输出配置1 其中: %T 处理响应的时间,以秒为单位 %D 处理响应的时间,以毫秒为单位 统计... -
tomcat的localhost_access_log日志格式含义
2018-12-17 15:58:28 -
nginx服务器access_log日志分析及配置详解
2017-09-18 16:04:10nginx的access log日志 -
根据nginx的access_log查看接口请求时间
2020-12-24 15:33:43首先修改修改生成日志的格式,在nginx配置文件的http里添加如下内容:log_format'$remote_addr-$remote_user[$time_local]"$request"''$status$body_bytes_sent$request_body"$http_referer"''"$http_user_agent""$... -
nginx关闭日志功能access_log关闭
2019-07-29 11:56:00网上一堆错误示例,我就不吐槽了,未经验证...access_log on; 以上这些会产生名字为 off/on 的日志文件.... 正确关闭方式: access_log off; error_log /dev/null; 转载于:https://www.cnblogs.com/jonnyan/p/1126... -
OpenResty 自定义 access_log 格式
2017-01-12 01:25:29定义access log的format是 Nginx已经提供的功能,有了 ngx_lua 之后就可以更灵活的记录请求相关的信息,而不仅仅拘泥于 Nginx的内置变量了,可以自定义一些格式和变量来存储结构化的数据,这样做离线的统计更加方面... -
access_log分析
2017-12-21 10:48:57cat access_log | grep "19/May/2011:00" | grep "61.135.166.230" | awk '{print $7}' | sort | uniq -c | sort -nr | head -n 10 5,当天访问页面排前10的url: cat access_log | grep "19/May/...