auto/ # 编译脚本
src/
core/ # 基础类型与函数:字符串、数组、日志、内存池等
event/ # 事件驱动核心
modules/ # 事件通知实现:epoll、kqueue、select 等
http/ # HTTP 核心模块与公共代码
modules/ # 其他 HTTP 模块
v2/ # HTTP/2 支持
mail/ # Mail 模块
os/ # 平台相关代码
unix/
win32/
stream/ # Stream 模块
include/ # 头文件
每个源文件开头必须包含:
#include
#include
HTTP 相关还需:
#include
Mail 相关:
#include
Stream 相关:
#include
ngx_int_t
与 ngx_uint_t
分别对应 intptr_t
与 uintptr_t
。宏 | 含义 |
---|---|
NGX_OK |
成功 |
NGX_ERROR |
失败 |
NGX_AGAIN |
操作未完成,需要重试 |
NGX_DECLINED |
操作被拒绝(非错误,如配置禁用) |
NGX_BUSY |
资源不可用 |
NGX_DONE |
操作完成或在其他地方继续,也可做成功替代码 |
NGX_ABORT |
操作中止,也可做失败替代码 |
ngx_errno
/ ngx_socket_errno
获取最近系统或 socket 错误。ngx_set_errno()
/ ngx_set_socket_errno()
设置错误。ngx_log_error()
添加系统错误文本。ngx_int_t ngx_my_kill(ngx_pid_t pid, ngx_log_t *log, int signo) {
ngx_err_t err;
if (kill(pid, signo) == -1) {
err = ngx_errno;
ngx_log_error(NGX_LOG_ALERT, log, err,
"kill(%P, %d) failed", pid, signo);
if (err == NGX_ESRCH) return 2;
return 1;
}
return 0;
}
typedef struct {
size_t len;
u_char *data;
} ngx_str_t;
len
字符长度;data
指向字符数组。(通常非 null 终止)包装标准 C 接口:
ngx_strcmp, ngx_strncmp, ngx_strstr, ngx_strlen,
ngx_strchr, ngx_memcmp, ngx_memset, ngx_memcpy, ngx_memmove
Nginx 专用:
ngx_memzero, ngx_explicit_memzero, ngx_cpymem, ngx_movemem,
ngx_strlchr, ngx_strlow, ngx_strcasecmp, ngx_strncasecmp
初始化宏:
ngx_string(text), ngx_null_string,
ngx_str_set(str, text), ngx_str_null(str)
ngx_sprintf
, ngx_snprintf
, ngx_slprintf
, ngx_vslprintf
, ngx_vsnprintf
支持 %O, %T, %z, %i, %p, %V, %s, %*s
等。ngx_atoi
, ngx_atosz
, ngx_atoof
, ngx_atotm
, ngx_atofp
, ngx_hextoi
。基于 PCRE,头文件:
#include
编译时需宏保护:
#if (NGX_PCRE)
ngx_regex_compile_t rc;
rc.pattern = ngx_string("(\d+)");
rc.pool = cf->pool;
if (ngx_regex_compile(&rc) != NGX_OK) {
ngx_conf_log_error(...); return NGX_CONF_ERROR;
}
ngx_regex_exec(rc.regex, &input, captures, size);
#endif
ngx_time_t
、ngx_tm_t
(映射 struct tm
/ SYSTEMTIME
)ngx_cached_err_log_time
, ngx_cached_http_log_time
, ngx_cached_syslog_time
, ngx_cached_http_time
, ngx_cached_http_log_iso8601
ngx_time()
, ngx_timeofday()
, ngx_gettimeofday()
, ngx_time_update()
ngx_gmtime()
, ngx_libc_gmtime()
, ngx_localtime()
, ngx_libc_localtime()
, ngx_http_time()
, ngx_http_cookie_time()
typedef struct {
void *elts; // 元素起始地址
ngx_uint_t nelts; // 元素数量
size_t size; // 元素大小
ngx_uint_t nalloc; // 分配容量
ngx_pool_t *pool;
} ngx_array_t;
ngx_array_create(pool, n, size)
ngx_array_push(a)
/ ngx_array_push_n(a, n)
typedef struct {
ngx_list_part_t *last;
ngx_list_part_t part;
size_t size;
ngx_uint_t nalloc;
ngx_pool_t *pool;
} ngx_list_t;
ngx_list_create
, ngx_list_init
, ngx_list_push
, 迭代…双向链表,入队/出队:
ngx_queue_init, ngx_queue_insert_head/tail,
ngx_queue_remove, ngx_queue_split, ngx_queue_add,
ngx_queue_head, ngx_queue_last, ngx_queue_data
#include
ngx_rbtree_init
, ngx_rbtree_insert
, ngx_rbtree_delete
。#include
ngx_hash_keys_array_init
, ngx_hash_add_key
;再构建 ngx_hash_init
;查找 ngx_hash_find
。ngx_hash_combined_t
, NGX_HASH_WILDCARD_KEY
, ngx_hash_wildcard_init
, ngx_hash_find_combined
。ngx_alloc, ngx_calloc, ngx_memalign, ngx_free
ngx_create_pool, ngx_destroy_pool,
ngx_palloc, ngx_pcalloc, ngx_pnalloc, ngx_pfree
ngx_alloc_chain_link
, ngx_free_chain
ngx_pool_cleanup_add
ngx_shared_memory_add -> ngx_shm_zone_t
ngx_slab_pool_t
,ngx_slab_alloc
, ngx_slab_calloc
, ngx_slab_free
ngx_shmtx_lock
, ngx_shmtx_unlock
ngx_log_t
,支持 stderr、file、syslog、memory。NGX_LOG_EMERG
, NGX_LOG_ALERT
, …, NGX_LOG_DEBUG
NGX_LOG_DEBUG_CORE
, NGX_LOG_DEBUG_ALLOC
, …ngx_log_error
, ngx_log_debug0~8
ngx_cycle_t
存储运行时上下文字段:data
, handler
, write
, active
, ready
, timer
, timedout
, posted
…
ngx_get_connection
-> c->read
, c->write
ngx_handle_read_event
, ngx_handle_write_event
ngx_add_timer(ev, msec)
, ngx_del_timer(ev)
ngx_event_timer_rbtree
ngx_post_event(ev, q)
, ngx_delete_posted_event
-> ngx_event_process_posted
ngx_event_find_timer
ngx_thread_mutex_t
, ngx_thread_cond_t
ngx_thread_pool_t
ngx_thread_task_t
, ngx_thread_task_post
config
脚本和源码config
设置变量:ngx_module_type
, ngx_module_name
, ngx_module_srcs
, …--add-module
/ --add-dynamic-module
struct ngx_module_s {
void *ctx;
ngx_command_t *commands;
ngx_uint_t type;
ngx_int_t (*init_master)(ngx_log_t*);
ngx_int_t (*init_module)(ngx_cycle_t*);
ngx_int_t (*init_process)(ngx_cycle_t*);
// ... exit thread/process/master
};
struct ngx_command_s {
ngx_str_t name;
ngx_uint_t type;
char *(*set)(ngx_conf_t*, ngx_command_t*, void*);
ngx_uint_t conf;
ngx_uint_t offset;
void *post;
};
NGX_CONF_TAKE1..7
, NGX_CONF_BLOCK
, NGX_CONF_FLAG
, context 标志等ngx_conf_set_flag_slot
, _set_str_slot
, _set_num_slot
, _set_size_slot
, _set_keyval_slot
, _set_enum_slot
, _set_bitmask_slot
…ngx_event_accept
-> ngx_http_init_connection
ngx_http_wait_request_handler
ngx_http_process_request_line
(解析请求行)ngx_http_process_request_headers
ngx_http_core_run_phases
ngx_http_finalize_request
ngx_http_finalize_connection
字段:connection
, ctx
, main_conf
, srv_conf
, loc_conf
, pool
, headers_in/out
, uri
, args
, method
, http_version
, phase_handler
, subrequests
, count
, …
POST_READ -> SERVER_REWRITE -> FIND_CONFIG -> REWRITE -> POST_REWRITE -> PREACCESS -> ACCESS -> POST_ACCESS -> PRECONTENT -> CONTENT -> LOG
NGX_OK
, NGX_DECLINED
, NGX_AGAIN
, NGX_DONE
, 或 HTTP 状态码ngx_http_variable_value_t
表示变量值ngx_http_get_indexed_variable
, _get_flushed_variable
, _get_variable
ngx_http_add_variable
ngx_http_compile_complex_value
,运行时:ngx_http_complex_value
ngx_http_internal_redirect
ngx_http_named_location
ngx_http_subrequest
,post_subrequest
回调ngx_http_read_client_request_body
, ngx_http_discard_request_body
ngx_http_read_unbuffered_request_body
ngx_http_top_request_body_filter
ngx_http_send_header
-> ngx_http_top_header_filter
ngx_http_output_filter
-> ngx_http_top_body_filter
ngx_http_upstream_module
提供通用上游支持ngx_http_upstream_srv_conf_t
配置上下文init_upstream
, init_peer
-> get
, free
, notify
, set_session
, save_session
风格:80 列、4 空格缩进、无制表符与行尾空格、对齐声明、结构清晰、函数原型、注释风格、宏命名等。
陷阱: