nginx internals

33
Nginx Internals [email protected]

Upload: liqiang-xu

Post on 08-May-2015

4.253 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Nginx internals

Nginx Internals

[email protected]

Page 2: Nginx internals

Agenda

• What

• Why

• Architecture

• Infrastructure

• Http Module

• Conf & Location &Static

• Upstream & fastcgi

• Extension

Page 3: Nginx internals

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/

Page 4: Nginx internals

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

Page 5: Nginx internals

Code Organization

• 117590

• Coremain/lib/data struct

• Eventepool/time/mutex

• Http http server/upstream

• Os & mail

Page 6: Nginx internals

Architecture

• Multi-process

• Master+worker

• Event driven

• Non-blocking

• Single-threaded

• Highly modular

• Cache management process

Page 7: Nginx internals

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

Page 8: Nginx internals

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

Page 9: Nginx internals

Memory pool

Page 10: Nginx internals

Master process

Page 11: Nginx internals

Worker process

Page 12: Nginx internals

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

Page 13: Nginx internals

Config & command

• Command-style configuration files, if/echo

• Configuration block, ‘{}’

• Support Include command

• Support inheritance(Http main/Server/Location)

• Multi-parameter

• Variables

• Conf demo

Page 14: Nginx internals

Config parse

• Typeparse_fileparse_blockparse_param

• Command handler

Page 15: Nginx internals

Modular

Page 16: Nginx internals

Http Module

• http command, http {}

• ngx_http_block

• ngx_http_module_t

Page 17: Nginx internals

Http request processing

ngx_http_process_request_line

ngx_http_init_connection

ngx_http_init_request

ngx_http_process_request_headers

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_log_module

ngx_http_static_module

ngx_http_access_module

ngx_http_index_module

ngx_http_rewrite_module

ngx_http_finalize_request

1 、 phase checker2 、 phase handler, array3 、 sub request

Page 18: Nginx internals

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_PHASE 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

Page 19: Nginx internals

content phase handler vs content_handler

• checker: ngx_http_core_content_phase

• Fastcgi & autoindex

Page 20: Nginx internals

Nginx Filter

• Header Filter

• Content Filter

• ngx_http_send_header

• ngx_http_output_filter

• Example : gzip 、 more_set_header

Page 21: Nginx internals

Location features

• Exactly match

• Regular(sensitive/ insensitive)

• Location inheritance

• How to find the right location?

Page 22: Nginx internals

Location internals

• Ngx_http_block loc_conf

• ngx_http_init_locationsLocation 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

Page 23: Nginx internals

Static module

• Sendfile http://linux.die.net/man/2/sendfile

• NGX_HTTP_CONTENT_PHASEngx_http_static_handler

• ngx_http_static_handler1 、 method check. GET/HEAD/POST2 、 Cache processing3 、 ngx_http_discard_request_body4 、 output

• Only Cache fd

• Red black tree

Page 24: Nginx internals

Nginx Upstream

• Protocol : Http/Fastcgi/Memcache/SCGI

• Load-balance: hash/random/round_robin

• Short connection

• Long connection only for memcached

Page 25: Nginx internals

Nginx Upstream : fastcgi

Page 26: Nginx internals

Extension Develop

• Core Module Extension, such as nginx-php

• Http Module ExtensionHandler : echoFilter : gzip, usertrackupstream : fastcgiLoad-balancers : rand

• Emiller's Guide To Nginx Module Development

Page 27: Nginx internals

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

Page 28: Nginx internals

Extension Develop—where?

Page 29: Nginx internals

Extension Develop—where?

Page 30: Nginx internals

Extension Develop—where?

• Http Phase handler

• Content_handler

• Header Filter

• Content Filter

Page 31: Nginx internals

Extension Develop—step

• “Config” filengx_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/directoryMakeMake install

Page 32: Nginx internals

Extension Develop—demo

• Demo

Page 33: Nginx internals

Q&A

Q&AThanks