国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > Nginx > 正文

Nginx服務器中的模塊編寫及相關內核源碼初探

2024-08-30 12:26:27
字體:
來源:轉載
供稿:網友

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

2.nginx模塊編寫
a、創建模塊文件夾

mkdir -p /opt/nginx_hello_world cd /op/nginx_hello_word 

b、創建模塊配置文件

vi /opt/nginx_hello_word/config 

c、創建模塊主文件

vi /opt/nginx_hello_world/ngx_http_hello_world_module.c 

寫入如下內容:

#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); 

寫的helloworld模塊 
  
 

/* 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/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); } 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; }             
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宜良县| 肃宁县| 车致| 普格县| 陕西省| 龙口市| 仲巴县| 平舆县| 安福县| 新绛县| 黎城县| 读书| 湘西| 龙江县| 尚义县| 花莲县| 岐山县| 如皋市| 涞水县| 长葛市| 怀柔区| 新化县| 台湾省| 怀来县| 临夏县| 徐水县| 张家川| 中超| 府谷县| 吕梁市| 东方市| 奉新县| 伽师县| 色达县| 弋阳县| 青田县| 黑水县| 准格尔旗| 商丘市| 伊川县| 本溪市|