Professional Documents
Culture Documents
Agenda
Source code layout Key concepts and infrastructure The event-driven architecture HTTP request handling Mail proxying process Nginx module development Misc. topics
Files
$ find . -name "*\.[hc]" -print | wc l 234 $ ls src core event http mail misc os $ find . -name "*\.[hc]" -print | xargs wc -l | tail -n1 110953 total
Lines of code
Code Organization
core/
The backbone and infrastructure The event-driven engine and modules The HTTP server and modules The Mail proxy server and modules C++ compatibility test and the Google perftools module OS dependent implementation files
event/
http/
mail/
misc/
os/
Nginx Architecture
Non-blocking Event driven Single threaded[*] One master process and several worker processes Resource efficient Highly modular
Agenda
Source code layout Key concepts and infrastructure The event-driven architecture HTTP request handling Mail proxying process Nginx module development Misc. topics
Memory Pool
Avoid memory fragmentation Avoid memory leak Allocation and deallocation can be very fast Lifetime and pool size
ngx_pool_t
Small blocks Large blocks Free chain list Cleanup handler list ngx_palloc
API
memory aligned
ngx_pnalloc ngx_pcalloc
Buffer Management
Buffer
Pointers
memory
start/pos/last/end file_pos/file_last/file
file
Flags
Buffer chain
Output chain
in/free/busy chains
Chain writer
String Utilities
ngx_str_t
Memory related String formatting String comparison String search Base64 encoding/decoding URI escaping/unescaping UTF-8 decoding String-to-number conversion
Data Structures
Array List Queue Hash table Red black tree Radix tree Set object values after added
Characteristic
Logging
Error log
Access log
variables
Per location
Rotation
Configuration File
Directive
Parsing
Values
Block
Variables
Agenda
Source code layout Key concepts and infrastructure The event-driven architecture HTTP request handling Mail proxying process Nginx module development Misc. topics
Master
Monitor workers, respawn when a worker dies Handle signals and notify workers
Worker
handle connections
Inter-process Communication
Signals
Channel
socketpair command
Shared memory
Event
ngx_event_t
Callbacks Handlers
ngx_event_accept ngx_process_events_and_timers ngx_handle_read_event ngx_handle_write_event Posted accept events queue Posted events queue
Posted events
Time Cache
Timer resolution
Interval timer
setitimer()
Timer Management
Actions
Add a timer Delete a timer Get the minimum timer O(log n) complexity
Accept Mutex
I/O
Multiplexing
kqueue/epoll
select/poll/dev/poll
Advanced I/O
TCP_CORK/TCP_NODELAY/TCP_DEFER_ACCEPT
Agenda
Source code layout Key concepts and infrastructure The event-driven architecture HTTP request handling Mail proxying process Nginx module development Misc. topics
Important Structures
Connection
HTTP connection
HTTP request
headers_in headers_out
Virtual Servers
Locations
Location tree
Static Regex
= ^~ ~ ~*
Per-location configuration
Value
inheritance override
Handler try_files/post_action/error_page
Named location
HTTP Contexts
Types
main_conf srv_conf loc_conf ngx_http_get_module_main_conf ngx_http_get_module_srv_conf ngx_http_get_module_loc_conf ngx_http_conf_get_module_main_conf ngx_http_conf_get_module_srv_conf ngx_http_conf_get_module_loc_conf ngx_http_get_module_ctx ngx_http_set_ctx
Request
Module context
HTTP Handling
Receive data Parse the request Find the virtual server Find the location Run phase handlers Generate the response Filter response headers Filter the response body Send out the output to the client
Request Parsing
Phases
POST_READ SERVER_REWRITE FIND_CONFIG REWRITE POST_REWRITE PREACCESS ACCESS POST_ACCESS TRY_FILES CONTENT LOG Checker Handler Next
Phase handler
Phase engine
Handlers server_rewrite_index location_rewrite_index r->phase_handler ngx_http_core_generic_phase ngx_http_core_find_config_phase ngx_http_core_post_rewrite_phase ngx_http_core_access_phase ngx_http_core_post_access_phase ngx_http_core_try_files_phase ngx_http_core_content_phase
Default checkers
Filter Chain
top_header_filter ngx_http_top_body_filter
ngx_http_output_filter
Process order
curl -i http://localhost/
HTTP Keep-Alive
Request memory reuse Connection memory shrink Keep-alive timeout Request count
Subrequest
Filters
Maximum subrequests
Internal Redirect
Upstream
Hooks
input_filter_init input_filter create_request reinit_request process_header abort_request finalize_request rewrite_redirect FastCGI Proxy Memcached
Modules
Agenda
Source code layout Key concepts and infrastructure The event-driven architecture HTTP request handling Mail proxying process Nginx module development Misc. topics
Mail Proxy
Sequence diagram
Mail session
Command parsing Packets relay Load balancing Authentication rewriting Black lists/white lists
Agenda
Source code layout Key concepts and infrastructure The event-driven architecture HTTP request handling Mail proxying process Nginx module development Misc. topics
Context
Directives Type
core/event/http/mail init_master
Hooks
called at master process initialization called when the module is loaded called at worker process initialization called at worker process termination called at master process termination
init_module
init_process
exit_process
exit_master
Name Hooks
Examples
Name Hooks
add del enable disable add_conn del_conn process_changes process_events init done
Protocol
Hooks
Files
Build
Configuration
Agenda
Source code layout Key concepts and infrastructure The event-driven architecture HTTP request handling Mail proxying process Nginx module development Misc. topics
Auto Scripts
Reconfiguration
Thank You!