本文建立在
1.14种预设方法
1.ngx_conf_set_flag_slot
假设我们希望在nginx.conf中有一个配置项的名称为test_flag,它的后面携带1个参数,这个参数的取值必须是on或者off
引入
ngx_flag_t my_flag;
typedef intptr_t ngx_flag_t;//相当于ngx_flag_int
mycf->test_flag=NGX_CONF_UNSET//必须初始化
static ngx_command_t ngx_http_mytest_commands[] = {
…
{ ngx_string("test_flag"),//配置名
NGX_HTTP_LOC_CONF| NGX_CONF_FLAG,//type 出现在location{}
ngx_conf_set_flag_slot,//方法
NGX_HTTP_LOC_CONF_OFFSET,//loc内存偏移
offsetof(ngx_http_mytest_conf_t, my_flag),//配置项
NULL },
ngx_null_command
};
1.ngx_flag出现在ngx_http_mytest_conf_t中
2.test_flag配置只能出现在location{…}块中。其中,test_flag配置项的参数为on时,ngx_http_mytest_conf_t结构体中的my_flag会设为 1,而参数为off时my_flag会设为0
2.ngx_conf_set_str_slot
ngx_str_t my_str;
static ngx_command_t ngx_http_mytest_commands[] = {
...
{ ngx_string("test_str"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF| NGX_CONF_TAKE1,//参数一 可以出现http //server location
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mytest_conf_t, my_str),
NULL },
ngx_null_command
};
效果:
配置项
location …
{
test_str apple;
}
结果:
my_str的值为{len=5;data=“apple”;}
3.ngx_conf_set_str_array_slot(同名 代表不同变量)
如果希望在nginx.conf中有多个同名配置项,如名称是test_str_array,那么每个配置项后 都跟着一个字符串参数。这些同名配置项可能具有多个不同的参数值。这时,可以使用 ngx_conf_set_str_array_slot预设方法
static ngx_command_t ngx_http_mytest_commands[] = {
...
{ ngx_string("test_str_array"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,//变量一
ngx_conf_set_str_array_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mytest_conf_t, my_str_array),
NULL },
ngx_null_command
};
效果:
location …
{
test_str_array Content-Length;
test_str_array Content-Encoding;
}
ngx_str_t*pstr=mycf->my_str_array->elts;
pstr[0]={en=14;data=“Content-Length”}
pstr[1]={len=16;data=“Content-Encoding”;}
(4)ngx_conf_set_keyval_slot(同名变量 2值)
ngx_conf_set_keyval_slot与ngx_conf_set_str_array_slot非常相似,唯一的不同点是 ngx_conf_set_str_array_slot要求同名配置项后的参数个数是1,而ngx_conf_set_keyval_slot则要 求配置项后的参数个数是2,分别表示key/value。如果用ngx_array_t*类型的my_keyval变量存 储以test_keyval作为配置名的参数,则必须设置NGX_CONF_TAKE2,表示test_keyval后跟两 个参数
static ngx_command_t ngx_http_mytest_commands[] = {
...
{ ngx_string("test_keyval"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE2,
ngx_conf_set_keyval_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mytest_conf_t, my_keyval),
NULL },
ngx_null_command
};
效果
location …
{
test_keyval Content-Type image/png;
test_keyval Content-Type image/gif;
test_keyval Accept-Encoding gzip;
}
typedef struct {
ngx_str_t key;
ngx_str_t value;
} ngx_keyval_t;
ngx_keyval_t* pkv = mycf->my_keyval->elts
pkv[0]={“Content-Type”,“image/png”}
....
note1:
在ngx_http_mytest_create_loc_conf创建结构体时,如果想使用 ngx_conf_set_keyval_slot,必须把my_keyval初始化为NULL空指针,“mycf>my_keyval=NULL;”,否则ngx_conf_set_keyval_slot在解析时会报错。
(5)ngx_conf_set_num_slot
ngx_conf_set_num_slot处理的配置项必须携带1个参数,这个参数必须是数字。我们用 ngx_http_mytest_conf_t结构中的以下成员来存储这个数字参数如下所示
mycf->my_num=NGX_CONF_UNSET: //初始化
static ngx_command_t ngx_http_mytest_commands[] = {
...
{ 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 },
ngx_null_command
};
效果:
location{
test_num 10;
}
my_num=10
(6)ngx_conf_set_size_slot和ngx_conf_set_off_slot
如果希望配置项表达的含义是空间大小,那么用ngx_conf_set_size_slot来解析配置项是非 常合适的,因为ngx_conf_set_size_slot允许配置项的参数后有单位,例如,k或者K表示 Kilobyte,m或者M表示Megabyte。用ngx_http_mytest_conf_t结构中的size_t my_size;来存储参 数,解析后的my_size表示的单位是字节
mycf->my_size=NGX_CONF_UNSET_SIZE;
mycf->my_off=NGX_CONF_SET
static ngx_command_t ngx_http_mytest_commands[] = {
...
{ ngx_string("test_size"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mytest_conf_t, my_size),
NULL },
ngx_null_command
};
static ngx_command_t ngx_http_mytest_commands[] = {
...
{ ngx_string("test_off"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_off_slot, NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mytest_conf_t, my_off),
NULL },
ngx_null_command
};
效果:
因为ngx_conf_set_size_slot允许配置项的参数后有单位,
例如,k或者K表示Kilobyte,m或者M表示Megabyte。用ngx_http_mytest_conf_t结构中的size_t my_size;来存储参数,解析后的my_size表示的单位是字节
如果在nginx.conf中配置了test_size 10k;,那么my_size将会设置为10240。如果配置为
test_size 10m;,则my_size会设置为10485760。
事实上,ngx_conf_set_off_slot与ngx_conf_set_size_slot是非常相似的,最大的区别是
ngx_conf_set_off_slot支持的参数单位还要多1个g或者G,表示Gigabyte
(7)ngx_conf_set_sec_slot和ngx_conf_set_msec_slot
static ngx_command_t ngx_http_mytest_commands[] = {
...
{ ngx_string("test_msec"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_msec_slot, NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mytest_conf_t, my_msec),
NULL },
ngx_null_command
};
static ngx_command_t ngx_http_mytest_commands[] = {
...
{ ngx_string("test_sec"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_sec_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mytest_conf_t, my_sec),
NULL },
ngx_null_command
};
一个将配置项为sec(秒) 另一个为msec(毫秒)
两个都要初始化为NGX_CONF_UNSET
(8)ngx_conf_set_bufs_slot
Nginx中许多特有的数据结构都会用到两个概念:单个ngx_buf_t缓存区的空间大小和允 许的缓存区个数。ngx_conf_set_bufs_slot就是用于设置它的,它要求配置项后必须携带两个 参数,第1个参数是数字,通常会用来表示缓存区的个数;第2个参数表示单个缓存区的空间 大小,它像ngx_conf_set_size_slot中的参数单位一样,可以不携带单位,也可以使用k或者 K、m或者M作为单位,如“gzip_buffers 4 8k;”
typedef struct {
ngx_int_t num;
size_t size;
} ngx_bufs_t;
static ngx_command_t ngx_http_mytest_commands[] = {
...
{ ngx_string("test_bufs"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE2,
ngx_conf_set_bufs_slot, NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mytest_conf_t, my_bufs),
NULL },
ngx_null_command
};
(9)ngx_conf_set_enum_slot和ngx_conf_bitmask_slot(效率更高)
typedef struct {
ngx_str_t name;
ngx_uint_t value;
} ngx_conf_enum_t;
typedef struct {
ngx_str_t name;
ngx_uint_t mask;
} ngx_conf_bitmask_t;
static ngx_conf_enum_t test_enums[] = {
{ ngx_string("apple"), 1 },
{ ngx_string("banana"), 2 },
{ ngx_string("orange"), 3 },
{ ngx_null_string, 0 }
};
static ngx_conf_bitmask_t test_bitmasks[] = {
{ ngx_string("good"), 0x0002 },
{ ngx_string("better"), 0x0004 },
{ ngx_string("best"), 0x0008 },
{ ngx_null_string, 0 }
};
static ngx_command_t ngx_http_mytest_commands[] = {
...
{ ngx_string("test_enum"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_enum_slot, NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mytest_conf_t, my_enum_seq),
test_enums },
ngx_null_command
};
static ngx_command_t ngx_http_mytest_commands[] = {
...
{ ngx_string("test_bitmask"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_bitmask_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mytest_conf_t, my_bitmask),
test_bitmasks },
ngx_null_command
};
1.name表示配置项后的参数只能与name指向的字符串相等,而value表示如果参数 中出现了name,ngx_conf_set_enum_slot方法将会把对应的value设置到存储的变量中
2 bitmask整型是4字节(32 位)的话,在这个枚举配置项中最多只能有32项。
效果:
这样,如果在nginx.conf中出现了配置项test_enum banana;,my_enum_seq的值是2。如果
配置项test_enum出现了除apple、banana、orange之外的值,Nginx将会报“invalid value”错误
(10)ngx_conf_set_access_slot
ngx_conf_set_access_slot用于设置读/写权限,配置项后可以携带1~3个参数,因此,在 ngx_command_t中的type成员要包含NGX_CONF_TAKE123
static ngx_command_t ngx_http_mytest_commands[] = {
...
{ ngx_string("test_access"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE123,
ngx_conf_set_access_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mytest_conf_t, my_access),
NULL },
ngx_null_command
};
(14)ngx_conf_set_path_slot
ngx_conf_set_path_slot可以携带1~4个参数,其中第1个参数必须是路径,第2~4个参数必 须是整数(大部分情形下可以不使用)
ngx_conf_set_path_slot会把配置项中的路径参数转化为ngx_path_t结构
typedef struct {
ngx_str_t name;
size_t len;
size_t level[3];
ngx_path_manager_pt manager;
ngx_path_loader_pt loader;
void data;
u_char conf_file;
ngx_uint_t line;
} ngx_path_t
其中,name成员存储着字符串形式的路径,而level数组就会存储着第2、第3、第4个参 数
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/129613.html