nginx 自定义带参数模块
来源:互联网 发布:python 判断主机存活 编辑:程序博客网 时间:2024/06/07 00:01
上一文基本都是从书中学来的。例子也基本都是书中的。接下来尝试着自己写了个超级简单的自定义模块。另外加了参数的解析:
照旧先将demo代码贴上:
#include <ngx_config.h>#include <ngx_core.h>#include <ngx_http.h>static char* ngx_http_mytest2(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);static void* ngx_http_mytest_create_loc_conf(ngx_conf_t *cf);static ngx_int_t ngx_http_mytest_handler2(ngx_http_request_t *r);//自定义的参数结构体typedef struct { ngx_str_t my_config_str; ngx_int_t my_config_num;} ngx_http_mytest_conf_t;static ngx_command_t ngx_http_mytest_commands[] = { { ngx_string("mytest2"),//指令名称,在配置文件中使用 NGX_HTTP_LOC_CONF | NGX_CONF_TAKE12, ngx_http_mytest2, // 回调函数 NGX_HTTP_LOC_CONF_OFFSET, //位置 0, //指令的值保存的位置 NULL }, ngx_null_command};static char* ngx_http_mytest2(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){ /*//注意,参数HTTP框架传给用户的在ngx_http_mytest_create_loc_conf回调方法中分配的结构体ngx_http_mytest_conf_t*/ ngx_http_mytest_conf_t *mycf = conf; /* cf->args是1个ngx_array_t队列,它的成员都是ngx_str_t结构。我们用value指向ngx_array_t的elts内容,其中 value[1]就是第1个参数,同理,value[2]是第2个参数 */ ngx_str_t *value = cf->args->elts; // ngx_array_t的nelts表示参数的个数 if (cf->args->nelts > 1) { // 直接赋值即可,ngx_str_t结构只是指针的传递 mycf->my_config_str = value[1]; } if (cf->args->nelts > 2) { // 将字符串形式的第2个参数转为整型 mycf->my_config_num = ngx_atoi(value[2].data, value[2].len); /*如果字符串转化整型失败,将报“invalid number”错误,Nginx启动失败*/ if (mycf->my_config_num == NGX_ERROR) { return "invalid number"; } } ngx_http_core_loc_conf_t *clcf; clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); clcf->handler = ngx_http_mytest_handler2; // 返回成功 return NGX_CONF_OK;}static ngx_http_module_t ngx_http_mytest_module_ctx = { NULL, NULL, NULL, NULL, NULL, NULL, ngx_http_mytest_create_loc_conf, NULL};ngx_module_t ngx_http_mytest_module2 = { NGX_MODULE_V1, &ngx_http_mytest_module_ctx, ngx_http_mytest_commands, NGX_HTTP_MODULE, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NGX_MODULE_V1_PADDING};//回调函数 handlerstatic ngx_int_t ngx_http_mytest_handler2(ngx_http_request_t *r){ ngx_http_mytest_conf_t *mycf = ngx_http_get_module_loc_conf(r, ngx_http_mytest_module2); if(!(r->method & (NGX_HTTP_GET | NGX_HTTP_HEAD))) return NGX_HTTP_NOT_ALLOWED; ngx_int_t rc = ngx_http_discard_request_body(r); if(rc != NGX_OK) return rc; ngx_str_t type = ngx_string("text/plain"); char a1[] = "Hello, this is nginx world!"; char a2[mycf->my_config_str.len +1] ; size_t i=0; while(i < mycf->my_config_str.len){ a2[i]=mycf->my_config_str.data[i]; i++; } a2[i] = '\0'; char a3[sizeof(a1)+sizeof(a2) -1]; memset(a3,0,sizeof(a1)+sizeof(a2) -1); strcat(a3,a2); strcat(a3,a1); ngx_str_t response = ngx_string(a3); r->headers_out.status = NGX_HTTP_OK; r->headers_out.content_length_n = response.len; r->headers_out.content_type = type; rc = ngx_http_send_header(r); if(rc == NGX_ERROR || rc > NGX_OK || r->header_only) return rc; ngx_buf_t *b; b = ngx_create_temp_buf(r->pool, response.len); if(b == NULL) return NGX_HTTP_INTERNAL_SERVER_ERROR; ngx_memcpy(b->pos, response.data, response.len); b->last = b->pos + response.len; b->last_buf = 1; ngx_chain_t out; //构造输出链表 out.buf = b; out.next = NULL; return ngx_http_output_filter(r, &out);}static void* ngx_http_mytest_create_loc_conf(ngx_conf_t *cf){ ngx_http_mytest_conf_t *mycf; mycf = (ngx_http_mytest_conf_t *)ngx_pcalloc(cf->pool, sizeof(ngx_http_mytest_conf_t)); if (mycf == NULL) { return NULL; } //mycf->my_config_str = NGX_CONF_UNSET; mycf->my_config_num = NGX_CONF_UNSET; return mycf;}
其实跟之前的例子就是多了一个队参数的解析。定义了一个结构体。然后 在ctx 的 create_loc_conf 方法来创建保存参数的结构体。在ngx_command_t 的set方法里面可以获取到哪个参数。然后来处理。这个例子很简单。
0 0
- nginx 自定义带参数模块
- nginx模块参数
- nginx 自定义模块
- 带参数的内核模块
- 带参数到内核模块
- 自定义事件(自定义参数,带参数实现)
- nginx模块使用自定义变量
- nginx自定义模块编写-根据post参数路由到不同服务器
- nginx自定义模块编写-根据post参数路由到不同服务器
- Nginx自定义模块编写:根据post参数路由到不同服务器
- nginx自定义模块编写-根据post参数路由到不同服务器
- Nginx自定义模块编写:根据post参数路由到不同服务器
- Nginx自定义模块编写:根据post参数路由到不同服务器
- JavaWeb---自定义带参参数
- Nginx 自带防盗链模块
- nginx获取自定义header参数
- 带参数的可加载模块
- nginx 修改flv模块,增加end 参数
- Mybatis步步进阶(二)——SQL的基本配置与执行
- 【c#、Red5】关于c#如何使用FluorineFx与Red5通信及相关
- Ubuntu Linux 安装后,建立嵌入式开发环境
- Minidwep-gtk字典 破 WPA
- jQuery的事件,选择器,h5的表单
- nginx 自定义带参数模块
- tomcat结合nginx使用小结
- 三大WEB服务器(apache lighttpd nginx) 对比分析
- 前端开发面试题之 HTML
- good explanation of __read_mostly, __init, __exit macros
- MVC常遇见的几个场景代码分享
- Mybatis步步进阶(三)——1:N关系配置 && 动态SQL拼接
- Android电话本数据查询总结
- 关于pycharm编码问题