Nginx internals

  • 3,816 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
3,816
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
164
Comments
0
Likes
11

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Nginx Internals [email_address]
  • 2. Agenda
    • What
    • Why
    • Architecture
    • Infrastructure
    • Http Module
    • Conf & Location &Static
    • Upstream & fastcgi
    • Extension
  • 3. What’s Nginx
    • October 4, 2004
    • 6.8% of the top 1 million
    • HTTP Server
    • Reverse proxy server
    • Mail proxy server
    • http://nginx.org/en/
  • 4. Why Nginx
    • Features : Nginx = transmit + apache
    • Stability : Nginx > Lighttpd
    • Performance : Nginx > Lighttpd > apache
    • Security : Acess & Limit Zone & Limit Requests
    • Community : sina/qq/tudou/taobao
    • Third party modules
    • more
  • 5. Code Organization
    • 117590
    • Core main/lib/data struct
    • Event epool/time/mutex
    • Http http server/upstream
    • Os & mail
  • 6. Architecture
    • Multi-process
    • Master+worker
    • Event driven
    • Non-blocking
    • Single-threaded
    • Highly modular
    • Cache management process
  • 7. Infrastructure
    • String
    • Array/List/Queue
    • Hash Table
    • Red black tree
    • Memory pool
    • Buff
    • Radix Tree / Slab / Spinlock / shmtx
    • http://code.google.com/p/nginxsrp/wiki/NginxCodeReview
  • 8. Memory Management
    • ngx_alloc , ngx_calloc , ngx_memalign
    • Memory pool
      • Avoid memory fragmentation and memory leaks
      • Allocation once, deallocation once
      • lifetime : cycle 、 connection 、 Request
      • Optimizing large memory allocation
      • ngx_palloc ngx_pnalloc ngx_pcalloc ngx_pmemalign
  • 9. Memory pool
  • 10. Master process
  • 11. Worker process
  • 12. Master & woker
    • Socketpair
    • Accept Lock, avoid thundering herd
    • Multi Accept
    • Worker Processes Load-balance (7/8)
    • Connection List per process
    • Timeout handling. Red black tree
  • 13. Config & command
    • Command-style configuration files, if/echo
    • Configuration block, ‘{}’
    • Support Include command
    • Support inheritance(Http main/Server/Location)
    • Multi-parameter
    • Variables
    • Conf demo
  • 14. Config parse
    • Type parse_file parse_block parse_param
    • Command handler
  • 15. Modular
  • 16. Http Module
    • http command, http {}
    • ngx_http_block
    • ngx_http_module_t
  • 17. Http request processing 1 、 phase checker 2 、 phase handler, array 3 、 sub request
  • 18. http phase
    • NGX_HTTP_POST_READ_PHASE (realip)
    • NGX_HTTP_SERVER_REWRITE_PHASE (Rewrite)
    • NGX_HTTP_FIND_CONFIG_PHASE Can not be extended
    • NGX_HTTP_REWRITE_PHASE (Rewite)
    • NGX_HTTP_POST_REWRITE_PHAS E Can not be extended
    • NGX_HTTP_PREACCESS_PHASE (limit_req 、 limit_zone 、 degradation 、 realip)
    • NGX_HTTP_ACCESS_PHASE (ACCESS 、 auth_basic)
    • NGX_HTTP_POST_ACCESS_PHASE Can not be extended
    • NGX_HTTP_TRY_FILES_PHASE Can not be extended
    • NGX_HTTP_CONTENT_PHASE (Autoindex 、 static 、 random_index 、 gzip_static)
    • NGX_HTTP_LOG_PHASE
  • 19. content phase handler vs content_handler
    • checker: ngx_http_core_content_phase
    • Fastcgi & autoindex
  • 20. Nginx Filter
    • Header Filter
    • Content Filter
    • ngx_http_send_header
    • ngx_http_output_filter
    • Example : gzip 、 more_set_header
  • 21. Location features
    • Exactly match
    • Regular(sensitive/ insensitive)
    • Location inheritance
    • How to find the right location?
  • 22. Location internals
    • Ngx_http_block loc_conf
    • ngx_http_init_locations Location inheritance check
    • ngx_http_init_static_location_trees Ternary Search Tree(better than trie)
    • Find location(ngx_http_core_find_location) first static location tree then regex locations
  • 23. Static module
    • Sendfile http://linux.die.net/man/2/sendfile
    • NGX_HTTP_CONTENT_PHASE ngx_http_static_handler
    • ngx_http_static_handler 1 、 method check. GET/HEAD/POST 2 、 Cache processing 3 、 ngx_http_discard_request_body 4 、 output
    • Only Cache fd
    • Red black tree
  • 24. Nginx Upstream
    • Protocol : Http/Fastcgi/Memcache/SCGI
    • Load-balance: hash/random/round_robin
    • Short connection
    • Long connection only for memcached
  • 25. Nginx Upstream : fastcgi
  • 26. Extension Develop
    • Core Module Extension, such as nginx-php
    • Http Module Extension Handler : echo Filter : gzip, usertrack upstream : fastcgi Load-balancers : rand
    • Emiller's Guide To Nginx Module Development
  • 27. Extension Develop—where?
    • Just before the server reads the config file
    • For every configuration directive for the location and server for which it appears;
    • When Nginx initializes the main configuration
    • When Nginx initializes the server (i.e., host/port) configuration
    • When Nginx merges the server configuration with the main configuration
    • When Nginx initializes the location configuration
    • When Nginx merges the location configuration with its parent server configuration
    • When Nginx's master process starts
    • When a new worker process starts
    • When a worker process exits
    • When the master exits
    • Handling a request
    • Filtering response headers
    • Filtering the response body
    • Picking a backend server
    • Initiating a request to a backend server
    • Re-initiating a request to a backend server
    • Processing the response from a backend server
    • Finishing an interaction with a backend server
  • 28. Extension Develop—where?
  • 29. Extension Develop—where?
  • 30. Extension Develop—where?
    • Http Phase handler
    • Content_handler
    • Header Filter
    • Content Filter
  • 31. Extension Develop—step
    • “ Config” file
    • ngx_addon_name=ngx_http_baidu_usertrack_module
    • HTTP_AUX_FILTER_MODULES="$HTTP_AUX_FILTER_MODULES ngx_http_baidu_usertrack_module"
    • NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/src/ngx_http_baidu_usertrack_module.c $ngx_addon_dir/src/baidu_des.c"
    • NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ngx_addon_dir/src/baidu_des.h"
    • ORE_LIBS="$CORE_LIBS -lcrypto"
    • Develop and build ./configure -add-module=path/to/your/new/module/directory Make Make install
  • 32. Extension Develop—demo
    • Demo
  • 33. Q&A Q&A Thanks