Nginx的error_log和Access_log分析 nginx access log( 二 )


ngx_http_core_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_core_loc_conf_t *prev = parent;
ngx_http_core_loc_conf_t *conf = child;
。。。。。。
if (conf->error_log == NULL) {
if (prev->error_log) {
conf->error_log = prev->error_log;
} else {
conf->error_log = &cf->cycle->new_log;
}
}
。。。。。。
}
那为什么不把两个指令合并到一起呢 。
首先看一下模块注册顺序:
ngx_module_t *ngx_modules[] = {
&ngx_core_module,
&ngx_errlog_module,
&ngx_conf_module,
&ngx_events_module,
&ngx_event_core_module,
&ngx_rtsig_module,
&ngx_epoll_module,
&ngx_regex_module,
&ngx_http_module,
&ngx_http_core_module,
&ngx_http_log_module,
......
}
可见ngx_errlog_module 和 ngx_http_core_module中间有n多模块 。这些模块是独立于http的,而且需要日志的打印,这些日志肯定是需要记录的 。
则此模块不可省,那么考虑把ngx_http_core_module的error_log合并过来呢,想想也不行,为什么呢?
想想ngx_errlog_module将error_log信息存在哪里,想想ngx_http_core_module的error_log信息存在哪里 。
在调用ngx_error_log时候,把针对不同server或者location的error_log信息存储在哪里 。(模块之间不能深度耦合)
为了两个模块互不影响,这是个好办法呀!
二:access_log
接下来看一下access_log,access_log指令是属于ngx_http_log_module模块 。
ngx_http_log_module有三个指令:
static ngx_command_t ngx_http_log_commands[] = {
{ ngx_string("log_format"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_2MORE,
ngx_http_log_set_format,
NGX_HTTP_MAIN_CONF_OFFSET,
0,
NULL },
{ ngx_string("access_log"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
|NGX_HTTP_LMT_CONF|NGX_CONF_TAKE123,
ngx_http_log_set_log,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
{ ngx_string("open_log_file_cache"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
ngx_http_log_open_file_cache,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
ngx_null_command
};
每个block都可以配置上面的指令 ,这些指令对应着各个block配置中的loc[ngx_http_log_module.index](要理解这个,需要知道配置文件的整个解析过程和block对应关系),即下面这个数据结构:
typedef struct {
ngx_array_t *logs; /* array of ngx_http_log_t */
ngx_open_file_cache_t *open_file_cache;
time_t open_file_cache_valid;
ngx_uint_t open_file_cache_min_uses;
ngx_uint_t off; /* unsigned off:1 */
} ngx_http_log_loc_conf_t;
关于access_log主要有以下几个点:
1、ngx_http_log_module是属于nginx状态机最后一个阶段NGX_HTTP_LOG_PHASE的处理模块,即一个http请求结束时执行的,它的任务就是打印这次request的访问情况 。
2、access_log支持根据变量指令路径,如:
access_log logs/'$remote_addr'access.log main;
3、不管是变量路径还是常量路径,都将信息放入了 ngx_http_log_loc_conf_t的logs这个数组里进行管理,logs数组的元素是ngx_http_log_t 。
typedef struct {
ngx_open_file_t *file;
ngx_http_log_script_t *script;
time_t disk_full_time;
time_t error_log_time;
ngx_http_log_fmt_t *format;
} ngx_http_log_t;
当是常量的时候使用file记录,这个文件记录会放入到cycle->open_files
struct ngx_open_file_s {
ngx_fd_t fd;
ngx_str_t name;
u_char *buffer;
u_char *pos;
u_char *last;
};
当是变量的时候使用script记录

推荐阅读