Simple Nginx 4

ngx_http_core_module模块变量

ngx_http_core_module模块在处理请求时,会有大量的变量,这些变量可以通过访问日志进行记录,也可以用于其他nginx模块.

在我们对请求做策略如改写时等等都会用到一些变量,一下是对这些变量的总结:

参数名称                    注释
$arg_PARAMETER            HTTP 请求中某个参数的值,如/index.<a href="http://www.ttlsa.com/php/" title="php"target="_blank">php</a>?site=www.ttlsa.com,可以用$arg_site取得www.ttlsa.com这个值.
$args HTTP                请求中的完整参数。例如,在请求/index.php?width=400&height=200 中,$args表示字符串width=400&height=200.
$binary_remote_addr        二进制格式的客户端地址。例如:\x0A\xE0B\x0E
$body_bytes_sent        表示在向客户端发送的http响应中,包体部分的字节数
$content_length            表示客户端请求头部中的Content-Length 字段
$content_type            表示客户端请求头部中的Content-Type 字段
$cookie_COOKIE            表示在客户端请求头部中的cookie 字段
$document_root            表示当前请求所使用的root 配置项的值
$uri                    表示当前请求的URI,不带任何参数
$document_uri            与$uri 含义相同
$request_uri            表示客户端发来的原始请求URI,带完整的参数。$uri$document_uri未必是用户的原始请求,在内部重定向后可能是重定向后的URI,而$request_uri 永远不会改变,始终是客户端的原始URI.
$host                    表示客户端请求头部中的Host字段。如果Host字段不存在,则以实际处理的server(虚拟主机)名称代替。如果Host字段中带有端口,如IP:PORT,那么$host是去掉端口的,它的值为IP。$host 是全小写的。这些特性与http_HEADER中的http_host不同,http_host只取出Host头部对应的值。 
$hostname                表示 Nginx所在机器的名称,与 gethostbyname调用返回的值相同      
$http_HEADER            表示当前 HTTP请求中相应头部的值。HEADER名称全小写。例如,示请求中 Host头部对应的值 用 $http_host表示
$sent_http_HEADER        表示返回客户端的 HTTP响应中相应头部的值。HEADER名称全小写。例如,用 $sent_ http_content_type表示响应中 Content-Type头部对应的值      
$is_args                表示请求中的 URI是否带参数,如果带参数,$is_args值为 ?,如果不带参数,则是空字符串      
$limit_rate                表示当前连接的限速是多少,0表示无限速      
$nginx_version            表示当前 Nginx的版本号     
$query_string            请求 URI中的参数,与 $args相同,然而 $query_string是只读的不会改变      
$remote_addr            表示客户端的地址      
$remote_port            表示客户端连接使用的端口      
$remote_user            表示使用 Auth Basic Module时定义的用户名      
$request_filename        表示用户请求中的 URI经过 root或 alias转换后的文件路径      
$request_body            表示 HTTP请求中的包体,该参数只在 proxy_pass或 fastcgi_pass中有意义      
$request_body_file        表示 HTTP请求中的包体存储的临时文件名      
$request_completion        当请求已经全部完成时,其值为 “ok”。若没有完成,就要返回客户端,则其值为空字符串;或者在断点续传等情况下使用 HTTP range访问的并不是文件的最后一块,那么其值也是空字符串。
$request_method            表示 HTTP请求的方法名,如 GET、PUT、POST等      
$scheme                    表示 HTTP scheme,如在请求 https://nginx.com/中表示 https      
$server_addr            表示服务器地址      
$server_name            表示服务器名称      
$server_port            表示服务器端口      
$server_protocol        表示服务器向客户端发送响应的协议,如 HTTP/1.1或 HTTP/1.0

日志配置

Nginx有一个非常灵活的的日志记录模式,每个级别的配置可以使用各自独立的访问日志,日志格式通过log_format命令来定义,ngx_http_log_module是用来定义请求日志格式的.

access_log指令

语法:
access_log path [format [buffer=size [flush=time]]];
access_log path format gzip[=level] [buffer=size] [flush=time];
access_log syslog:server=address[,parameter=value] [format];
access_log off;

默认值: access_log logs/access.log combined;
配置段: http, server, location, if in location, limit_except
配置项: gzip压缩等级,buffer设置内存缓冲区大小,flush保持在缓冲区中的最长时间,以及关闭日志.

使用默认combined格式记录日志:access_log logs/access.log 或 access_log logs/access.log combined;

log_format指令

语法:

log_format name string …;

默认值: log_format combined “…”;
配置段: http

name表示格式名称,string表示等义的格式.log_format有一个默认的无需设置的combined格式,相当于apache的combined格式:

log_format  combined  '$remote_addr - $remote_user  [$time_local]  '
                                   ' "$request"  $status  $body_bytes_sent  '
                                   ' "$http_referer"  "$http_user_agent" ';

如果nginx位于负载均衡器,squid或nginx反向代理之后,即作为web服务器使用,web服务器无法获取到真实的IP地址了, $remote_addr只能获取反向代理的IP地址.而在负载均衡服务器中,可以在转发请求的http头信息中,增加X-Forwarded-For信息,用来记录客户端的真实IP和客户端请求的服务器地址:

log_format  porxy  '$http_x_forwarded_for - $remote_user  [$time_local]  '
                             ' "$request"  $status $body_bytes_sent '
                             ' "$http_referer"  "$http_user_agent" ';

日志格式中允许包含的变量注释如下:

$remote_addr, $http_x_forwarded_for 记录客户端IP地址
$remote_user 记录客户端用户名称
$request 记录请求的URL和HTTP协议
$status 记录请求状态
$body_bytes_sent 发送给客户端的字节数,不包括响应头的大小; 该变量与Apache模块mod_log_config里的“%B”参数兼容。
$bytes_sent 发送给客户端的总字节数。
$connection 连接的序列号。
$connection_requests 当前通过一个连接获得的请求数量。
$msec 日志写入时间。单位为秒,精度是毫秒。
$pipe 如果请求是通过HTTP流水线(pipelined)发送,pipe值为“p”,否则为“.”。
$http_referer 记录从哪个页面链接访问过来的
$http_user_agent 记录客户端浏览器相关信息
$request_length 请求的长度(包括请求行,请求头和请求正文)。
$request_time 请求处理时间,单位为秒,精度毫秒; 从读入客户端的第一个字节开始,直到把最后一个字符发送给客户端后进行日志写入为止。
$time_iso8601 ISO8601标准格式下的本地时间。
$time_local 通用日志格式下的本地时间。

注意: 返回给客户端的响应头拥有”senthttp“前缀,比如$sent_http_content_range.

实例:

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                                        '"$status" $body_bytes_sent "$http_referer" '
                                        '"$http_user_agent" "$http_x_forwarded_for" '
                                        '"$gzip_ratio" $request_time $bytes_sent $request_length';

    log_format srcache_log '$remote_addr - $remote_user [$time_local] "$request" '
                                '"$status" $body_bytes_sent $request_time $bytes_sent $request_length '
                                '[$upstream_response_time] [$srcache_fetch_status] [$srcache_store_status] [$srcache_expire]';

    open_log_file_cache max=1000 inactive=60s;

    server {
        server_name ~^(www\.)?(.+)$;
        access_log logs/$2-access.log main;
        error_log logs/$2-error.log;

        location /srcache {
            access_log logs/access-srcache.log srcache_log;
        }
    }
}

open_log_file_cache指令

语法:

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;

对于每一条日志,都是先打开文件,再写人,然后再关闭,可以通过open_log_file_cache设置日志文件缓存,格式如下:

参数注释:

max:设置缓存中的最大文件描述符数量,如果缓存被占满,采用LRU算法将描述符关闭。
inactive:设置存活时间,默认是10s
min_uses:设置在inactive时间段内,日志文件最少使用多少次后,该日志文件描述符记入缓存中,默认是1valid:设置检查频率,默认60s
off:禁用缓存

配置实例:

open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;

log_not_found指令

语法: log_not_found on | off;
默认值: log_not_found on;
配置段: http, server, location

是否在error_log中记录不存在的错误。默认是

log_subrequest指令

语法: log_subrequest on | off;
默认值: log_subrequest off;
配置段: http, server, location

是否在access_log中记录子请求的访问日志。默认不记录。

rewrite_log指令

由ngx_http_rewrite_module模块提供的。用来记录重写日志的。对于调试重写规则建议开启。 Nginx重写规则指南

语法: rewrite_log on | off;
默认值: rewrite_log off;
配置段: http, server, location, if

启用时将在error log中记录notice级别的重写日志。

error_log指令

语法: error_log file | stderr | syslog:server=address[,parameter=value] [debug | info | notice | warn | error | crit | alert | emerg];
默认值: error_log logs/error.log error;
配置段: main, http, server, location

配置错误日志。

日志切割

logrotate日志管理工具

logrotate是一个日志文件管理工具,负责把日志文件压缩轮转删除,并创建新的日志文件,我们可以根据日志文件的大小时间来进行转存.

配置实例:

/var/log/messages {
    rotate 5
    weekly
    postrotate
        /sbin/killall -HUP syslogd
    endscript
}

"/var/log/httpd/access.log" /var/log/httpd/error.log {
    rotate 5
    mail www@my.org
    size 100k
    sharedscripts
    postrotate
        /sbin/killall -HUP httpd
    endscript
}

/var/log/news/* {
    monthly
    rotate 2
    olddir /var/log/news/old
    missingok
    postrotate
        kill -HUP ‘cat /var/run/inn.pid‘
    endscript
    nocompress
}

配置选项说明:

compress:通过gzip 压缩转储旧的日志
nocompress:不需要压缩时,用这个参数
copytruncate:用于还在打开中的日志文件,把当前日志备份并截断
nocopytruncate:备份日志文件但是不截断
create mode owner group:使用指定的文件模式创建新的日志文件
nocreate:不建立新的日志文件
delaycompress:和 compress 一起使用时,转储的日志文件到下一次转储时才压缩
nodelaycompress:覆盖 delaycompress 选项,转储同时压缩。
errors address:专储时的错误信息发送到指定的Email 地址
ifempty:即使是空文件也转储,这个是 logrotate 的缺省选项。
notifempty:如果是空文件的话,不转储
mail address:把转储的日志文件发送到指定的E-mail 地址
nomail:转储时不发送日志文件
olddir directory:转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
noolddir:转储后的日志文件和当前日志文件放在同一个目录下
prerotate/endscript:在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行
postrotate/endscript:在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行
sharedscripts:所有的日志文件都轮转完毕后统一执行一次脚本
daily:指定转储周期为每天
weekly:指定转储周期为每周
monthly:指定转储周期为每月
rotate count:指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份
size size:当日志文件到达指定的大小时才转储,Size 可以指定 bytes (缺省)以及KB (sizek)或者MB

命令参数说明:

logrotate --help

Usage: logrotate [OPTION...] <configfile>
  -d, --debug               调试模式,输出调试结果,并不执行。隐式-v参数
  -f, --force               强制模式,对所有相关文件进行rotate
  -m, --mail=command        发送邮件 (instead of `/bin/mail')
  -s, --state=statefile     状态文件,对于运行在不同用户情况下有用
  -v, --verbose             显示debug信息

定义日志滚轮策略

vim nginx-log-rotate

/data/weblogs/*.log {
    nocompress
    daily
    copytruncate
    create
    notifempty
    rotate 7
    olddir /data/weblogs/old_log
    missingok
    dateext
    postrotate
        /bin/kill -HUP `cat /var/run/nginx.pid 2> /dev/null` 2> /dev/null || true
    endscript
}

注意:如果使用通配符模式/data/weblogs/*.log,则/data/weblogs/路径下的所有log文件都会被切割,如果需要切割固定的文件,直接指定该文件即可.

设置定时任务

crontab -e
59 23 * * * root ( /usr/sbin/logrotate -f /PATH/TO/nginx-log-rotate)
crontab -l

这样每天23:59:00会定时进行切割.

参考链接