亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 網站 > Nginx > 正文

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

2024-08-30 12:27:49
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了Nginx服務器中的模塊編寫及相關源碼初探,文中以一個簡單的Hello world模塊的編寫來深入分析Nginx內核所用到的基礎函數,需要的朋友可以參考下
 

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

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

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

e、配置nginx.conf

location= /hello {  hello_world; } 

f、啟動nginx,訪問http://localhost/hello ,可以看到編寫的helloworld模塊輸出的文字。
 
3.hello world模塊分析
a.ngx_command_t函數用于定義包含模塊指令的靜態數組ngx_http_hello_world_commands

static ngx_command_t ngx_http_hello_world_commands[] = {  { ngx_string("hello_world"), //設置指令名稱字符串,注意不能包含空格,數據類型ngx_str_t之后會詳細講解。   NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS, //配置指令的合法位置,這里表示:location部分合法,并且指令沒有參數。   ngx_http_hello_world,//回調函數,三個參數(ngx_conf_t *cf,ngx_command_t *cmd, void *conf)   0,//后面的參數有待發掘,我還沒有用到   0,   NULL },  ngx_null_command }; 

b.static u_char ngx_hello_world[] ="hello world" 則是輸出到屏幕的字符串。
c.ngx_http_module_t用來定義結構體ngx_http_hello_world_module_ctx:

static ngx_http_module_t ngx_http_hello_world_module_ctx = {  NULL,         /* 讀入配置前調用*/  NULL,          /* 讀入配置后調用*/  NULL,         /* 創建全局部分配置時調用 */  NULL,         /* 初始化全局部分的配置時調用*/  NULL,         /* 創建虛擬主機部分的配置時調用*/  NULL,         /* 與全局部分配置合并時調用 */  NULL,         /* 創建位置部分的配置時調用 */  NULL         /* 與主機部分配置合并時調用*/ }; 

d.ngx_module_t定義結構體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.處理函數,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 //可以訪問到客戶端的頭部和不久要發送的回復頭部 {  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模塊里面涉及最重要的數據就是ngx_module_t指針數組,這個指針數組包含了當前編譯版本支持的所有模塊,這個指針數組定義實在自動腳本生成的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  }; 

 

這里只有每個模塊變量的聲明,并且每個模塊的定義都包含在自己的模塊文件當中,比如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里面非常相似了,沒錯,他們都是模塊,唯一的不同點就是helloworld是你另外加進去的。
到現在位置也只是初探nginx的模塊,最后提一張別人畫的nginx的模塊圖,有助于接下來的學習。



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
久久久噜久噜久久综合| 日韩极品精品视频免费观看| 久久久国产一区二区| 亚洲人成网站777色婷婷| 日韩在线观看免费网站| 欧美国产欧美亚洲国产日韩mv天天看完整| 国产主播在线一区| y97精品国产97久久久久久| 亚洲图片欧洲图片av| 95av在线视频| 久久久久久久久久久免费精品| 国产成人+综合亚洲+天堂| 久久网福利资源网站| 在线日韩av观看| 亚洲第一区在线| 欧美一级高清免费| 国产成人拍精品视频午夜网站| 日韩精品视频免费| 97久久久免费福利网址| 亚洲视频在线观看网站| 国产精品久久久久久超碰| 国产九九精品视频| 国产一区二区成人| 欧美高清激情视频| 亚洲欧美国产一区二区三区| 91人人爽人人爽人人精88v| 国产视频精品va久久久久久| www欧美xxxx| 日韩欧美在线观看视频| 欧美大片在线免费观看| 成人深夜直播免费观看| 欧美不卡视频一区发布| 中文字幕在线日韩| 欧美成人免费全部观看天天性色| 中文字幕综合一区| 日本免费久久高清视频| 国产欧美亚洲视频| 亚洲男人天堂古典| 2020欧美日韩在线视频| 777午夜精品福利在线观看| 色综合男人天堂| 在线视频中文亚洲| 日韩精品在线观看视频| 亚洲黄色免费三级| 亚洲人成电影在线观看天堂色| 久久久综合免费视频| 欧美在线免费视频| 久久久伊人日本| 日本国产一区二区三区| 成人国产在线视频| 久久夜色精品国产亚洲aⅴ| 午夜精品久久久久久久99热| 亚洲视频在线看| 久久久久久噜噜噜久久久精品| 久久人人爽人人爽人人片av高请| 久久国产加勒比精品无码| 国产在线精品播放| 国产免费久久av| 欧美老肥婆性猛交视频| 欧美性猛交视频| 成人激情视频在线| 亚洲国产精品热久久| 久久久久久久久久国产精品| 91久久久国产精品| 欧美黄色性视频| 欧美在线激情网| 久久偷看各类女兵18女厕嘘嘘| 日本高清不卡的在线| 欧美一级大片视频| 欧美激情一区二区久久久| 欧美裸身视频免费观看| 青草青草久热精品视频在线网站| 日韩电视剧免费观看网站| 亚洲大胆美女视频| 国产精品久久久久久久午夜| 欧美午夜无遮挡| 97国产一区二区精品久久呦| 日韩在线观看免费高清完整版| 91在线观看欧美日韩| 日本久久久久久久久| 性欧美暴力猛交69hd| 国产免费一区二区三区在线观看| 久久91亚洲精品中文字幕| 国产精品午夜视频| 亚洲天堂av电影| 欧美电影在线免费观看网站| 福利一区福利二区微拍刺激| 国产在线精品成人一区二区三区| 久久久久久久久久久久av| 欧美一级高清免费播放| 亚洲黄色av网站| 色偷偷噜噜噜亚洲男人的天堂| 乱亲女秽乱长久久久| 久久五月天综合| 国产中文日韩欧美| 色综合亚洲精品激情狠狠| 欧美成人激情图片网| 超在线视频97| 精品久久久久久中文字幕一区奶水| 欧美精品日韩三级| 欧美一级淫片丝袜脚交| 国产视频综合在线| 国产深夜精品福利| 欧美视频在线观看免费网址| 久久久久999| 中文字幕视频在线免费欧美日韩综合在线看| 欧美性猛交xxxx黑人猛交| 亚洲视频日韩精品| 美女扒开尿口让男人操亚洲视频网站| 国产一区玩具在线观看| 日韩精品在线私人| 成人欧美在线视频| 欧美午夜影院在线视频| 亚洲激情久久久| 久久久亚洲成人| 久久夜色精品国产亚洲aⅴ| 国产一区欧美二区三区| 久久人人爽人人爽人人片亚洲| 亚洲电影av在线| 成人av.网址在线网站| 精品国产91久久久久久老师| 91精品久久久久久久久不口人| 国产欧美最新羞羞视频在线观看| 精品人伦一区二区三区蜜桃网站| 中文字幕免费国产精品| 欧美激情视频在线观看| 中文字幕av一区二区| 欧美xxxx18性欧美| 久久精品国产亚洲7777| 亚洲午夜激情免费视频| 欧美亚洲午夜视频在线观看| 日韩精品免费在线| 久久视频免费观看| 久久久久这里只有精品| 夜夜嗨av一区二区三区四区| 欧美视频精品一区| 欧美在线一区二区三区四| 97在线视频免费观看| 国产美女扒开尿口久久久| 日韩精品视频观看| 精品香蕉在线观看视频一| 亚洲人成电影在线观看天堂色| 亚洲成人中文字幕| 国产福利视频一区二区| 懂色aⅴ精品一区二区三区蜜月| 亚洲а∨天堂久久精品喷水| 91国内产香蕉| 欧美激情网站在线观看| 亚洲精品在线视频| 中文字幕视频在线免费欧美日韩综合在线看| 国产精品久久久久久久久粉嫩av| 伦伦影院午夜日韩欧美限制| 91在线播放国产| 国产亚洲精品美女久久久久| 亚洲精选中文字幕| 欧美国产一区二区三区| 亚洲男人av电影| 2019亚洲日韩新视频| 欧美日韩国产一区二区三区| 日韩欧亚中文在线| 色婷婷av一区二区三区在线观看| 欧美性20hd另类| 91视频九色网站| 日韩在线观看免费高清|