Nginx 日誌分析

2022-04-04 08:51:51 字數 4471 閱讀 7070

nginx啟動由main開始執行,在nginx.c的278行,呼叫ngx_log_init(ngx_prefix),如下:

log = ngx_log_init(ngx_prefix);
其中【ngx_log.c】ngx_log_init(ngx_prefix)**如下【主要實現了建立日誌檔案/usr/local/logs/error.log】

ngx_log_t *

ngx_log_init(u_char *prefix)

p = null;

#if (ngx_win32)

if (name[1] != ':') else

if (plen)

p = ngx_cpymem(name, prefix, plen);

if (!ngx_path_separator(*(p - 1)))

ngx_cpystrn(p, (u_char *) ngx_error_log_path, nlen + 1);

p = name;}}

ngx_file_create_or_open,

ngx_file_default_access);

if (ngx_log_file.fd == ngx_invalid_file)

if (p)

return &ngx_log;

}

在之後的解析配置檔案nginx.conf的過程中

static ngx_command_t  ngx_errlog_commands = ,

ngx_null_command

};static ngx_core_module_t ngx_errlog_module_ctx = ;

ngx_module_t ngx_errlog_module = ;

其中ngx_error_log.c的**如下【主要實現了按照配置檔案的命令建立新的日誌檔案和設定日誌輸出的等級】

char *

ngx_log_set_levels(ngx_conf_t *cf, ngx_log_t *log)

log->log_level = n;

found = 1;

break;}}

for (n = 0, d = ngx_log_debug_first; d <= ngx_log_debug_last; d <<= 1)

log->log_level |= d;

found = 1;

break;}}

if (!found)

}// 如果error_log logs/error.log ngx_log_debug;相當於設定了全部的debug level.

if (log->log_level == ngx_log_debug)

return ngx_conf_ok;

}static char *

ngx_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)

value = cf->args->elts;

if (ngx_strcmp(value[1].data, "stderr") == 0) else

cf->cycle->new_log.file = ngx_conf_open_file(cf->cycle, &name);

if (cf->cycle->new_log.file == null)

if (cf->args->nelts == 2)

cf->cycle->new_log.log_level = 0;

return ngx_log_set_levels(cf, &cf->cycle->new_log);

}

若配置檔案中沒有配置使用error_log,則【ngx_cycle.c】如下**,作用生成新的new_log。

(此處發現ngx_cycle_s中的new_log僅僅是為了記錄新日誌檔案之用;另外為了記錄日誌初始化之前的日誌,所以ngx_log_init中建立

/usr/local/logs/error.log,若配置檔案中重新配置,則以配置檔案為準;若沒有配置,則就以此檔案記錄日誌)

if (cycle->new_log.file == null) 

}

最後,

cycle->log = &cycle->new_log;

pool->log = &cycle->new_log;

至此日誌可正常使用。

日誌使用nginx啟動由main開始執行,在nginx.c的278行,呼叫ngx_log_init(ngx_prefix),如下:

log = ngx_log_init(ngx_prefix);
其中【ngx_log.c】ngx_log_init(ngx_prefix)**如下【主要實現了建立日誌檔案/usr/local/logs/error.log】

ngx_log_t *

ngx_log_init(u_char *prefix)

p = null;

#if (ngx_win32)

if (name[1] != ':') else

if (plen)

p = ngx_cpymem(name, prefix, plen);

if (!ngx_path_separator(*(p - 1)))

ngx_cpystrn(p, (u_char *) ngx_error_log_path, nlen + 1);

p = name;}}

ngx_file_create_or_open,

ngx_file_default_access);

if (ngx_log_file.fd == ngx_invalid_file)

if (p)

return &ngx_log;

}

在之後的解析配置檔案nginx.conf的過程中

static ngx_command_t  ngx_errlog_commands = ,

ngx_null_command

};static ngx_core_module_t ngx_errlog_module_ctx = ;

ngx_module_t ngx_errlog_module = ;

其中ngx_error_log.c的**如下【主要實現了按照配置檔案的命令建立新的日誌檔案和設定日誌輸出的等級】

char *

ngx_log_set_levels(ngx_conf_t *cf, ngx_log_t *log)

log->log_level = n;

found = 1;

break;}}

for (n = 0, d = ngx_log_debug_first; d <= ngx_log_debug_last; d <<= 1)

log->log_level |= d;

found = 1;

break;}}

if (!found)

}// 如果error_log logs/error.log ngx_log_debug;相當於設定了全部的debug level.

if (log->log_level == ngx_log_debug)

return ngx_conf_ok;

}static char *

ngx_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)

value = cf->args->elts;

if (ngx_strcmp(value[1].data, "stderr") == 0) else

cf->cycle->new_log.file = ngx_conf_open_file(cf->cycle, &name);

if (cf->cycle->new_log.file == null)

if (cf->args->nelts == 2)

cf->cycle->new_log.log_level = 0;

return ngx_log_set_levels(cf, &cf->cycle->new_log);

}

若配置檔案中沒有配置使用error_log,則【ngx_cycle.c】如下**,作用生成新的new_log。

(此處發現ngx_cycle_s中的new_log僅僅是為了記錄新日誌檔案之用;另外為了記錄日誌初始化之前的日誌,所以ngx_log_init中建立

/usr/local/logs/error.log,若配置檔案中重新配置,則以配置檔案為準;若沒有配置,則就以此檔案記錄日誌)

if (cycle->new_log.file == null) 

}

最後,

cycle->log = &cycle->new_log;

pool->log = &cycle->new_log;

至此日誌可正常使用。

分析 Nginx 日誌

日誌格式 統計訪問 ip 次數 awk end access.log 統計訪問訪問大於 100 次的 ip awk end access.log 統計訪問 ip 次數並排序取前 10 awk end access.log 統計時間段訪問最多的 ip awk 4 02 jan 2017 00 02 0...

NGINX日誌分析

stub status模組主要用於檢視nginx的一些狀態資訊,例如統計nginx的訪問量,首先我們得檢視該模組有沒有安裝,如果沒有安裝,得先安裝,安裝好後,修改nginx的配置檔案,開啟該模組,然後就可以使用以下命令來進行統計,如 1.根據訪問ip統計uv awk access.log sort ...

Nginx日誌分析

日誌服務支援通過資料接入嚮導配置採集nginx日誌,並自動建立索引和nginx日誌儀錶盤,達到快速採集並分析nginx日誌。很多個人站長選取nginx作為伺服器搭建 在對 訪問情況進行分析時,需要對nginx訪問日誌統計分析,從中獲得 的訪問量,訪問時段等訪問情況,傳統模式下利用cnzz模式,在前端...