NGINX加载动态库相关问题



1.目的:一个handler的功能可以随着业务变化不断修改,如果静态编译的话nginx本身要经常编译,稳定性得不到保证。

所以在需要的点上先写好一个稳定的handler,然后由它来加载动态库,以后的维护就放在动态库中,只编译动态库。虽然损失百分之几的性能,

但可维护性大大增加。


2.方法:在read_conf事件处理函数中根据配置加载动态库并将句柄保存到loc_conf中,然后在每次请求的时候获取这个句柄调用。

read_conf时:

ngx_test_loc_conf_t *cglcf;
cglcf = ngx_http_conf_get_module_loc_conf(cf, ngx_module_test);
char dllpath[4096] = {0};
memcpy(dllpath, cglcf->ecdata.data, cglcf->ecdata.len);
cglcf->dllHandle = dlopen(dllpath, RTLD_LAZY);
if (cglcf->dllHandle == NULL) {
printf("could not load %s: %s\n", dlerror(), dllpath);
}

然后在主进程hadler中:

ngx_test_loc_conf_t *cglcf;
cglcf = ngx_http_get_module_loc_conf(r, ngx_module_test);

void * handle = cglcf->dllHandle;
if (handle != NULL) {

ngx_int_t(*func)(ngx_http_request_t *, ngx_buf_t * buf) = dlsym(handle, "ngx_test_dll_handler");
func(r, buf);

}

句柄不需要关闭,等nginx重启时释放。

3.开始在动态库的ngx_test_dll_handler中直接处理request的输出和申请内存池,但是这是个错误的思想,在主进程的handelr中将request和out

传给动态库,在动态库函数中仅从 request中获取内容和对out.buf数据进行修改。其它所有输出,转发等控制链还是在主进程的hander。

而且这是统一的通用流程,写好以后基本不需要修改。需要修改的就是对request和out.buf的控制。

最后主进程handler改成:

ngx_chain_t out;
ngx_buf_t *buf;
buf = ngx_pcalloc(r->pool, sizeof (ngx_buf_t));
if (buf == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed to allocate response buffer.");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
out.buf = buf;
out.next = NULL;

ngx_int_t(*func)(ngx_http_request_t *, ngx_chain_t *) = dlsym(handle, "ngx_test_dll_handler");
func(r, &out);

ngx_int_t rc;
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
}
return ngx_http_output_filter(r, &out);

动态库的示例为:

char * content = "This is a demo for axman.";
if (!(r->method & (NGX_HTTP_GET | NGX_HTTP_HEAD))) {
return NGX_HTTP_NOT_ALLOWED;
}
if (r->headers_in.if_modified_since) {
return NGX_HTTP_NOT_MODIFIED;
}

r->headers_out.content_type.len = sizeof ("text/html") - 1;
r->headers_out.content_type.data = (u_char *) "text/html";
r->headers_out.status = NGX_HTTP_OK;

r->headers_out.content_length_n = strlen(content);
if (r->method == NGX_HTTP_HEAD) {
return NGX_OK;
}
out->buf->pos = content;
out->buf->last = content + (strlen(content));
out->buf->memory = 1;
out->buf->last_buf = 1;
return NGX_OK;


4.so文件开始没加-g,调试进不去,编译时别忘记加-g。



你可能感兴趣的:(NGINX加载动态库相关问题)