½â¶ÁNginxµÄÄ£¿é¿ª·¢ºÍÀ©Õ¹»úÖƵĵײãʵÏÖÔÀí
½â¶ÁnginxµÄÄ£¿é¿ª·¢ºÍÀ©Õ¹»úÖƵĵײãʵÏÖÔÀí
NginxÊÇÒ»¸öºÜÊÇÊ¢ÐеĸßÐÔÄÜWebЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬ËüµÄÄ£¿é¿ª·¢ºÍÀ©Õ¹»úÖÆʹµÃÓû§¿ÉÒÔºÜÀû±ãµØÀ©Õ¹NginxµÄ¹¦Ð§¡£±¾ÎĽ«ÆÊÎöNginxµÄÄ£¿é¿ª·¢ºÍÀ©Õ¹»úÖƵĵײãʵÏÖÔÀí£¬²¢¸ø³öһЩ´úÂëʾÀý¡£
NginxÄ£¿éµÄ½á¹¹
Ò»¸ö±ê×¼µÄNginxÄ£¿éÊÇÒ»¸ö¶¯Ì¬Á´½Ó¿â£¬Ëü°üÀ¨ÁËһϵÁеĻص÷º¯Êý£¬ÕâЩ»Øµ÷º¯Êý»áÔÚNginxÔËÐÐÀú³ÌÖеÄÏìӦʱ»ú±»Å²Óá£Ò»¸öNginxÄ£¿éµÄ½á¹¹Ê¾ÀýÈçÏ£º
#include <ngx_config.h> #include <ngx_core.h> #include <ngx_http.h> static ngx_int_t ngx_http_example_handler(ngx_http_request_t *r); static ngx_http_module_t ngx_http_example_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 configuration */ NULL /* merge location configuration */ }; ngx_module_t ngx_http_example_module = { NGX_MODULE_V1, &ngx_http_example_module_ctx, /* module context */ NULL, /* 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 }; static ngx_command_t ngx_http_example_commands[] = { { ngx_string("example"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS, ngx_http_example_command, NGX_HTTP_LOC_CONF_OFFSET, 0, NULL }, ngx_null_command }; static ngx_http_module_t ngx_http_example_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 configuration */ NULL /* merge location configuration */ }; ngx_module_t ngx_http_example_module = { NGX_MODULE_V1, &ngx_http_example_module_ctx, /* module context */ ngx_http_example_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 };
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇ¿ÉÒÔ¿´µ½ngx_module_t½á¹¹½ç˵ÁËÒ»¸öNginxÄ£¿é£¬²¢Ö¸¶¨Á˸ÃÄ£¿éµÄÉÏÏÂÎĺÍÖ¸¶¨µÄ»Øµ÷º¯Êý¡£ngx_http_module_t½á¹¹ÔòÊÇÓÃÓÚHTTPÄ£¿éµÄ½ç˵¡£
NginxÄ£¿éµÄ½¹µã»Øµ÷º¯Êý
NginxÄ£¿éµÄ½¹µã»Øµ÷º¯Êýͨ¹ýngx_http_module_t½á¹¹ÖеÄÖ¸ÕëÖ¸ÏòÏìÓ¦µÄº¯Êý¡£ÒÔÏÂÊÇһЩ³£ÓõĽ¹µã»Øµ÷º¯ÊýºÍʾÀý´úÂ룺
static ngx_int_t ngx_http_example_handler(ngx_http_request_t *r) { ngx_int_t rc; ngx_buf_t *b; ngx_chain_t out; /* ½¨ÉèÒ»¸öÊä³ö»º³åÇø */ b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); if (b == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } out.buf = b; out.next = NULL; /* ÉèÖÃÊä³ö»º³åÇøµÄÄÚÈÝ */ b->pos = (u_char *) "Hello, Nginx!"; b->last = b->pos + sizeof("Hello, Nginx!") - 1; b->memory = 1; b->last_buf = 1; /* ÉèÖÃÏìӦͷ²¿ */ r->headers_out.status = NGX_HTTP_OK; r->headers_out.content_length_n = sizeof("Hello, Nginx!") - 1; rc = ngx_http_send_header(r); /* ·¢ËÍÏìÓ¦ÄÚÈÝ */ if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { return rc; } return ngx_http_output_filter(r, &out); } static ngx_int_t ngx_http_example_init(ngx_conf_t *cf) { /* »ñÈ¡httpÄ£¿éµÄngx_http_core_moduleÉÏÏÂÎÄ */ ngx_http_core_main_conf_t *cmcf; cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); /* ÔÚngx_http_core_moduleµÄ´¦ÀíÇëÇóµÄ»Øµ÷º¯ÊýÊý×éhandlersÖмÓÈë×Ô½ç˵»Øµ÷º¯Êý */ ngx_http_handler_pt *h; h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers); if (h == NULL) { return NGX_ERROR; } *h = ngx_http_example_handler; return NGX_OK; }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöʾÀý´úÂëÖУ¬ngx_http_example_handlerº¯ÊýÊÇÏÖʵ´¦ÀíHTTPÇëÇóµÄº¯Êý¡£±ðµÄ£¬ngx_http_example_initº¯ÊýÓÃÓÚ½«ngx_http_example_handlerÌí¼Óµ½NginxµÄÇëÇó´¦Àí»Øµ÷º¯ÊýÊý×éÖС£
NginxÄ£¿éµÄ±àÒëºÍ¼ÓÔØ
±àÒëNginxÄ£¿éµÄʱ¼ä£¬ÐèÒªÔÚconfigureÏÂÁîÖмÓÈë–add-module=/path/to/module/directory²ÎÊý£¬½«Ä£¿éµÄÔ´ÂëĿ¼ת´ï¸øconfigure¾ç±¾¡£È»ºóʹÓÃmakeÏÂÁî±àÒëNginx¡£
¼ÓÔØNginxÄ£¿é£¬¿ÉÒÔÔÚNginxµÄÉèÖÃÎļþÖÐʹÓÃload_moduleÖ¸Áָ¶¨Ä£¿éµÄ·¾¶¡£ÀýÈ磺
load_module /path/to/module.so;
µÇ¼ºó¸´ÖÆ
×ܽá
ͨ¹ý±¾ÎÄ£¬ÎÒÃÇÏàʶÁËNginxÄ£¿é¿ª·¢ºÍÀ©Õ¹»úÖƵĵײãʵÏÖÔÀí£¬²¢¸ø³öÁËһЩ´úÂëʾÀý¡£Ï£Íû¶ÁÕßÄܹ»¶ÔNginxµÄÄ£¿é¿ª·¢ºÍÀ©Õ¹ÓиüÉîÈëµÄÃ÷È·£¬Îª×Ô¼ºµÄÏîÄ¿Ìí¼Ó¸ü¶àµÄ¹¦Ð§¡£
ÒÔÉϾÍÊǽâ¶ÁNginxµÄÄ£¿é¿ª·¢ºÍÀ©Õ¹»úÖƵĵײãʵÏÖÔÀíµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡