Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ngx_share: conf & module

David Zhang
August 10, 2018
19

ngx_share: conf & module

Brief introduction to Nginx's configuration and modules.

David Zhang

August 10, 2018
Tweet

Transcript

  1. 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)
  2. ngx_introduction • Multi-process ◦ Master ◦ Worker ◦ Cache loader

    ◦ Cache manager • Single-threaded • Highly modular • Event-driven • Non-blocking
  3. 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.”
  4. 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/
  5. ngx_conf_args • Flag: on/off • Str • Str Array •

    Keyval Array • Num • Size/Off: 1G 100M • Msec/Sec • Bufs • Enum • Bitmask • Access • Path
  6. ngx_conf_vars • $arg_ • $cookie_ • $uri • ... •

    http://nginx.org/en/docs/varindex.html
  7. 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
  8. 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;
  9. 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 };
  10. 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;
  11. 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 */ };
  12. 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