首先nginx和apache的不同就是nginx的模塊
vi /opt/nginx_hello_word/config
不能夠動(dòng)態(tài)添加,需要在編譯時(shí),指定要添加的模塊路徑,與nginx源碼一起編譯。
nginx模塊的處理流程:
a.客戶端發(fā)送http請(qǐng)求道nginx服務(wù)器
b.nginx基于配置文件中的位置選擇一個(gè)合適的處理模塊
c.負(fù)載均衡模塊選擇一臺(tái)后端服務(wù)器(反向代理情況下)
d.處理模塊進(jìn)行處理并把輸出緩沖放到第一個(gè)過(guò)濾模塊上
e.第一個(gè)過(guò)濾模塊處理后輸出給第二個(gè)過(guò)濾模塊
f.然后第二個(gè)過(guò)濾模塊又到第三個(gè)過(guò)濾模塊
g.第N個(gè)過(guò)濾模塊。。。
h.發(fā)處理結(jié)果發(fā)給客戶端
a、創(chuàng)建模塊文件夾
mkdir -p /opt/nginx_hello_world cd /opt/nginx_hello_world
b、創(chuàng)建模塊配置文件
vi /opt/nginx_hello_word/config
寫(xiě)入內(nèi)容如下:
ngx_addon_name=ngx_http_hello_world_module HTTP_MODULES="$HTTP_MODULES ngx_http_hello_world_module" NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_hello_world_module.c"
c、創(chuàng)建模塊主文件
vi /opt/nginx_hello_world/ngx_http_hello_world_module.c
寫(xiě)入內(nèi)容如下:
#include <ngx_config.h> #include <ngx_core.h> #include <ngx_http.h> static char *ngx_http_hello_world(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); /* Commands */ static ngx_command_t ngx_http_hello_world_commands[] = { { ngx_string("hello_world"), NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS, ngx_http_hello_world, 0, 0, NULL }, ngx_null_command }; static u_char ngx_hello_world[] = "hello world"; static ngx_http_module_t ngx_http_hello_world_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 */ }; /* hook */ ngx_module_t ngx_http_hello_world_module = { NGX_MODULE_V1, &ngx_http_hello_world_module_ctx, /* module context */ ngx_http_hello_world_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 }; static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t *r) { /* ngx_int_t rc; */ ngx_buf_t *b; ngx_chain_t out; /* Http Output Buffer */ r->headers_out.content_type.len = sizeof("text/html;charset=utf-8") - 1; r->headers_out.content_type.data = (u_char *) "text/html;charset=utf-8"; b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); out.buf = b; out.next = NULL; b->pos = ngx_hello_world; b->last = ngx_hello_world + sizeof(ngx_hello_world); b->memory = 1; b->last_buf = 1; r->headers_out.status = NGX_HTTP_OK; r->headers_out.content_length_n = sizeof(ngx_hello_world); ngx_http_send_header(r); return ngx_http_output_filter(r, &out); } static char * ngx_http_hello_world(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_http_core_loc_conf_t *clcf ; /* register hanlder */ clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); clcf->handler = ngx_http_hello_world_handler; return NGX_CONF_OK; }
d、編譯我的加了nginx_lua模塊,其他模塊等的。
./configure --prefix=/usr/local/nginx-1.9.6 --with-pcre=/root/pcre-8.36 --with-zlib=/root/zlib-1.2.8 --with-openssl=/root/openssl-1.0.2a --with-stream --with-stream_ssl_module --with-cpp_test_module --with-http_stub_status_module --with-http_degradation_module --with-http_secure_link_module --with-http_gzip_static_module --with-http_gunzip_module --with-http_sub_module --with-http_realip_module --with-http_v2_module --with-http_ssl_module --with-threads --add-module=/root/ngx_devel_kit-master --add-module=/root/lua-nginx-module-master --add-module=/root/echo-nginx-module-master --add-module=/root/stream-lua-nginx-module-master --add-module=/root/lua-upstream-nginx-module-master --add-module=/root/headers-more-nginx-module-master --add-module=/opt/nginx_hello_world
默認(rèn)只需要
./configure --prefix=/usr/local/nginx --add-module=/opt/nginx_hello_world/
make make install
e、配置nginx.conf
只需要在server域增加location
server { listen 83; server_name _; charset utf-8; access_log logs/tttt.access.log; location /helloworld { hello_world; } }
訪問(wèn):http://192.168.26.61:83/helloworld即可得到helloworld
3.hello world模塊分析a. ngx_command_t函數(shù)用于定義包含模塊指令的靜態(tài)數(shù)組ngx_http_hello_world_commands
static ngx_command_t ngx_http_hello_world_commands[] = { { ngx_string("hello_world"), //設(shè)置指令名稱字符串,注意不能包含空格,數(shù)據(jù)類型ngx_str_t之后會(huì)詳細(xì)講解。 NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS, //配置指令的合法位置,這里表示:location部分合法,并且指令沒(méi)有參數(shù)。 ngx_http_hello_world,//回調(diào)函數(shù),三個(gè)參數(shù)(ngx_conf_t *cf,ngx_command_t *cmd, void *conf) 0,//后面的參數(shù)有待發(fā)掘,我還沒(méi)有用到 0, NULL }, ngx_null_command };
b.static u_char ngx_hello_world[] ="hello world" 則是輸出到屏幕的字符串。
c.ngx_http_module_t用來(lái)定義結(jié)構(gòu)體ngx_http_hello_world_module_ctx:
static ngx_http_module_t ngx_http_hello_world_module_ctx = { NULL, /* 讀入配置前調(diào)用*/ NULL, /* 讀入配置后調(diào)用*/ NULL, /* 創(chuàng)建全局部分配置時(shí)調(diào)用 */ NULL, /* 初始化全局部分的配置時(shí)調(diào)用*/ NULL, /* 創(chuàng)建虛擬主機(jī)部分的配置時(shí)調(diào)用*/ NULL, /* 與全局部分配置合并時(shí)調(diào)用 */ NULL, /* 創(chuàng)建位置部分的配置時(shí)調(diào)用 */ NULL /* 與主機(jī)部分配置合并時(shí)調(diào)用*/ };
d.ngx_module_t定義結(jié)構(gòu)體ngx_http_hello_world_module
ngx_module_t ngx_http_hello_world_module = { NGX_MODULE_V1, &ngx_http_hello_world_module_ctx, /* module context */ ngx_http_hello_world_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 };
他包含有模塊的主要內(nèi)容和指令的執(zhí)行部分,下一節(jié)會(huì)詳細(xì)講解。
e.處理函數(shù),ngx_http_hello_world_handler,也是hello world 模塊的核心部分。
static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t *r)//ngx_http_request_t *r 可以訪問(wèn)到客戶端的頭部和不久要發(fā)送的回復(fù)頭部 { /* ngx_int_t rc; */ ngx_buf_t *b; ngx_chain_t out; /* Http Output Buffer */ r->headers_out.content_type.len = sizeof("text/plain") - 1; r->headers_out.content_type.data = (u_char *) "text/plain"; b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); out.buf = b; out.next = NULL; b->pos = ngx_hello_world; b->last = ngx_hello_world + sizeof(ngx_hello_world); b->memory = 1; b->last_buf = 1; r->headers_out.status = NGX_HTTP_OK; r->headers_out.content_length_n = sizeof(ngx_hello_world); ngx_http_send_header(r); return ngx_http_output_filter(r, &out); }
網(wǎng)站標(biāo)題:初識(shí)nginxhelloworld模塊
網(wǎng)站地址:http://m.rwnh.cn/article22/cpojjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷、App開(kāi)發(fā)、品牌網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、網(wǎng)站收錄、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)