栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

nginx里ngx

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

nginx里ngx

        location /wyx {
             sean 1 2 3 4 $arg_keyA;
        }

以上面的配置块为例,我们要在nginx运行时获取内置变量$arg_key的值,$arg_keyA内置变量表示在http的get请求时的参数名为key的参数,比如请求http://127.0.0.1:8881/wyx?keyA=42,我们要通过ngx_http_compile_complex_value和ngx_http_complex_value来获取keyA的值42。

我们可以这样来做,首先我们在配置阶段使用ngx_http_compile_complex_value来关联(编译)我们需要的变量

//获取复杂的值,就是带变量的值
static void ngx_sean_compile_complex_value_test(ngx_conf_t *cf, ngx_sean_conf_t* sean_cf)
{
	//sean 1 2 3 4 $arg_keyA;   这里取变量$arg_keyA
	ngx_str_t                         *value;

	//args是一个数组,他保存了配指令的名字及参数,这里就是sean 1 2 3 4 $arg_keyA
	value = (ngx_str_t*)cf->args->elts;   

	if(cf->args->nelts>=5)
	{
		ngx_http_compile_complex_value_t   ccv;
		ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
		
		ccv.cf = cf;
		
		//value[5]既是$arg_keyA
		ccv.value = &value[5];
		ccv.complex_value = &sean_cf->complex_value;

		//编译$arg_keyA
		if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {

			ngx_log_error(NGX_LOG_alert, cf->log, 0
					, "ngx_http_compile_complex_value error");

			return;
			//return NGX_CONF_ERROR;
		}
	}

	ngx_log_error(NGX_LOG_alert, cf->log, 0
			, "ngx_sean_compile_complex_value_test cf->args->nelts %d", cf->args->nelts);
	
}

这里我是在配置阶段的ngx_sean_block函数里处理的,

static ngx_command_t  ngx_sean_commands[] = {

    { ngx_string("sean"),         //               name
      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_TAKE4|NGX_CONF_TAKE5,   //type NGX_CONF_NOARGS 标识后面不带参数 NGX_CONF_TAKE4 标识后面可以带4个参数
      ngx_sean_block,             //  char               *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
      NGX_HTTP_LOC_CONF_OFFSET,                       //   conf
      0,                          //   offset 
      NULL },                     //   post

     ngx_null_command
};

当请求http://127.0.0.1:8881/wyx?keyA=42到来时,我们来获取这个参数的值

static void ngx_sean_complex_value_test(ngx_http_request_t *r, ngx_sean_conf_t* sean_cf)
{
	ngx_str_t keyA;
	//sean_cf->complex_value是我们上面ngx_http_compile_complex_value编译处理好的结果
	//ngx_http_complex_value的结果会保存到ngx_str_t keyA里
	if (ngx_http_complex_value(r, &sean_cf->complex_value, &keyA) != NGX_OK) {
		return;
		//return NGX_HTTP_INTERNAL_SERVER_ERROR;
	}

	ngx_log_error(NGX_LOG_alert, r->connection->log, 0
		, "ngx_sean_complex_value_test complex_value %V, sean_cf %p", &keyA, sean_cf);
}

运行得到结果

2021/11/03 16:54:23 [alert] 11804#0: *1 ngx_sean_complex_value_test keyA 42, sean_cf 0000560AF31E6FD8, client: 127.0.0.1, server: localhost, request: "GET /wyx?key=42 HTTP/1.1", host: "127.0.0.1:8881"

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/422734.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号