update 2026-10-27 07:31:06

This commit is contained in:
action committed 2026-10-27 07:31:06 +08:00
1 parent 197c60d843
commit a4d6470641
4213 files changed
-1575003

No files matched your search

@@ -1,343 +0,0 @@
# Module ngx-dynamic-conf-module
---
## Instructions
System will reload conf when nginx dynamic config file change. Developer can use this module to reload file without reload nginx worker.
Now it support NGX\_CORE\_MODULE and NGX\_HTTP\_MODULE
## Directives
Syntax : dynamic_conf dynamic_file time;
Default : -
Context : main
Set dynamic config file and interval system checked file changed.
Syntax : dynamic_log log_file level;
Default : -
Context : main
Set dynamic conf load log file and log level. If not configured, use cycle log as default.
Example:
dynamic_conf conf/nginx_dynamic.conf 10;
dynamic_log logs/error_dynamic.log info;
## API
### MAIN dynamic conf
**header file**
For using this API, You should include the header file as below:
#include "ngx_dynamic_conf.h"
**dynamic module define**
typedef struct {
ngx_str_t name;
void *(*create_conf)(ngx_conf_t *cf);
char *(*init_conf)(ngx_conf_t *cf, void *conf);
} ngx_dynamic_core_module_t;
dynamic conf module define as below
ngx_module_t ngx_test_module = {
NGX_MODULE_V1,
&ngx_test_module_ctx, /* module context */
ngx_test_commands, /* module directives */
NGX_CORE_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
(uintptr_t) &ngx_test_module_dctx, /* module dynamic context */
(uintptr_t) ngx_test_dcommands, /* module dynamic directives */
NGX_MODULE_V1_DYNAMIC_PADDING
};
**module dynamic context** struct define as above, **module dynamic directives** define use ngx\_command\_t. Use ngx\_dynamic\_core\_test\_module define in t/ngx\_dynamic\_conf\_test\_module.c as reference
**ngx\_dynamic\_conf\_parse**
ngx_int_t ngx_dynamic_conf_parse(ngx_conf_t *cf, unsigned init)
- return value:
- return NGX\_OK for successd, NGX\_ERROR for failed
- paras:
- cf : ngx\_conf\_t passed from ngx\_dynamic\_conf\_load_conf
- init : only ngx\_dynamic\_conf\_load\_conf set 1, otherwise set 0
This interface is supported for other dynamic conf module, such as ngx\_conf\_parse
**ngx\_dynamic\_regex\_compile**
typedef struct {
ngx_regex_t *regex;
ngx_str_t name;
} ngx_dynamic_regex_t;
ngx_dynamic_regex_t *ngx_dynamic_regex_compile(ngx_conf_t *cf,
ngx_regex_compile_t *rc);
- return value:
- return regex context
- paras:
- cf: ngx\_conf\_t passed in dynamic cmd handler
- rc: regex options
compile regex
**ngx\_get\_dconf**
void *ngx_get_dconf(ngx_module_t *m)
return NGX\_CORE\_MODULE dynamic config for module
### HTTP dynamic conf
**header file**
For using this API, You should include the header file as below:
#include "ngx_dynamic_conf.h"
**dynamic module define**
dynamic conf module define is same as MAIN dynamic conf
http dynamic conf context define as below:
typedef struct {
void *(*create_main_conf)(ngx_conf_t *cf);
char *(*init_main_conf)(ngx_conf_t *cf, void *conf);
void *(*create_srv_conf)(ngx_conf_t *cf);
char *(*init_srv_conf)(ngx_conf_t *cf, void *conf);
void *(*create_loc_conf)(ngx_conf_t *cf);
char *(*init_loc_conf)(ngx_conf_t *cf, void *conf);
} ngx_http_dynamic_module_t;
**notice:** http dynamic conf do not support merge
**ngx\_http\_get\_module\_main\_dconf**
void *ngx_http_get_module_main_dconf(ngx_http_request_t *r, ngx_module_t *m);
return http request main dynamic conf for module m
**ngx\_http\_get\_module\_srv\_dconf**
void *ngx_http_get_module_srv_dconf(ngx_http_request_t *r, ngx_module_t *m);
return http request srv dynamic conf for module m
**ngx\_http\_get\_module\_loc\_dconf**
void *ngx_http_get_module_loc_dconf(ngx_http_request_t *r, ngx_module_t *m);
return http request loc dynamic conf for module m
## Build
cd to NGINX source directory & run this:
./configure --add-module=/path/to/nginx-toolkit-module/
make && make install
## Example
See
- t/ngx\_dynamic\_conf\_test\_module.c as MAIN conf for usage of dynamic conf
- t/ngx\_http\_dynamic\_test\_module.c as HTTP conf for usage of http dynamic conf
**Build:**
./configure --with-debug --add-module=/path/to/nginx-toolkit-module/ --add-module=/path/to/nginx-toolkit-module/t
make && make install
**Configure:**
dynamic_conf conf/nginx_dynamic.conf 10;
dynamic_log logs/error_dynamic.log info;
http {
...
server {
...
location /dynamic_conf_test/ {
dynamic_conf_test;
}
}
}
**Dynamic Configure:**
dynamic_test_i 200;
dynamic_test_s hello_world;
http {
main_int 1000;
main_str gogogo;
#defult server
server {
srv_int 1;
srv_str default;
}
#wildcard_head
server {
srv_int 2;
srv_str wildcard_head;
serverid baidu;
server_name *.baidu.com;
}
#wildcard_tail
server {
srv_int 3;
srv_str wildcard_tail;
serverid google;
server_name www.google.*;
}
#hash
server {
srv_int 4;
srv_str hash;
serverid sina;
server_name sports.sina.com.cn;
location = / {
loc_int 1;
loc_str =/;
}
location / {
loc_int 2;
loc_str /;
}
location ^~ /test1/ {
loc_int 3;
loc_str ^~/test1/;
}
location ~* \.(gif|jpg|jpeg)$ {
loc_int 4;
loc_str ~*\.(gif|jpg|jpeg)$;
}
location /test {
loc_int 5;
loc_str /test;
}
}
#pcre
server {
srv_int 5;
srv_str pcre;
serverid test;
server_name ~^flv(?!.*(dl\.))[A-Za-z0-9]*\.test\.com$;
}
#multi
server {
srv_int 6;
srv_str multi;
serverid others;
server_name ~^flv(?!.*(dl\.))[A-Za-z0-9]*\.haha\.com$ www.sohu.com;
server_name *.qq.com;
}
}
**Test:**
- Main for dynamic config
get conf configured in dynamic config file for test module
curl -v 'http://127.0.0.1/dynamic_conf_test/test'
change config in dynamic config, the test api will return new config value after dynamic conf refresh
- Main for http
curl -v 'http://127.0.0.1/'
change config in http block of dynamic config, the test api will return new config value after dynamic conf refresh
- Server for http
- defult server
curl -v 'http://127.0.0.1/http_dynamic_test/test'
curl -v -H 'Host: github.com' 'http://127.0.0.1/http_dynamic_test/test'
- wildcard_head
curl -v -H 'Host: map.baidu.com' 'http://127.0.0.1/http_dynamic_test/test'
- wildcard_tail
curl -v -H 'Host: www.google.co.jp' 'http://127.0.0.1/http_dynamic_test/test'
- hash
curl -v -H 'Host: sports.sina.com.cn' 'http://127.0.0.1/http_dynamic_test/test'
- pcre
curl -v -H 'Host: flvdl7a8e4223.test.com' 'http://127.0.0.1/http_dynamic_test/test'
- multi
curl -v -H 'Host: flvdl7a8e4223.haha.com' 'http://127.0.0.1/http_dynamic_test/test'
curl -v -H 'Host: www.sohu.com' 'http://127.0.0.1/http_dynamic_test/test'
curl -v -H 'Host: v.qq.com' 'http://127.0.0.1/http_dynamic_test/test'
- Location for http
- no location
curl -v -H 'Host: flvdl7a8e4223.haha.com' 'http://127.0.0.1/'
- location = /
curl -v -H 'Host: sports.sina.com.cn' 'http://127.0.0.1/'
- location /
curl -v -H 'Host: sports.sina.com.cn' 'http://127.0.0.1/t'
- location ^~ /test1/
curl -v -H 'Host: sports.sina.com.cn' 'http://127.0.0.1/test1/123'
- ~* \.(gif|jpg|jpeg)$
curl -v -H 'Host: sports.sina.com.cn' 'http://127.0.0.1/test/123.gif'
- /test
curl -v -H 'Host: sports.sina.com.cn' 'http://127.0.0.1/test/123'
@@ -1,157 +0,0 @@
# Module ngx-dynamic-resolver-module
---
## Instructions
System will resolver domain in dynamic resolver every few seconds configured.
The module will return addr whose domain is resolved in dynamic resolver, otherwise, the module will add domain into dynamic resolver, resolv domain by event resolver, and call callback when resolved.
## Directives
Syntax : dynamic_refresh_interval time;
Default : dynamic_refresh_interval 5m;
Context : events
Set time interval for DNS query frequency, 0 for shutdown this function.
Syntax : dynamic_domain_buckets number;
Default : dynamic_domain_buckets 101;
Context : events
Bucket for dynamic resolver domain hash table. Use prime key is Recommended.
Example:
events {
resolver 192.168.84.254 valid=1m;
dynamic_refresh_interval 5m;
dynamic_domain_buckets 1001;
}
## API
**header file**
For using this API, You should include the header file as below:
#include "ngx_dynamic_resolver.h"
**start resolver**
void ngx_dynamic_resolver_start_resolver(ngx_str_t *domain,
ngx_dynamic_resolver_handler_pt h, void *data);
- return value:
- None
- paras:
- domain: domain for DNS query
- h : callback handler
- data : data for callback
h's protype is:
typedef void (* ngx_dynamic_resolver_handler_pt)(void *data,
struct sockaddr *sa, socklen_t socklen);
- return value:
- None
- paras:
- data : user private data set in ngx\_dynamic\_resolver\_start\_resolver
- sa : sock address get
- socklen: sock address len, 0 for get none address
**gethostbyname**
socklen_t ngx_dynamic_resolver_gethostbyname(ngx_str_t *domain, struct sockaddr *sa);
- return value:
- socklen for successd
- 0 for failed
- paras:
- domain: domain for query
- sa : sock address get
**add domain**
void ngx_dynamic_resolver_add_domain(ngx_str_t *domain);
- return value:
- None
- paras:
- domain: domain for query
**del domain**
void ngx_dynamic_resolver_del_domain(ngx_str_t *domain);
- return value:
- None
- paras:
- domain: domain for DNS query
## Build
cd to NGINX source directory & run this:
./configure --add-module=/path/to/nginx-toolkit-module/
make && make install
## Example
See t/ngx\_dynamic\_resolver\_test\_module.c as reference
Build:
./configure --with-debug --add-module=/path/to/nginx-toolkit-module/ --add-module=/path/to/nginx-toolkit-module/t
make && make install
Configure:
events {
resolver 192.168.84.254 valid=1m;
dynamic_refresh_interval 5m;
}
http {
...
server {
...
location /dynamic_resolver_test/ {
dynamic_resolver_test 192.168.84.4 www.test1.com;
}
}
}
Test:
- sync resolver
curl -v 'http://127.0.0.1/dynamic_resolver_test/resolver?domain=www.test1.com&sync=1'
- start resolver
curl -v 'http://127.0.0.1/dynamic_resolver_test/resolver?domain=www.test.com'
- del domain
curl -XDELETE -v 'http://127.0.0.1/dynamic_resolver_test/resolver?domain=www.test.com'
@@ -1,126 +0,0 @@
# Module ngx-event-resolver-module
---
## Instructions
Common resovler in event modules, just like http resolver, stream resolver in nginx, but can be used by http modules, stream modules and other modules
## Directives
Syntax : resolver address ... [valid=time] [ipv6=on|off];
Default : -
Context : events
Configures name servers used to resolve names into addresses, for example:
> resolver 127.0.0.1 [::1]:5353;
An address can be specified as a domain name or IP address, and an optional port. If port is not specified, the port 53 is used. Name servers are queried in a round-robin fashion.
By default, nginx will look up both IPv4 and IPv6 addresses while resolving. If looking up of IPv6 addresses is not desired, the ipv6=off parameter can be specified.
By default, nginx caches answers using the TTL value of a response. The optional valid parameter allows overriding it:
> resolver 127.0.0.1 [::1]:5353 valid=30s;
Syntax : resolver_timeout time;
Default : resolver_timeout 60s;
Context : events
Sets a timeout for name resolution, for example:
> resolver\_timeout 5s;
Example:
events {
resolver 192.168.84.254 valid=20s;
resolver_timeout 10s;
}
## API
**header file**
For using this API, You should include the header file as below:
#include "ngx_event_resolver.h"
**resolver a domain**
void ngx_event_resolver_start_resolver(ngx_str_t *domain,
ngx_event_resolver_handler_pt h, void *data);
- return value:
None
- paras:
- domain: domain for resolving
- h : callback handler
- data : data for callback
h's protype is:
typedef void (* ngx_event_resolver_handler_pt)(void *data,
ngx_resolver_addr_t *addrs, ngx_uint_t naddrs);
- return value:
None
- paras:
- data : user private data set in ngx\_event\_resolver\_start\_resolver
- addrs : addrs resolv by DNS
- naddrs: number of addrs resolv by DNS
## Build
cd to NGINX source directory & run this:
./configure --add-module=/path/to/nginx-toolkit-module/
make && make install
## Example
See t/ngx\_event\_resolver\_test\_module.c as reference
Build:
./configure --with-debug --add-module=/path/to/nginx-toolkit-module/ --add-module=/path/to/nginx-toolkit-module/t
make && make install
Configure:
events {
resolver 192.168.84.254 114.114.114.114 valid=20s;
resolver_timeout 10s;
}
http {
...
server {
...
location /event_resolver_test/ {
event_resolver_test;
}
}
}
Install bind server
/path/to/nginx-toolkit-module/t/dns_install.sh
modify /var/named/test.com.zone dns ip address to fit your enviroment
Test:
- add domain for resolving
curl -v "http://127.0.0.1/event_resolver_test/domain?domain=www.test.com"
@@ -1,112 +0,0 @@
# Module ngx-event-timer-module
---
## Instructions
Independent timer for nginx
## Directives
Syntax : worker_timers number;
Default : worker_timers 1024;
Context : events
Sets the maximum number of timers that can be used in worker process.
events {
...
worker_timers 1024;
}
## API
**header file**
For using this API, You should include the header file as below:
#include "ngx_event_timer_module.h"
**registe domain**
ngx_int_t ngx_event_timer_add_timer(ngx_msec_t tv,
ngx_timer_handler_pt h, void *data);
- return value:
return timerid for successd, NGX_ERROR for failed.
Error:
- h is NULL
- not enough timer for assigned
- paras:
- tv : timer interval to trigger handler
- h : timer handler
- data : data of h para
Register a timer handler, timer interval is tv, measured by millisecond. When timer triggered, h will be called, using data as function parameters.
h's protype is:
typedef void (* ngx_timer_handler_pt)(void *data);
- return value:
None
- paras:
- data: data set in ngx\_event\_timer\_add\_timer, for paras transmit
**del timer**
void ngx_event_timer_del_timer(ngx_uint_t timerid);
- return value:
void
- paras:
- timerid: return by ngx\_event\_timer\_add\_timer
Deregister timer handler.
## Build
cd to NGINX source directory & run this:
./configure --add-module=/path/to/nginx-toolkit-module/
make && make install
## Example
See t/ngx\_event\_timer\_test\_module.c as reference
Build:
./configure --add-module=/path/to/nginx-toolkit-module/t/ --add-module=/path/to/nginx-toolkit-module/
make && make install
Configure:
location /event_timer_test/ {
event_timer_test;
}
Test:
add timer
curl -XPOST -v "http://127.0.0.1/event_timer_test/timer?time=3s"
Test module will start a timer when process init. It will log after 3 seconds, set in args:
2016/12/10 18:48:37 [error] 20295#0: event timer test timer handler
del timer
curl -XDELETE -v "http://127.0.0.1/event_timer_test/timer?timerid=1"
Test module will stop a timer whose timerid is 1
@@ -1,112 +0,0 @@
# ngx-map
---
## Instructions
A map implement use ngx_rbtree
## API
**header file**
For using this API, You should include the header file as below:
#include "ngx_map.h"
**structure**
typedef struct {
ngx_rbtree_t rbtree;
ngx_rbtree_node_t sentinel;
ngx_map_hash_pt hash;
ngx_cmp_pt cmp;
} ngx_map_t;
typedef struct {
ngx_rbtree_node_t rn;
intptr_t raw_key;
ngx_map_t *map;
} ngx_map_node_t;
use ngx\_map\_t instance as a map, use ngx\_map\_init to initial. Put ngx\_map\_node\_t in your structure, and set raw_key, then you can insert your node in the map. Use raw_key, you can find node in map, or delete node in map.
**ngx\_map\_init**
void ngx_map_init(ngx_map_t *map, ngx_map_hash_pt hash, ngx_cmp_pt cmp)
Interface for init a map, hash is hash func handler to calculate hashkey for rawkey, cmp is compare func handler for compare two raw_key, when raw_keys' hashkey is same.
We support base hash and cmp func as below:
/* ngx_str_t */
ngx_rbtree_key_t ngx_map_hash_str(intptr_t key)
int ngx_cmp_str(intptr_t key1, intptr_t key2)
/* ngx_uint_t */
ngx_rbtree_key_t ngx_map_hash_uint(intptr_t key)
int ngx_cmp_uint(intptr_t key1, intptr_t key2)
/* ngx_int_t */
ngx_rbtree_key_t ngx_map_hash_int(intptr_t key)
int ngx_cmp_int(intptr_t key1, intptr_t key2)
User also can use own hash and cmp func with protype below:
/*
* key: key for map node
*/
typedef ngx_rbtree_key_t (* ngx_map_hash_pt)(intptr_t key);
/*
* if key1 < key2, return -1
* if key1 = key2, return 0
* if key1 > key2, return 1
*/
typedef int (* ngx_cmp_pt)(intptr_t key1, intptr_t key2);
**ngx\_map\_empty**
#define ngx_map_empty(map) (map->rbtree.root == map->rbtree.sentinel)
If map is empty, return 1, else, return false
**ngx\_map\_begin**
ngx_map_node_t *ngx_map_begin(ngx_map_t *map)
Return the mininum key node of map
**ngx\_map\_rbegin**
ngx_map_node_t *ngx_map_rbegin(ngx_map_t *map)
Return the maxinum key node of map
**ngx\_map\_next**
ngx_map_node_t *ngx_map_next(ngx_map_node_t *n)
Return the next node of n, if n is the maximum key node in map, return NULL
**ngx\_map\_prev**
ngx_map_node_t *ngx_map_prev(ngx_map_node_t *n)
Return the previous node of n, if n is the mininum key node in map, return NULL
**ngx\_map\_insert**
void ngx_map_insert(ngx_map_t *map, ngx_map_node_t *node, ngx_flag_t covered)
Interface for insert a node in map. New node will force replace old node in map if raw_key is same when covered set to 1. Otherwise, new node can't insert in map
**ngx\_map\_delete**
void ngx_map_delete(ngx_map_t *map, intptr_t key)
Interface for delete a node in map, if node's raw_key equal to key in paras. If node is not exist, do nothing.
**ngx\_map\_find**
ngx_map_node_t *ngx_map_find(ngx_map_t *map, intptr_t key)
Interface for find a node in map, if node's raw_key equal to key in paras. If node is not exist, return NULL.
@@ -1,19 +0,0 @@
# ngx-poold
---
## Instructions
Pool debug for check whether pool is destroy or destroy twice
## API
**NGX\_CREATE\_POOL**
#define NGX_CREATE_POOL(size, log)
Replace NGX\_CREATE\_POOL instead of ngx\_create\_pool, it will record position creating pool. must use with NGX\_DESTROY\_POOL
**NGX\_DESTROY\_POOL**
#define NGX_DESTROY_POOL(pool)
Replace NGX\_DESTROY\_POOL instead of ngx\_destroy\_pool, it will delete info which NGX\_CREATE\_POOL add. If pool not register in record pool, the pool will not destroy, and log delete twice log and position call the NGX\_DESTROY\_POOL.
@@ -1,40 +0,0 @@
# ngx-rbuf
---
## Instructions
A recycled chainbuf for nginx
## API
**header file**
For using this API, You should include the header file as below:
#include "ngx_rbuf.h"
**ngx\_get\_chainbuf**
ngx_chain_t *ngx_get_chainbuf(size_t size, ngx_flag_t alloc_rbuf);
Interface for get a chain with buf, if alloc\_rbuf is set to 1, rbuf will alloc a buf with size set in paras; if alloc\_rbuf is set to 0, size is nonsense here.
Notice, for recycled buf better memory manager, the size should be same for the same usage. Such as chunk size in nginx rtmp module.
**ngx\_put\_chainbuf**
void ngx_put_chainbuf(ngx_chain_t *cl);
Interface for recycle chain with buf alloc from rbuf
**ngx\_rbuf\_statee**
ngx_chain_t *ngx_rbuf_state(ngx_http_request_t *r);
Interface for query rbuf state, result:
ngx_rbuf_nalloc_node: 1
ngx_rbuf_nalloc_buf: 1
ngx_rbuf_nfree_buf: 1
ngx_rbuf_nalloc_chain: 3
ngx_rbuf_nalloc_chain: 3
@@ -1,89 +0,0 @@
# ngx-toolkit-misc
---
## Instructions
Misc toolkit functions
## API
**header file**
For using this API, You should include the header file as below:
#include "ngx_toolkit_misc.h"
**structure**
/*
* scheme://[user@]host[:port]/path[?args][#fragment]
*/
typedef struct {
ngx_str_t scheme;
ngx_str_t user;
ngx_str_t host;
ngx_str_t port;
ngx_str_t path;
ngx_str_t args;
ngx_str_t fragment;
ngx_str_t host_with_port; /* host[:port] */
ngx_str_t uri_with_args; /* /path[?args][#fragment] */
} ngx_request_url_t;
**ngx\_parse\_request\_url**
ngx_int_t ngx_parse_request_url(ngx_request_url_t *request_url, ngx_str_t *url);
parse request url format as: scheme://[user@]host[:port]/path[?args][#fragment]
- return value:
- NGX_OK: parse success
- NGX_ERROR: request url format error
- paras:
- request_url: url parse result return to user, all paras in request url is segment point to url
- url: request url for parse
**ngx\_request\_port**
in_port_t ngx_request_port(ngx_str_t *scheme, ngx_str_t *port);
convert port to in_port_t according to scheme and port
- return value:
- If port is set to correct number range in [1, 65535], return port
- If port is set to non correct value, return 0
- If port is not set, return default value for scheme:
- 80 for http
- 443 for https
- 1935 for rtmp
- 0 for others now
- values:
- scheme : sheme string like http https or rtmp
- port : port for convert to in_port_t
**ngx\_md5\_file**
#define NGX_MD5KEY_LEN 32
ngx_int_t ngx_md5_file(ngx_fd_t fd, u_char md5key[NGX_MD5KEY_LEN]);
calculating file md5key as md5sum in shell
- return value:
- NGX_OK: calculating success
- NGX_ERROR: calculating error such as file is not exist
- paras:
- fd: file desc for calculating md5key
- md5key: md5key result