nginx 模块内存泄漏通常由未释放的上下文对象、未关闭的文件描述符或错误的生命周期管理引起。处理这类问题的核心在于严格遵循 nginx 的生命周期钩子,在 ngx_http_cleanup_handler 或请求结束阶段显式调用销毁函数,并配合 ngx_pool_t 进行资源隔离。通过 valgrind 或 addresssanitizer 定位具体泄漏点,确保每个 alloc 都有对应的 free 或池回收机制。

如何识别和定位 Nginx 模块中的内存泄漏
定位 Nginx 模块内存泄漏最有效的方法是使用 valgrind 对编译后的二进制文件进行动态分析。由于 Nginx 采用多进程模型,主进程负责监听而工作进程处理请求,因此需要在测试环境中模拟并发请求以触发泄漏路径。使用 valgrind --leak-check=full --show-leak-kinds=all 运行 Nginx 工作进程,可以详细列出未释放的内存块及其调用栈。对于自定义模块,必须确保在 ngx_http_cleanup_handler 中注册清理回调,否则请求结束时的临时数据将随进程复用而累积,最终导致内存耗尽。

#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
static ngx_int_t my_module_init(ngx_conf_t *cf);
static void *my_module_create_loc_conf(ngx_conf_t *cf);
static char *my_module_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);
static ngx_command_t my_module_commands[] = {
{ ngx_string("my_module_setting"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_my_loc_conf_t, setting),
NULL },
ngx_null_command
};
static ngx_http_module_t my_module_ctx = {
NULL, /* preconfiguration */
my_module_init, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
my_module_create_loc_conf, /* create location configuration */
my_module_merge_loc_conf /* merge location configuration */
};
typedef struct {
ngx_str_t setting;
} ngx_http_my_loc_conf_t;
static ngx_http_module_t my_module_module = {
&my_module_ctx,
ngx_string("my_module")
};
static ngx_int_t my_module_init(ngx_conf_t *cf) {
return NGX_OK;
}
static void *my_module_create_loc_conf(ngx_conf_t *cf) {
ngx_http_my_loc_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_my_loc_conf_t));
if (conf == NULL) {
return NGX_CONF_ERROR;
}
// 正确做法:使用 ngx_pcalloc 分配,自动关联到当前请求的 pool
// 错误做法:使用 ngx_palloc 但未在清理时重置 pool,或在非请求上下文中分配
return conf;
}
static char *my_module_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) {
ngx_http_my_loc_conf_t *prev = parent;
ngx_http_my_loc_conf_t *conf = child;
ngx_conf_merge_str_value(conf->setting, prev->setting, "default");
return NGX_CONF_OK;
}
// 预期结果:每次请求创建新的 loc_conf,请求结束后 pool 被销毁,内存自动回收。
// 若在此处手动 free 或使用全局链表且未清理,则会导致泄漏。</ngx_http.h></ngx_core.h></ngx_config.h>
如何通过生命周期钩子防止内存泄漏
防止 Nginx 模块内存泄漏的关键是正确使用生命周期钩子。Nginx 提供了多个阶段用于初始化和清理资源,包括 ngx_http_top_request_handler 之前的清理阶段。开发者应在 create_loc_conf 中使用 ngx_pcalloc 分配内存,这样内存会自动绑定到请求的 pool 上,当请求结束时,整个 pool 会被一次性释放,无需逐个 free。如果必须在堆上分配独立于 pool 的资源(如打开的文件或网络连接),则必须注册 ngx_http_cleanup_handler,并在其中执行清理操作。此外,避免在 postconfiguration 阶段分配持久化内存而不提供清理机制,这是常见的泄漏源头。
Nginx 配置与调优技能。支持反向代理、负载均衡、SSL/TLS 终止、高性能静态资源服务。触发词:Nginx 配置、代理设置、SSL 证书、性能调优、负载均衡、反向代理。

#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
static ngx_int_t my_module_handler(ngx_http_request_t *r);
static void my_module_cleanup(void *data);
static ngx_command_t my_module_commands[] = {
ngx_null_command
};
static ngx_http_module_t my_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
NULL, /* create location configuration */
NULL /* merge location configuration */
};
static ngx_http_module_t my_module_module = {
&my_module_ctx,
ngx_string("my_module")
};
static ngx_int_t my_module_handler(ngx_http_request_t *r) {
// 模拟需要手动管理的资源
char *buffer = ngx_palloc(r->pool, 1024);
if (buffer == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
// 注册清理回调,确保即使发生错误也能释放资源
ngx_http_cleanup_t *cln = ngx_http_cleanup_add(r, 0);
if (cln == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
cln->handler = my_module_cleanup;
cln->data = buffer;
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = 0;
return ngx_http_send_header(r) == NGX_OK ? ngx_http_output_filter(r, NULL) : NGX_ERROR;
}
static void my_module_cleanup(void *data) {
// 注意:如果使用 ngx_palloc,通常不需要手动 free,因为 pool 会处理
// 但如果分配的是独立于 pool 的资源(如 malloc),则需在此处 free
// 此处示例为演示 cleanup 机制,实际中若用 ngx_palloc 可省略此步
// ngx_free(data); // 仅适用于非 pool 分配的内存
}
// 预期结果:通过 ngx_http_cleanup_add 注册清理函数,确保请求结束时资源被安全释放。
// 若未注册清理函数,且在 handler 中使用了非 pool 分配的内存,则会导致泄漏。</ngx_http.h></ngx_core.h></ngx_config.h>
在特定输入条件下如何验证模块是否发生内存泄漏
在特定输入条件下验证模块是否发生内存泄漏,需要构造能够触发异常路径或长生命周期的请求。例如,发送大量包含特殊字符或大负载的请求,观察工作进程的 RSS 内存变化。使用 ab -n 10000 -c 100 http://localhost/test 进行压力测试,并结合 top 或 /proc/[pid]/status 监控内存增长。如果内存持续增长且不回落,说明存在泄漏。进一步,可以在代码中添加日志,记录每次请求分配和释放的资源数量,对比两者差异。对于自定义模块,特别关注 merge_loc_conf 和 handler 中的内存操作,确保没有遗漏清理逻辑。










