欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

初識nginxhelloworld模塊

成都創(chuàng)新互聯(lián)公司專注于麗水網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供麗水營銷型網(wǎng)站建設(shè),麗水網(wǎng)站制作、麗水網(wǎng)頁設(shè)計、麗水網(wǎng)站官網(wǎng)定制、微信平臺小程序開發(fā)服務(wù),打造麗水網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供麗水網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。1.nginx模塊

首先nginx和apache的不同就是nginx的模塊

vi /opt/nginx_hello_word/config

不能夠動態(tài)添加,需要在編譯時,指定要添加的模塊路徑,與nginx源碼一起編譯。
nginx模塊的處理流程:
a.客戶端發(fā)送http請求道nginx服務(wù)器
b.nginx基于配置文件中的位置選擇一個合適的處理模塊
c.負載均衡模塊選擇一臺后端服務(wù)器(反向代理情況下)
d.處理模塊進行處理并把輸出緩沖放到第一個過濾模塊上
e.第一個過濾模塊處理后輸出給第二個過濾模塊
f.然后第二個過濾模塊又到第三個過濾模塊
g.第N個過濾模塊。。。
h.發(fā)處理結(jié)果發(fā)給客戶端

2.nginx模塊編寫

a、創(chuàng)建模塊文件夾

mkdir -p /opt/nginx_hello_world cd /opt/nginx_hello_world

b、創(chuàng)建模塊配置文件

vi /opt/nginx_hello_word/config

寫入內(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

寫入內(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

默認只需要

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

訪問: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之后會詳細講解。 NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS, //配置指令的合法位置,這里表示:location部分合法,并且指令沒有參數(shù)。 ngx_http_hello_world,//回調(diào)函數(shù),三個參數(shù)(ngx_conf_t *cf,ngx_command_t *cmd, void *conf) 0,//后面的參數(shù)有待發(fā)掘,我還沒有用到 0, NULL }, ngx_null_command };

b.static u_char ngx_hello_world[] ="hello world" 則是輸出到屏幕的字符串。

c.ngx_http_module_t用來定義結(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)建全局部分配置時調(diào)用 */ NULL, /* 初始化全局部分的配置時調(diào)用*/ NULL, /* 創(chuàng)建虛擬主機部分的配置時調(diào)用*/ NULL, /* 與全局部分配置合并時調(diào)用 */ NULL, /* 創(chuàng)建位置部分的配置時調(diào)用 */ NULL /* 與主機部分配置合并時調(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é)會詳細講解。

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 可以訪問到客戶端的頭部和不久要發(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)題:初識nginxhelloworld模塊
網(wǎng)站地址:http://chinadenli.net/article22/cpojjc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷App開發(fā)品牌網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、網(wǎng)站收錄網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)