Nginx日志配置(Ngx_http_log_module)

来源:互联网 发布:plc编程入门视频 编辑:程序博客网 时间:2024/06/05 06:18

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

  • 官方文档:http://nginx.org/en/docs/http/ngx_http_log_module.html

相关指令:(Syntax:使用语法,Default:默认,Context:默认配置段)

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_exceptSyntax:log_format name [escape=default|json] string ...;Default:log_format combined "...";Context:http

  • 指定日志格式:

#示例配置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:页面资源压缩比;
......
  • 缓存所打开的日志文件的元数据:

Syntax:open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
open_log_file_cache off;Default:open_log_file_cache off;Context:http, 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 -r3.10.0-327.el7.x86_64[root@GaoServer ~]# nginx -Vnginx version: nginx/1.10.2......

  • 相关配置/参数:(打开日志缓存加速日志性能)
#在http配置段,定义日志参数;注意,如果在server配置段中也同样定义日志参数,以server配置段中参数生效(最小定义);[root@GaoServer ~]# vim /etc/nginx/nginx.confhttp {......    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.confserver {        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 -pvmkdir: 已创建目录 "/var/log/nginx/nginx_log"[root@GaoServer ~]# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@GaoServer ~]# nginx -s reload#访问测试:[root@GaoServer ~]# curl 127.0.0.1server1[root@GaoServer ~]# curl 127.0.0.1:8080server2......[root@GaoServer ~]# cd /var/log/nginx/nginx_log/[root@GaoServer nginx_log]# lsserver1_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 -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@GaoServer ~]# nginx -s reload




原创粉丝点击