当前位置: 首页 > 工具软件 > ngx_ruby > 使用案例 >

Nginx日志配置(Ngx_http_log_module)

壤驷深
2023-12-01

Ngx_http_log_module:定义日志格式,并且以指定的格式保存

Syntaxaccess_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition];
access_log off;
Defaultaccess_log logs/access.log combined;
Contexthttp, server, location, if in location, limit_except
Syntaxlog_format name [escape=default|json] string …;
Defaultlog_format combined “…”;
Contexthttp
  • 指定日志格式:
#示例配置  
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;
#access_log:访问日志文件路径;
#buffer=32;缓冲在内存中,32K的内存空间,日志先缓冲至内存中,根据时间节点,空间节点在存储至硬盘中(效率更高);

变量定义:多数为Nginx内建变量
$remote_addr:客户端地址;
$remote_user:客户端用户;
[$time_local]:收到用户请求时服务器本地时间;
$reques:请求的url;
$status:响应码;
$bytes_sent:发送客户端的响应报文字节数;
$http_referer:从什么地方跳转到当前页面资源;
$http_user_agent:客户端浏览器的类型;
$gzip_ratio:页面资源压缩比;
……

  • 缓存所打开的日志文件的元数据:
Syntaxopen_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
open_log_file_cache off;
Defaultopen_log_file_cache off;
Contexthttp, server, location

相关选项:
max=N:设置缓存中描述符的最大数量;
inactive=time:设置缓存描述符关闭的时间(定义非活动时长,如果在此期间使用次数小于最小使用次数,定义非活动项目)默认10s;
min_uses=N:在由inactive参数定义的时间内文件使用的最小次数;
简单来讲:在inactive=time时间内,缓存项最少访问min_uses=N次,为活动项,否则为非活动;
valid=time:设置文件检查(每隔多久检查一次缓存项是否有效);
off:禁用缓存;

  • 演示环境:
Server:192.168.47.140
[root@GaoServer ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@GaoServer ~]# uname -r
3.10.0-327.el7.x86_64
[root@GaoServer ~]# nginx -V
nginx version: nginx/1.10.2
......  
  • 相关配置/参数:(打开日志缓存加速日志性能)
#在http配置段,定义日志参数;注意,如果在server配置段中也同样定义日志参数,以server配置段中参数生效(最小定义);  
[root@GaoServer ~]# vim /etc/nginx/nginx.conf  
http {  
......  
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '    #main定义格式  
                      '$status $body_bytes_sent "$http_referer" '  
                      '"$http_user_agent" "$http_x_forwarded_for"';  

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

#定义不同虚拟主机使用不同的访问日志:  
[root@GaoServer ~]# vim /etc/nginx/conf.d/server.conf  
server {  
        listen 80;  
        server_name www.server1.com;  
        access_log /var/log/nginx/nginx_log/server1_access.log main;  
        location / {  
                root /data/nginx/server1;  
        }  
        error_page 404 =200     /404.html;  
        location = /404.html {  
                root /etc/nginx/error_pages/;  
        }  
}  

server {  
        listen 8080;  
        server_name www.server2.com;  
        access_log /var/log/nginx/nginx_log/server2_access.log main;  
        location / {  
                root /data/nginx/server2;  
        }  
}  
#创建日志存放文件上级目录:  
[root@GaoServer]# mkdir /var/log/nginx/nginx_log -pv  
mkdir: 已创建目录 "/var/log/nginx/nginx_log"  
[root@GaoServer ~]# nginx -t  
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok  
nginx: configuration file /etc/nginx/nginx.conf test is successful  
[root@GaoServer ~]# nginx -s reload  

#访问测试:  
[root@GaoServer ~]# curl 127.0.0.1  
server1  
[root@GaoServer ~]# curl 127.0.0.1:8080  
server2  
......  
[root@GaoServer ~]# cd /var/log/nginx/nginx_log/  
[root@GaoServer nginx_log]# ls  
server1_access.log  server2_access.log  
[root@GaoServer nginx_log]# tail server2_access.log   
127.0.0.1 - - [02/Nov/2017:07:17:08 +0800] "GET / HTTP/1.1" 200 8 "-" "curl/7.29.0" "-"  
......  
  • 定义日志缓存:

Usage example:(官方示例)
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;

[root@GaoServer ~]# vim /etc/nginx/nginx.conf
......  
    access_log  /var/log/nginx/access.log  main;
    open_log_file_cache max=100 inactive=20s valid=1m min_uses=2;
......
[root@GaoServer ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@GaoServer ~]# nginx -s reload
 类似资料: