nginx: writing your first module

4,234
-1

Published on

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,234
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
138
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

nginx: writing your first module

  1. 1. FEERSUM ENJINN nginx: inside
  2. 2. FEATURES event driven non-blocking modular
  3. 3. CORE MODULES event http main
  4. 4. EASY TO REMEMBER
  5. 5. BASE MODULES HTTP Core HTTP Upstream HTTP Access HTTP Auth Basic HTTP Auto Index Browser Charset FastCGI Gzip HTTP Headers HTTP Referer HTTP Limit Zone HTTP Limit Requests Log HTTP Proxy Rewrite SSI User ID
  6. 6. OPTIONAL HTTP Addition Embedded Perl FLV Gzip Precompression Random Index GeoIP Real IP SSL Stub Status WebDAV Google Perftools XSLT Secure Link Image Filter
  7. 7. EASY EASY (SO, NO NEED TO LEARN IT)
  8. 8. PROCESSES
  9. 9. NGINX MASTER Monitors worker processes Does all signal handlind work -INT — exit -HUP — reconfigure -USR1 — log rotate
  10. 10. NGINX WORKER Processes client's requests Performs master's command
  11. 11. CACHE MANAGER/LOADER Loads content from files Drops stale (expired) content
  12. 12. HTTP REQUEST
  13. 13. HANDLING REQUEST receive data parse the request find virtual server find location run through the phase handlers generate the response filter response headers filter response body send out the result to the client
  14. 14. HANDLING PHASES typedef enum { NGX_HTTP_POST_READ_PHASE, NGX_HTTP_SERVER_REWRITE_PHASE, NGX_HTTP_FIND_CONFIG_PHASE, NGX_HTTP_REWRITE_PHASE, NGX_HTTP_POST_REWRITE_PHASE, NGX_HTTP_PREACCESS_PHASE, NGX_HTTP_ACCESS_PHASE, NGX_HTTP_POST_ACCESS_PHASE, NGX_HTTP_TRY_FILES_PHASE, NGX_HTTP_CONTENT_PHASE, NGX_HTTP_LOG_PHASE } ngx_http_phases;
  15. 15. YOUR OWN MODULE
  16. 16. HELLO WORLD
  17. 17. STEP 1 <ul>COMMANDS (CONFIG DIRECTIVES) </ul>
  18. 18. STEP 1 static ngx_command_t ngx_http_hello_commands[] = { { ngx_string(&quot;hello&quot;), NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS, ngx_http_hello, 0, 0, NULL }, ngx_null_command };
  19. 19. STEP 2 Module context
  20. 20. STEP 2 static ngx_http_module_t ngx_http_hello_module_ctx = { NULL, /* preconfiguration */ NULL, /* postconfiguration */ NULL, /* create main configuration */ NULL, /* init main configuration */ NULL, /* create server configuration */ NULL, /* merge server configuration */ NULL, /* create location configurtion */ NULL /* merge location configuration */ }
  21. 21. STEP 3 <ul>Module definition </ul>
  22. 22. STEP 3 ngx_module_t ngx_http_hello_module = { NGX_MODULE_V1, &ngx_http_hello_module_ctx, /* module context */ ngx_http_hello_commands, /* module directives */ NGX_HTTP_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 };
  23. 23. STEP 4 <ul>Setting handlers up </ul>
  24. 24. STEP 4 static char * ngx_http_hello(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_http_core_loc_conf_t *clcf; clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); clcf->handler = ngx_http_hello_handler; return NGX_CONF_OK; }
  25. 25. STEP 5 Your handler
  26. 26. STEP 5 static ngx_int_t ngx_http_hello_handler(ngx_http_request_t *r) { /* do it yourself */ return NGX_OK; }
  27. 27. BUILDING <ul>Config file ngx_addon_name=ngx_http_hello_module HTTP_MODULES=&quot;$HTTP_MODULES ngx_http_hello_module&quot; NGX_ADDON_SRCS=&quot;$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_hello_module.c&quot; Configure nginx ./configure --add-module=path/to/your/new/module/directory </ul>
  28. 28. COMPILE
  29. 29. DEBUG
  30. 30. Valgring Strace GDB TOOLS
  31. 31. Does it work? You are sooo coool. Enjoy.

×