最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Nginx服務(wù)器中的模塊編寫及相關(guān)內(nèi)核源碼初探

 更新時(shí)間:2015年12月24日 14:25:40   作者:SmartMZ  
這篇文章主要介紹了Nginx服務(wù)器中的模塊編寫及相關(guān)源碼初探,文中以一個(gè)簡(jiǎn)單的Hello world模塊的編寫來(lái)深入分析Nginx內(nèi)核所用到的基礎(chǔ)函數(shù),需要的朋友可以參考下

1.nginx模塊
首先nginx和apache最大的不同就是nginx的模塊不能夠動(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.處理結(jié)果發(fā)給客戶端

2.nginx模塊編寫
a、創(chuàng)建模塊文件夾

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

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

vi /opt/nginx_hello_word/config 

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

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

d、下載nginx源碼包,我下載的是nginx-1.0.13.tar.gz
這里注意在編譯helloworld模塊前首先確認(rèn),nginx是否可以獨(dú)立編譯成功,是否安裝了所需的所有模塊。
與helloworld模塊一起編譯nginx:

./configure --prefix=/usr/local/nginx --add-module=/opt/nginx_hello_world/ 
make 
make install 

e、配置nginx.conf

location= /hello { 
 hello_world; 
} 

f、啟動(dòng)nginx,訪問(wèn)http://localhost/hello ,可以看到編寫的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部分合法,并且指令沒有參數(shù)。 
  ngx_http_hello_world,//回調(diào)函數(shù),三個(gè)參數(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用來(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 
}; 

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

helloworld模塊里面涉及最重要的數(shù)據(jù)就是ngx_module_t指針數(shù)組,這個(gè)指針數(shù)組包含了當(dāng)前編譯版本支持的所有模塊,這個(gè)指針數(shù)組定義實(shí)在自動(dòng)腳本生成的objs/ngx_modules.c中,如下:

 extern ngx_module_t ngx_core_module; 
 extern ngx_module_t ngx_errlog_module; 
 extern ngx_module_t ngx_conf_module; 
 extern ngx_module_t ngx_events_module; 
 extern ngx_module_t ngx_event_core_module; 
 extern ngx_module_t ngx_epoll_module; 
 extern ngx_module_t ngx_http_module; 
 extern ngx_module_t ngx_http_core_module; 
 extern ngx_module_t ngx_http_log_module; 
 extern ngx_module_t ngx_http_upstream_module; 
 extern ngx_module_t ngx_http_static_module; 
 extern ngx_module_t ngx_http_autoindex_module; 
 extern ngx_module_t ngx_http_index_module; 
 extern ngx_module_t ngx_http_auth_basic_module; 
 extern ngx_module_t ngx_http_access_module; 
 extern ngx_module_t ngx_http_limit_zone_module; 
 extern ngx_module_t ngx_http_limit_req_module; 
 extern ngx_module_t ngx_http_geo_module; 
 extern ngx_module_t ngx_http_map_module; 
 extern ngx_module_t ngx_http_split_clients_module; 
 extern ngx_module_t ngx_http_referer_module; 
 extern ngx_module_t ngx_http_rewrite_module; 
 extern ngx_module_t ngx_http_proxy_module; 
 extern ngx_module_t ngx_http_fastcgi_module; 
 extern ngx_module_t ngx_http_uwsgi_module; 
 extern ngx_module_t ngx_http_scgi_module; 
 extern ngx_module_t ngx_http_memcached_module; 
 extern ngx_module_t ngx_http_empty_gif_module; 
 extern ngx_module_t ngx_http_browser_module; 
 extern ngx_module_t ngx_http_upstream_ip_hash_module; 
 extern ngx_module_t ngx_http_cache_purge_module; 
 extern ngx_module_t ngx_http_write_filter_module; 
 extern ngx_module_t ngx_http_header_filter_module; 
 extern ngx_module_t ngx_http_chunked_filter_module; 
 extern ngx_module_t ngx_http_range_header_filter_module; 
 extern ngx_module_t ngx_http_gzip_filter_module; 
 extern ngx_module_t ngx_http_postpone_filter_module; 
 extern ngx_module_t ngx_http_ssi_filter_module; 
 extern ngx_module_t ngx_http_charset_filter_module; 
 extern ngx_module_t ngx_http_userid_filter_module; 
 extern ngx_module_t ngx_http_headers_filter_module; 
 extern ngx_module_t ngx_http_copy_filter_module; 
 extern ngx_module_t ngx_http_range_body_filter_module; 
 extern ngx_module_t ngx_http_not_modified_filter_module; 
  
 ngx_module_t *ngx_modules[] = { 
  &ngx_core_module, 
  &ngx_errlog_module, 
  &ngx_conf_module, 
  &ngx_events_module, 
  &ngx_event_core_module, 
  &ngx_epoll_module, 
  &ngx_http_module, 
  &ngx_http_core_module, 
  &ngx_http_log_module, 
  &ngx_http_upstream_module, 
  &ngx_http_static_module, 
  &ngx_http_autoindex_module, 
  &ngx_http_index_module, 
  &ngx_http_auth_basic_module, 
  &ngx_http_access_module, 
  &ngx_http_limit_zone_module, 
  &ngx_http_limit_req_module, 
  &ngx_http_geo_module, 
  &ngx_http_map_module, 
  &ngx_http_split_clients_module, 
  &ngx_http_referer_module, 
  &ngx_http_rewrite_module, 
  &ngx_http_proxy_module, 
  &ngx_http_fastcgi_module, 
  &ngx_http_uwsgi_module, 
  &ngx_http_scgi_module, 
  &ngx_http_memcached_module, 
  &ngx_http_empty_gif_module, 
  &ngx_http_browser_module, 
  &ngx_http_upstream_ip_hash_module, 
  &ngx_http_cache_purge_module, 
  &ngx_http_write_filter_module, 
  &ngx_http_header_filter_module, 
  &ngx_http_chunked_filter_module, 
  &ngx_http_range_header_filter_module, 
  &ngx_http_gzip_filter_module, 
  &ngx_http_postpone_filter_module, 
  &ngx_http_ssi_filter_module, 
  &ngx_http_charset_filter_module, 
  &ngx_http_userid_filter_module, 
  &ngx_http_headers_filter_module, 
  &ngx_http_copy_filter_module, 
  &ngx_http_range_body_filter_module, 
  &ngx_http_not_modified_filter_module, 
  NULL 
 }; 

 

這里只有每個(gè)模塊變量的聲明,并且每個(gè)模塊的定義都包含在自己的模塊文件當(dāng)中,比如ngx_core_module定義在src/core/nginx.c中:

ngx_module_t ngx_core_module = { 
 NGX_MODULE_V1, 
 &ngx_core_module_ctx,     /* module context */ 
 ngx_core_commands,      /* module directives */ 
 NGX_CORE_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 
}; 

是不是跟helloworld里面非常相似了,沒錯(cuò),他們都是模塊,唯一的不同點(diǎn)就是helloworld是你另外加進(jìn)去的。
到現(xiàn)在位置也只是初探nginx的模塊,最后提一張別人畫的nginx的模塊圖,有助于接下來(lái)的學(xué)習(xí)。

相關(guān)文章

  • Linux平臺(tái)通過(guò)nginx和vsftpd構(gòu)建圖片服務(wù)器

    Linux平臺(tái)通過(guò)nginx和vsftpd構(gòu)建圖片服務(wù)器

    這篇文章主要介紹了Linux平臺(tái)通過(guò)nginx和vsftpd構(gòu)建圖片服務(wù)器,需要的朋友可以參考下
    2017-05-05
  • nginx+lua(openresty)實(shí)現(xiàn)黑/白名單權(quán)限控制的示例

    nginx+lua(openresty)實(shí)現(xiàn)黑/白名單權(quán)限控制的示例

    本文介紹了如何使用Openresty進(jìn)行權(quán)限控制和灰度發(fā)布,具體通過(guò)定時(shí)器定期更新黑名單數(shù)據(jù),進(jìn)行用戶過(guò)濾和權(quán)限管控,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-09-09
  • Linux下安裝MongoDB的實(shí)現(xiàn)步驟

    Linux下安裝MongoDB的實(shí)現(xiàn)步驟

    這篇文章主要介紹了Linux下安裝MongoDB的實(shí)現(xiàn)步驟的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家輕松安裝,需要的朋友可以參考下
    2017-10-10
  • Nginx+tomcat負(fù)載均衡集群的實(shí)現(xiàn)方法

    Nginx+tomcat負(fù)載均衡集群的實(shí)現(xiàn)方法

    這篇文章主要介紹了Nginx+tomcat負(fù)載均衡集群,的實(shí)現(xiàn)方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • 一文帶你搞懂什么是Nginx服務(wù)器

    一文帶你搞懂什么是Nginx服務(wù)器

    這篇文章主要介紹了一文帶你搞懂什么是Nginx,Nginx?(engine?x)?是一個(gè)高性能的HTTP和反向代理web服務(wù)器,同時(shí)也提供了iMAP/POP3/SMTP服務(wù),需要的朋友可以參考下
    2023-04-04
  • nginx?location/區(qū)別詳解

    nginx?location/區(qū)別詳解

    本文主要介紹了nginx?location/區(qū)別詳解,主要介紹了8中不同的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • 詳解Nginx 被動(dòng)檢查服務(wù)器的存活狀態(tài)

    詳解Nginx 被動(dòng)檢查服務(wù)器的存活狀態(tài)

    Nginx 可以持續(xù)測(cè)試您的上游服務(wù)器,避免出現(xiàn)故障的服務(wù)器,并將恢復(fù)的服務(wù)器優(yōu)雅地添加到負(fù)載均衡組中。這篇文章主要介紹了Nginx 被動(dòng)檢查服務(wù)器的存活狀態(tài),需要的朋友可以參考下
    2021-10-10
  • Nginx URL重寫rewrite機(jī)制原理及使用實(shí)例

    Nginx URL重寫rewrite機(jī)制原理及使用實(shí)例

    這篇文章主要介紹了Nginx URL重寫(rewrite)機(jī)制原理及使用實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • Nginx 分發(fā)策略的實(shí)現(xiàn)

    Nginx 分發(fā)策略的實(shí)現(xiàn)

    分發(fā)策略是將客戶端請(qǐng)求根據(jù)一定的規(guī)則或算法,可以分配到不同的后端服務(wù)器上,本文就來(lái)介紹一下Nginx分發(fā)策略的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-02-02
  • 詳解用Nginx搭建CDN服務(wù)器方法(圖文)

    詳解用Nginx搭建CDN服務(wù)器方法(圖文)

    這篇文章主要介紹了詳解用Nginx搭建CDN服務(wù)器方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06

最新評(píng)論

临夏县| 星座| 偏关县| 朝阳市| 芦溪县| 安岳县| 湘潭市| 山东省| 聂拉木县| 外汇| 长宁区| 辰溪县| 启东市| 迭部县| 衡水市| 福安市| 宜宾县| 兴隆县| 玉林市| 府谷县| 西城区| 历史| 泰宁县| 那坡县| 鞍山市| 酒泉市| 庐江县| 界首市| 定州市| 曲沃县| 镇安县| 九龙县| 六安市| 垣曲县| 阿瓦提县| 乌拉特前旗| 临武县| 醴陵市| 普陀区| 谷城县| 博兴县|