http配置项解析编程

配置config

ngx_addon_name=ngx_http_mytest_module

HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module"

NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mytest_module.c"

在nginx_http_mytest_module.c中定义mytest模块,使用预设方法解析test_flag,test_str,test_num配置项,使用自己定义配置项处理方法解析mytest项。

<pre class="cpp" name="code">#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h> //create a structure
typedef struct{
ngx_str_t my_str;
ngx_int_t my_num;
ngx_flag_t my_flag;
ngx_str_t mytest_str;
ngx_int_t mytest_num;
} ngx_http_mytest_conf_t; static char* ngx_conf_set_mytest(ngx_conf_t* cf, ngx_command_t* cmd, void* conf);
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t* r); static void* ngx_http_mytest_create_loc_conf(ngx_conf_t* cf);
static char* ngx_http_mytest_merge_loc_conf(ngx_conf_t* cf, void* parent, void* child); //set the resolution of configuration items
static ngx_command_t ngx_http_mytest_commands[] = {
{
//test_flag configuration item
ngx_string("test_flag"),
NGX_HTTP_LOC_CONF | NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mytest_conf_t, my_flag),
NULL
},
{
//test_str configuration item
ngx_string("test_str"),
NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mytest_conf_t, my_str),
NULL
},
{
//test_num configuration item
ngx_string("test_num"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mytest_conf_t, my_num),
NULL
},
{
//mytest configuration item
ngx_string("mytest"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE2,
ngx_conf_set_mytest,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
}, ngx_null_command
};
static ngx_http_module_t ngx_http_mytest_module_ctx = {
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
ngx_http_mytest_create_loc_conf, //create location configuration
ngx_http_mytest_merge_loc_conf //merge the loc configurations
}; ngx_module_t ngx_http_mytest_module = {
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
}; static char* ngx_conf_set_mytest(ngx_conf_t* cf, ngx_command_t* cmd, void* conf)
{
ngx_http_mytest_conf_t* mycf = conf; ngx_str_t* value = cf->args->elts; mycf->mytest_num = ngx_atoi(value[2].data, value[2].len); mycf->mytest_str = value[1];
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_handler; return NGX_CONF_OK;
} static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t* r)
{
//we response 'GET' and 'HEAD' requests only
if(!(r->method & (NGX_HTTP_GET | NGX_HTTP_HEAD | NGX_HTTP_POST)))
{
return NGX_HTTP_NOT_ALLOWED;
}
//discard request body, since we don't need it here
ngx_int_t rc = ngx_http_discard_request_body(r);
if(rc != NGX_OK){
return rc;
} ngx_http_mytest_conf_t* my_conf;
my_conf = ngx_http_get_module_loc_conf(r, ngx_http_mytest_module); ngx_str_t type = ngx_string("text/plain"); u_char nginx_str_mytest[1024] = {0};
ngx_sprintf(nginx_str_mytest,"test_str=%s,test_flag=%i,test_num=%i,mytest_str=%s,mytest_num=%i",my_conf->my_str.data,my_conf->my_flag,my_conf->my_num,my_conf->mytest_str.data,my_conf->mytest_num); r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = ngx_strlen(nginx_str_mytest);
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_pcalloc(r->pool, sizeof(ngx_buf_t));
if(b == NULL)
{
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
b->pos = nginx_str_mytest;
b->last = nginx_str_mytest + ngx_strlen(nginx_str_mytest);
b->memory = 1;
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* conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_mytest_conf_t));
if(NULL == conf){
return NGX_CONF_ERROR;
} conf->my_str.len = 0;
conf->my_str.data = NULL;
conf->my_num = NGX_CONF_UNSET;
conf->my_flag = NGX_CONF_UNSET;
return conf;
} static char* ngx_http_mytest_merge_loc_conf(ngx_conf_t* cf, void* parent, void* child)
{
ngx_http_mytest_conf_t* prev = parent;
ngx_http_mytest_conf_t* conf = child;
ngx_conf_merge_str_value(conf->my_str, prev->my_str,""); return NGX_CONF_OK;
}



在ngx.conf中http以下默认server中增加例如以下配置项

location /test{

test_flag on;

test_str "helloworld";

test_num 88;

mytest"hello" 88;

}

启动nginx

./configure --add-module=/home/chen123/nginx/exp3

make

make install

sudo /usr/local/nginx/sbin/nginx

结果例如以下

Nginx学习——http配置项解析编程的更多相关文章

  1. Nginx学习之二-配置项解析及编程实现

    在开发功能灵活的Nginx模块时,需要从配置文件中获取特定的信息.不过,我们并不需要再编写一套读取配置的系统,Nginx已经为用户提供了强大的配置项解析机制,同时还支持“-s reload”命令,可以 ...

  2. [转帖]nginx学习,看这一篇就够了:下载、安装。使用:正向代理、反向代理、负载均衡。常用命令和配置文件

    nginx学习,看这一篇就够了:下载.安装.使用:正向代理.反向代理.负载均衡.常用命令和配置文件 2019-10-09 15:53:47 冯insist 阅读数 7285 文章标签: nginx学习 ...

  3. Nginx学习回顾总结 部分:

    21:46 2015/11/9Nginx学习回顾总结进程间通信,近似于socket通信的的东西:才发现这种通信并不是很难,并不是我想象的那样很多内容,新领域,入门只是几个函数的使用而已.以前猜过是这样 ...

  4. Nginx学习之十一-Nginx启动框架处理流程

    Nginx启动过程流程图 下面首先给出Nginx启动过程的流程图: ngx_cycle_t结构体 Nginx的启动初始化在src/core/nginx.c的main函数中完成,当然main函数是整个N ...

  5. 学习《Python核心编程》做一下知识点提要,方便复习(一)

    学习<Python核心编程>做一下知识点提要,方便复习. 计算机语言的本质是什么? a-z.A-Z.符号.数字等等组合成符合语法的字符串.供编译器.解释器翻译. 字母组合后产生各种变化拿p ...

  6. nginx源代码分析--配置文件解析

    ngx-conf-parsing 对 Nginx 配置文件的一些认识: 配置指令具有作用域,分为全局作用域和使用 {} 创建其他作用域. 同一作用域的不同的配置指令没有先后顺序:同一作用域能否使用同样 ...

  7. WCF学习笔记之事务编程

    WCF学习笔记之事务编程 一:WCF事务设置 事务提供一种机制将一个活动涉及的所有操作纳入到一个不可分割的执行单元: WCF通过System.ServiceModel.TransactionFlowA ...

  8. Nginx学习---Nginx的详解_【all】

    1.1. Nginx简介 1.什么是nginx nginx:静态的,开源的www软件,可以解析静态的小文件(低于1M ),支持高并发占用较发少的资源(3W并发,10个进程,内存150M),跨平台 te ...

  9. Nginx学习一路向西

    Nginx 学习一路向北 Java大猿帅成长手册,GitHub JavaEgg ,N线互联网开发必备技能兵器谱 1. Nginx简介 1.1 Nginx 概述 NGINX是一个免费.开源.高性能.轻量 ...

随机推荐

  1. Unity 鼠标点击左右移动,人物跟随旋转

    上代码: using UnityEngine; using System.Collections; public class Test : MonoBehaviour { private Vector ...

  2. 学习《Python核心编程》做一下知识点提要,方便复习(一)

    学习<Python核心编程>做一下知识点提要,方便复习. 计算机语言的本质是什么? a-z.A-Z.符号.数字等等组合成符合语法的字符串.供编译器.解释器翻译. 字母组合后产生各种变化拿p ...

  3. 对‘pthread_create’未定义的引用

    由于pthread库不是Linux系统默认的库,连接时需要使用库libpthread.a,所以在使用pthread_create创建线程时,在编译中要加-l pthread参数: gcc exampl ...

  4. 【二分答案nlogn/标解O(n)】【UVA1121】Subsequence

    A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...

  5. win7 下面使用任务计划程序执行php脚步

    1.操作系统中点击开始->所有程序->附件->系统工具->任务计划程序 2.如下图 3.下一步,如图: . 4.下一步,如图 5.下一步,如下图: 6.这样设置好以后,就可以了 ...

  6. ffmpeg的使用

    -i "转换前的视频绝对路径\文件名.mp4" -movflags faststart+rtphint "转换后的视频绝对路径\文件名.mp4" 上面这条语句是 ...

  7. 使用XE5-PACTH破解Delphi-XE5时,出现检查文件大小失败的解决方法

    今天给自己的64位Win7电脑破解Delphi-XE5时,一直出现检查bds.exe尺寸大小失败的错误提示. 网上查了很久,Csdn上给了个破解的bds.exe,本人一直没什么Csdn积分,没得下,所 ...

  8. c语言中的经典算法

    c语言经典排序以及查找方法 冒泡排序 #include <stdio.h> int main() { int i, j, t, a[11]; /*定义变量及数组为基本整型*/ printf ...

  9. Android studio教程:[1] 创建app项目

    Android studio作为面市不久的安卓开发工具,越来越受到大家的喜爱,这里我将介绍如何在Android studio中创建一个APP项目,并在以后经验中介绍其他有关Android studio ...

  10. noNamespaceSchemaLocation前添加xsi

    在.Net中操作xml文档,给节点添加,xsi:noNamespaceSchemaLocation属性时,不可以使用 XmlElement eleRoot = doc.CreateElement(&q ...