4. Static vs. Dynamic modulesStatic vs. Dynamic modules
● Dynamic modules:
– can be build after Nginx is in production
– can be loaded or disabled by configuration
– when disabled, they are not loaded into memory
– they are loaded using standard dlopen on every reread
of the configuration
load_module modules/<MODULE-NAME>.so;
14. Common return codesCommon return codes
● NGX_OK — Operation succeeded
● NGX_ERROR — Operation failed
● NGX_AGAIN — Operation incomplete; call the function
again.
● NGX_DECLINED — Operation rejected, for example,
because it is disabled in the configuration. This
is never an error.
● NGX_BUSY — Resource is not available.
● NGX_DONE — Operation complete or continued
elsewhere
● NGX_ABORT — Function was aborted. Also used as an
alternative error code.
18. Module directivesModule directives
NGX_HTTP_MAIN_CONF - should be used in main section
NGX_HTTP_SRV_CONF - should be used in server section
NGX_HTTP_LOC_CONF - should be used in location
section
NGX_HTTP_UPS_CONF - should be used in upstream
section
NGX_CONF_NOARGS - no arguments
NGX_CONF_TAKE1 - 1 argument
NGX_CONF_TAKE2 - 2 arguments
....
NGX_CONF_TAKE7 - 7 arguments
19. Module directivesModule directives
NGX_CONF_TAKE12 - 1 or 2 arguments
NGX_CONF_TAKE13 - 1 or 3 arguments
NGX_CONF_TAKE23 - 2 or 3 arguments
NGX_CONF_TAKE123 - 1, 2 or 3 arguments
NGX_CONF_TAKE1234 - 1, 2, 3 or 4 arguments
NGX_CONF_FLAG - "on" or "off"
NGX_CONF_1MORE - one or more arguments
NGX_CONF_2MORE - two or more arguments
definitions: src/core/ngx_conf_file.h
26. Module contextModule context
● Pre configuration
● Post configuration
● Creating the main conf
● Initializing the main conf
● Creating the server conf
● Merging it with the main conf
● Creating the location conf
● Merging it with the server conf
33. StringsStrings
ngx_memzero() — Fills memory with zeroes
ngx_cpymem() — Does the same as ngx_memcpy(), but
returns the final destination address. This one is
handy for appending multiple strings in a row.
ngx_movemem() — Does the same as ngx_memmove(),
but returns the final destination address.
ngx_strlchr() — Searches for a character in a
string, delimited by two pointers
src/core/ngx_string.h
35. StringsStrings
ngx_string(text) — static initializer for the
ngx_str_t type from the C string literal text
ngx_null_string — static empty string initializer
for the ngx_str_t type
ngx_str_set(str, text) — initializes string str of
ngx_str_t * type with the C string literal text
ngx_str_null(str) — initializes string str of
ngx_str_t * type with the empty string
38. TimeTime
ngx_time() and ngx_timeofday() -
macros return the current time value
in seconds
They are the preferred way to access
the cached time value.
Updating the time:
ngx_time_update()
ngx_time_sigsafe_update()
39. TimeTime
ngx_time() and ngx_timeofday() -
macros return the current time value
in seconds
They are the preferred way to access
the cached time value.
Updating the time:
ngx_time_update()
ngx_time_sigsafe_update()
40. Nginx memoryNginx memory
● Pools
– easy and fast allocations
– easy deallocation at request finish
– simplify thread safety
● Shared memory
– share memory between nginx processes
– created using mmpa()
41. Nginx memory HeapNginx memory Heap
● ngx_alloc(size, log) — Allocate memory from
system heap. Allocation error and debugging
information is logged to log.
● ngx_calloc(size, log) — Allocate memory from
system heap like ngx_alloc(), but fill memory
with zeros after allocation.
● ngx_memalign(alignment, size, log) — Allocate
aligned memory from system heap.
● ngx_free(p)
42. Nginx memory PoolNginx memory Pool
● ngx_create_pool(size, log) — Create a pool with specified block
size. The pool object returned is allocated in the pool as well.
● ngx_destroy_pool(pool) — Free all pool memory, including the
pool object itself.
● ngx_palloc(pool, size) — Allocate aligned memory from the
specified pool.
● ngx_pcalloc(pool, size) — Allocate aligned memory from the
specified pool and fill it with zeroes.
● ngx_pnalloc(pool, size) — Allocate unaligned memory from the
specified pool. Mostly used for allocating strings.
● ngx_pfree(pool, p)