Parse the log module in Nginx and the basic initialization and filtering configuration of the log
- 2020-05-10 23:31:34
- OfStack
No matter in any project, the log is a very important module, whether it is problem location or daily information management, it is inseparable from him
In nginx, the ngx_errlog_module module is dedicated to handling the nginx log information, and is one of the core modules of nginx
In the main function, the initialization of the log module takes place immediately after the completion of the time initialization
Log structure:
The main thing the initialization of the log module does is to initialize the global variable ngx_log and create the errlog file
ngx_log_s structure
The ngx_log variable is an ngx_log_s structure, which is defined in the core/ ngx_log.h file. The structure is as follows:
// struct ngx_log_s
// Log structures {{{
struct ngx_log_s {
ngx_uint_t log_level; // The log level
ngx_open_file_t *file; // Log file information
ngx_atomic_uint_t connection; // The number of connections that reference this log object
ngx_log_handler_pt handler; // The callback function to call when the log is output
void *data; // The parameters required by the callback function
ngx_log_writer_pt writer;
void *wdata;
/*
* we declare "action" as "char *" because the actions are usually
* the static strings and in the "u_char *" case we have to override
* their types all the time
*/
char *action; // Before logging, save the action that is currently in progress
ngx_log_t *next; // Point to the 1 The log object
}; // }}}
The level of logging
There are 9 levels of logging in nginx:
#define NGX_LOG_STDERR 0
#define NGX_LOG_EMERG 1
#define NGX_LOG_ALERT 2
#define NGX_LOG_CRIT 3
#define NGX_LOG_ERR 4
#define NGX_LOG_WARN 5
#define NGX_LOG_NOTICE 6
#define NGX_LOG_INFO 7
#define NGX_LOG_DEBUG 8
ngx_open_file_s structure
Among them, the file description structure of the log is as follows:
// struct ngx_open_file_s
// Open file structure {{{
struct ngx_open_file_s {
ngx_fd_t fd;
ngx_str_t name;
void (*flush)(ngx_open_file_t *file, ngx_log_t *log);
// Callback called when the file is refreshed
void *data; // The parameter used by the callback function
}; // }}}
All file related data structures and operations are in the core/ ngx_conf_file.h and core/ ngx_conf_file.c files
Log initialization process
The log initialization operations are all in the core/ ngx_log.c file, and now we are calling the initialization function in the core/ ngx_log.c file:
// ngx_log_t *ngx_log_init(u_char *prefix);
// Initialize the ngx_log Structure, create errlog file {{{
ngx_log_t *
ngx_log_init(u_char *prefix)
{
u_char *p, *name;
size_t nlen, plen;
ngx_log.file = &ngx_log_file;
ngx_log.log_level = NGX_LOG_NOTICE;
name = (u_char *) NGX_ERROR_LOG_PATH;
/*
* we use ngx_strlen() here since BCC warns about
* condition is always false and unreachable code
*/
nlen = ngx_strlen(name);
if (nlen == 0) {
ngx_log_file.fd = ngx_stderr;
return &ngx_log;
}
p = NULL;
// Determine if it is an absolute path
#if (NGX_WIN32)
if (name[1] != ':') {
#else
if (name[0] != '/') {
#endif
if (prefix) {
plen = ngx_strlen(prefix);
} else {
#ifdef NGX_PREFIX
prefix = (u_char *) NGX_PREFIX;
plen = ngx_strlen(prefix);
#else
plen = 0;
#endif
}
if (plen) {
name = malloc(plen + nlen + 2);
if (name == NULL) {
return NULL;
}
p = ngx_cpymem(name, prefix, plen);
if (!ngx_path_separator(*(p - 1))) {
*p++ = '/';
}
ngx_cpystrn(p, (u_char *) NGX_ERROR_LOG_PATH, nlen + 1);
p = name;
}
}
ngx_log_file.fd = ngx_open_file(name, NGX_FILE_APPEND,
NGX_FILE_CREATE_OR_OPEN,
NGX_FILE_DEFAULT_ACCESS);
if (ngx_log_file.fd == NGX_INVALID_FILE) {
ngx_log_stderr(ngx_errno,
"[alert] could not open error log file: "
ngx_open_file_n " \"%s\" failed", name);
#if (NGX_WIN32)
ngx_event_log(ngx_errno,
"could not open error log file: "
ngx_open_file_n " \"%s\" failed", name);
#endif
ngx_log_file.fd = ngx_stderr;
}
if (p) {
ngx_free(p);
}
return &ngx_log;
}
// }}}
This initialization process is very easy to understand, and the rest of the log operations are also in this file, which we will discuss in more detail later
After the function is executed, it returns the address of the ngx_log structure
The value of ngx_log structure is as follows:
{
log_level = 6,
file = 0x80e3000 <ngx_log_file>,
connection = 0,
handler = 0x0,
data = 0x0,
writer = 0x0,
wdata = 0x0,
action = 0x0,
next = 0x0
}
Value of file field:
{
fd = 3,
name = {
len = 0,
data = 0x0
},
flush = 0x0,
data = 0x0
}
No specific log is logged with ngx_log_if
Step 1:
To download Github ngx_log_if address https first: / / github com/cfsego/ngx_log_if /
Step 2:
Install the 3rd party module to Nginx. The installation of the third party module can reference http: / / wiki nginx. org / 3 rdPartyModules use - add ngx_log_if - module added after decompression are as follows
./configure --add-module=/var/local/ngx_log_if-master
Then compile and install Nginx.
Step 3:
Configure access_log_bypass_if to nginx.conf configuration file
server {
location / {
access_log_bypass_if ($status = 404); # Don't record 404 Status of all log information
access_log_bypass_if ($uri ~* 'images'); # Don't record uri All of the images Log information for files in the directory
access_log_bypass_if ($uri = '/index.html'); # Don't record uri for /index.html Log information
access_log_bypass_if ($host ~* 'tonv.cc'); # Don't record host for tonv.cc All log information
}
}
Restart Nginx to filter out specific logs