原创文章,转载请指明出处并保留原文url地址
本文主要针对nginx的几个主要基础指令做一个简单的整理,另外部分指令做了实际测试,本文具体包括如下指令:
access_log,log_format,open_log_file_cache。
1. access_log
syntax:
| access_log path [format [buffer=size]];
|
default:
| access_log logs/access.log combined;
|
context:
| http, server, location, if in location, limit_except
|
This directive appeared in version 0.7.7.
设置缓存写入日志的路径、格式、缓存大小。在同一个范围内可以指定数个日志。特殊的off值取消全部日志设置在当前房屋内。如果日志格式没有被至指定则预定义的combined格式被使用。
缓存的尺寸不能超过磁盘原子写入尺寸。对于FreeBSD这个尺寸没有限制
该文件路径可以包含变量(0.7.6+),但这样的日志有一些限制:
所使用工作进程用户凭据应该具有的权限来创建一个目录等。
一般情况nginx每条日志,要先打开文件,然后写入,最后关闭文件,如此循环。为解决这个效率低下的问题,需要开启nginx的open_log_file_cache功能,缓存经常被使用的日志文件描述符。open_log_file_cache 默认为off
Nginx官方原文如下:
Sets the path, format, and buffer size for the buffered log writes. Several logs can be specified on the same level. The special value off cancels all access_log directives on the current level. If format is not specified then the predefined format “combined” is used.
The buffer size must not exceed the size of the atomic write to a disk file. For FreeBSD this size is unlimited.
The file path can contain variables (0.7.6+), but such logs have some constraints:
the user whose credentials are used by worker processes should have permissions to create files in a directory with such logs;
buffered writes do not work;
a file is opened and closed for each log write. However, since the descriptors of frequently used files can be stored in a cache, writes during the time specified by the valid parameter of the open_log_file_cache directive can continue to be made to the old file.
during each log write the existence of the request’s root directory is checked, and if it does not exist the log is not created. It is thus a good idea to specify both root and access_log on the same level:
server {
root /spool/vhost/data/$host;
access_log /spool/vhost/logs/$host;
2. log_format
syntax:
| log_format namestring...;
|
default:
| log_format combined "...";
|
context:
| http
|
定义日志格式
日志格式能包括普通变量,和只存在于日志写入时存在的变量。
$bytes_sent:发送给客户端的字节数
$connection:连接序列号
$connection_requests:通过当前连接的请求数量
$msec:以毫秒为精度的度量日志写入花费的时间
$pipe:如果请求是管道方式则是“p”,否则是“.”
$request_length:请求长度,包括:请求行,请求头,请求body)
$request_time:以毫秒为精度的用户请求处理时间。时间消耗计算从第一个请求字节被接收开始计算,到最后一个相应字节发送给客户端后及日志写入时间。
$status:相应的状态, http状态,包括200,302.。。。。
$ time_iso8601:当地时间ISO8601标准格式
$ time_local:当地时间通用日志格式
Header lines sent to a client have the prefix “sent_http_”, for example, $sent_http_content_range. 没有理解这句的真正含义
预定义combined格式如下
log_format combined '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
Nginx官方解释如下:
Specifies format of a log.
The log format can contain common variables, and variables that exist only at the time of a log write:
$bytes_sent
the number of bytes sent to a client
Since versions 1.3.8 and 1.2.5 there is a common variable with the same name.
$connection
connection serial number
Since versions 1.3.8 and 1.2.5 there is a common variable with the same name.
$connection_requests
the current number of requests made through a connection (1.1.18)
Since versions 1.3.8 and 1.2.5 there is a common variable with the same name.
$msec
time in seconds with a milliseconds resolution at the time of log write
Since versions 1.3.9 and 1.2.6 there is a common variable with the same name.
$pipe
“p” if request was pipelined, “.” otherwise
$request_length
request length (including request line, header, and request body)
$request_time
request processing time in seconds with a milliseconds resolution; time elapsed between the first bytes were read from the client and the log write after the last bytes were sent to the client
Since versions 1.3.9 and 1.2.6 there is a common variable with the same name.
$status
response status
Since versions 1.3.2 and 1.2.2 there is a common variable with the same name.
$time_iso8601
local time in the ISO 8601 standard format
$time_local
local time in the Common Log Format
Header lines sent to a client have the prefix “sent_http_”, for example, $sent_http_content_range.
The configuration always includes the predefined format “combined”:
log_format combined '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
3. open_log_file_cache
syntax:
| open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
|
default:
| open_log_file_cache off;
|
context:
| http, server, location
|
一般情况nginx每条日志,要先打开文件,然后写入,最后关闭文件,如此循环。为解决这个效率低下的问题,需要开启nginx的open_log_file_cache功能,缓存经常被使用的日志文件描述符。open_log_file_cache 默认为off
定义高速缓存存储经常使用的日志文件描述符。
该指令有以下参数:
max:设置在高速缓存中的文件描述符最多数量,如果高速缓存已满,最近最少使用(LRU)算法将不常用描述符清理出去。
Inactive:当文件描述符在指定时间内没有存取操作则关闭,默认时间10秒。
min_uses 如果在inactive时间内,使用超过该参数定义的次数,就会被缓存,默认情况下,1
min_uses:如果在inactive时间内,使用超过该参数定义的次数,就会被缓存
valid: 设置多少时间检查一次,默认时间:60秒
Off:关闭cache
Nginx官方解释如下:
Defines a cache that stores file descriptors of frequently used logs whose names contain variables. The directive has the following parameters:
max
sets a maximum number of descriptors in a cache; if cache becomes full the least recently used (LRU) descriptors are closed
inactive
sets a time after which the cached descriptor is closed if there were no access during this time; by default, 10 seconds
min_uses
sets a minimum number of file uses during the time defined by the inactive parameter after which the descriptor will stay open in a cache; by default, 1
valid
sets a time after which it should be checked that the file still exists with the same name; by default, 60 seconds
off
disables caching
Example usage:
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;