nginx / Web Service

15.7 nginx:日志模块(ngx_http_log_module module)

 

日志模块:The < ngx_http_log_module module >writes request logs in the specified format.
======================================================================================

log_format:

Syntax:		log_format name [escape=default|json|none] string ...;
Default:	log_format combined "...";
Context:	http

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
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;
插曲,centos 7 更改地区区域:
~]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
192.168.206.1 - - [10/Jun/2018:11:48:14 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0" "-"
192.168.206.1 - - [10/Jun/2018:11:48:14 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0" "-"
192.168.206.1 - - [10/Jun/2018:11:48:32 +0800] "GET /basic_status HTTP/1.1" 200 98 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0" "-"
192.168.206.1 - - [10/Jun/2018:11:48:32 +0800] "GET /basic_status HTTP/1.1" 200 98 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0" "-"

log_format参数说明:

$remote_addr    客户端地址
$remote_user    客户端用户名称
$time_local       访问时间和时区
$request            请求的URI和HTTP协议

$status HTTP               请求状态
$body_bytes_sent      发送给客户端文件内容大小
$http_referer url        跳转来源

$http_user_agent      用户终端浏览器等信息

$ssl_protocol SSL           协议版本
$ssl_cipher                      交换数据中的算法

$upstream_addr           后台upstream的地址,即真正提供服务的主机地址
$upstream_status                      upstream状态
$upstream_response_time            请求过程中,upstream响应时间

$http_host                           请求地址,即浏览器中你输入的地址(IP或域名)
$request_time                    整个请求的总时间

 

nginx日志切割:
———————–

#!/bin/bash  
#Rotate the Nginx logs to prevent a single logfile from consuming too much disk space. 

nginx_log_path=/var/log/nginx
date_yesterday=$(date -d "yesterday" +%Y-%m-%d)  

mv ${nginx_log_path}/access.log  ${nginx_log_path}/access_${date_yesterday}.log  
mv ${nginx_log_path}/error.log  ${nginx_log_path}/error_${date_yesterday}.log  

## 向 Nginx 主进程发送 USR1 信号; 重新打开日志文件;  
kill -USR1 $(cat /var/run/nginx.pid)
0 0 * * * /usr/bin/bash /var/log/nginx/ngx_log_rotate.sh  > /dev/null

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    /path/to/log.gz    combined   gzip   flush=5m;

open_log_file_cache:

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: 缓存的最大文件描述符数量;
min_uses: 在”inactive”指定的时长内访问大于等于此值时才被当做活动项;
inactive: 非活动时长;
valid: 验证缓存中各缓存项是否为活动项的时间间隔;

open_log_file_cache   max=1000  inactive=20s  valid=1m  min_uses=2;
缓存最大文件描述符数量为1000个,每分钟检查一次,依据LRU(Least  recently  used,最近最少使用)算法在20秒内至少要被访问2次以上,否则就被关闭;

Leave a Reply

Your email address will not be published. Required fields are marked *