Slide 1

Slide 1 text

INSIDE NGINX CONF & MODULE David Zhang

Slide 2

Slide 2 text

ngx_agenda ● ngx_introduction ● ngx_conf ● ngx_module ● ngx_http ● ngx_reference

Slide 3

Slide 3 text

ngx_introduction

Slide 4

Slide 4 text

ngx_introduction ● Nginx (pronounced "engine x") ● HTTP, HTTPS, SMTP, IMAP, POP3 ● Igor Sysoev ○ Bauman Moscow State Technical University ○ Mail.ru ○ CTO of NGINX Inc. ● nginx.net ● NGINX Inc. ● http://hg.nginx.org/nginx/ (Mecurial) ● https://github.com/nginx/nginx (Mirror)

Slide 5

Slide 5 text

ngx_introduction

Slide 6

Slide 6 text

ngx_introduction ● Multi-process ○ Master ○ Worker ○ Cache loader ○ Cache manager ● Single-threaded ● Highly modular ● Event-driven ● Non-blocking

Slide 7

Slide 7 text

ngx_conf

Slide 8

Slide 8 text

ngx_conf_introduction ● nginx.conf ● Lua/OpenResty ● nginScript ○ http://nginx.org/en/docs/njs_about.html ○ https://www.nginx.com/blog/introduction-nginscript/ “Lua is a powerful scripting language. It, however, remains fairly niche in terms of adoption and is not typically found in the “skillset toolbox” of the frontend developer or DevOps engineer. The goal of nginScript is to provide programmatic configuration solutions to the widest possible community by using a popular programming language.”

Slide 9

Slide 9 text

ngx_conf_syntax ● ngx_conf is a DSL (Domain Specific Language) ○ Blocks ■ main ● event ● http ● mail ○ Directives ○ `if` ■ Directive ■ Not a condition ■ If Is Evil https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/

Slide 10

Slide 10 text

ngx_conf_args ● Flag: on/off ● Str ● Str Array ● Keyval Array ● Num ● Size/Off: 1G 100M ● Msec/Sec ● Bufs ● Enum ● Bitmask ● Access ● Path

Slide 11

Slide 11 text

ngx_conf_vars ● $arg_ ● $cookie_ ● $uri ● ... ● http://nginx.org/en/docs/varindex.html

Slide 12

Slide 12 text

ngx_module

Slide 13

Slide 13 text

core/ngx_module.h struct ngx_module_s { char *name; ... ngx_command_t *commands; ngx_uint_t type; ... ngx_int_t (*init_master)(ngx_log_t *log); ngx_int_t (*init_module)(ngx_cycle_t *cycle); ngx_int_t (*init_process)(ngx_cycle_t *cycle); ngx_int_t (*init_thread)(ngx_cycle_t *cycle); void (*exit_thread)(ngx_cycle_t *cycle); void (*exit_process)(ngx_cycle_t *cycle); void (*exit_master)(ngx_cycle_t *cycle); ... } ngx_module

Slide 14

Slide 14 text

ngx_module ● Core ● Event ● Http ● Mail ● Stream

Slide 15

Slide 15 text

ngx_command core/ngx_conf_file.h struct ngx_command_s { ngx_str_t name; ngx_uint_t type; char *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); ngx_uint_t conf; ngx_uint_t offset; void *post; }; core/ngx_core.h typedef struct ngx_command_s ngx_command_t;

Slide 16

Slide 16 text

ngx_http_module

Slide 17

Slide 17 text

ngx_http_module http/ngx_http.c ngx_module_t ngx_http_module = { NGX_MODULE_V1, &ngx_http_module_ctx, /* module context */ ngx_http_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 */ NGX_MODULE_V1_PADDING };

Slide 18

Slide 18 text

ngx_http_module http/ngx_http.c static ngx_command_t ngx_http_commands[] = { { ngx_string("http"), NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS, ngx_http_block, 0, 0, NULL }, ngx_null_command };

Slide 19

Slide 19 text

ngx_http_module http/ngx_http_config.h typedef struct { ngx_int_t (*preconfiguration)(ngx_conf_t *cf); ngx_int_t (*postconfiguration)(ngx_conf_t *cf); 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 *(*merge_srv_conf)(ngx_conf_t *cf, void *prev, void *conf); void *(*create_loc_conf)(ngx_conf_t *cf); char *(*merge_loc_conf)(ngx_conf_t *cf, void *prev, void *conf); } ngx_http_module_t;

Slide 20

Slide 20 text

ngx_http_rewrite_module http/modules/ngx_http_rewrite_module.c static ngx_http_module_t ngx_http_rewrite_module_ctx = { NULL, /* preconfiguration */ ngx_http_rewrite_init, /* postconfiguration */ NULL, /* create main configuration */ NULL, /* init main configuration */ NULL, /* create server configuration */ NULL, /* merge server configuration */ ngx_http_rewrite_create_loc_conf, /* create location configuration */ ngx_http_rewrite_merge_loc_conf /* merge location configuration */ };

Slide 21

Slide 21 text

ngx_http_rewrite_module http/modules/ngx_http_rewrite_module.c { ngx_string("if"), NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_BLOCK|NGX_CONF_1MORE, ngx_http_rewrite_if, NGX_HTTP_LOC_CONF_OFFSET, 0, NULL },

Slide 22

Slide 22 text

ngx_http_rewrite_module http/modules/ngx_http_rewrite_module.c static char * ngx_http_rewrite_if(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_http_rewrite_loc_conf_t *lcf = conf; ...

Slide 23

Slide 23 text

ngx_reference

Slide 24

Slide 24 text

ngx_reference 1. https://w3techs.com/technologies/details/ws-nginx/all/all 2. NGINX 事件驱动机制 3. http://nginx.org/en/docs/njs_about.html 4. Why does one NGINX worker take all the load? 5. http://andremouche.github.io/nginx/nginx-internal-basic-architecture.html

Slide 25

Slide 25 text

ngx_thank_you