nginx: writing your first module

  • Upload
    redivy

  • View
    4.730

  • Download
    0

Embed Size (px)

Citation preview

FEERSUM ENJINN

nginx: inside

FEATURES

event drivennon-blockingmodular

CORE MODULES

eventhttp main

EASY TO REMEMBER

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

OPTIONAL

HTTP AdditionEmbedded PerlFLVGzip PrecompressionRandom IndexGeoIPReal IP

SSLStub StatusWebDAVGoogle PerftoolsXSLTSecure LinkImage Filter

EASY EASY

(SO, NO NEED TO LEARN IT)

PROCESSES

NGINX MASTER

Monitors worker processesDoes all signal handlind work -INT exit -HUP reconfigure -USR1 log rotate

NGINX WORKER

Processes client's requestsPerforms master's command

CACHE MANAGER/LOADER

Loads content from filesDrops stale (expired) content

HTTP REQUEST

HANDLING REQUEST

receive dataparse the requestfind virtual serverfind locationrun through the phase handlersgenerate the responsefilter response headersfilter response bodysend out the result to the client

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;

YOUR OWN MODULE

HELLO WORLD

STEP 1

COMMANDS(CONFIG DIRECTIVES)

STEP 1

static ngx_command_t ngx_http_hello_commands[] = { { ngx_string("hello"), NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS, ngx_http_hello, 0, 0, NULL },

ngx_null_command};

STEP 2

Module context

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 */}

STEP 3

Module definition

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};

STEP 4

Setting handlers up

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;}

STEP 5

Your handler

STEP 5

static ngx_int_tngx_http_hello_handler(ngx_http_request_t *r){ /* do it yourself */ return NGX_OK;}

BUILDING

Config filengx_addon_name=ngx_http_hello_moduleHTTP_MODULES="$HTTP_MODULES ngx_http_hello_module"NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_hello_module.c"Configure nginx./configure --add-module=path/to/your/new/module/directory

COMPILE

DEBUG

ValgringStraceGDB

TOOLS

Does it work?You are sooo coool.Enjoy.