侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Nginx服务器中的模块编写及相关内核源码初探

linux  /  管理员 发布于 7年前   125

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的模块图,有助于接下来的学习。


  • 上一条:
    在Nginx服务器上屏蔽IP的一些基本配置方法分享
    下一条:
    使用AWS的ELB服务时为Nginx启用代理协议的步骤讲解
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在Linux系统中使用Iptables实现流量转发功能流程步骤(0个评论)
    • vim学习笔记-入门级需要了解的一些快捷键(0个评论)
    • 在centos7系统中实现分区并格式化挂载一块硬盘到/data目录流程步骤(0个评论)
    • 在Linux系统种查看某一个进程所占用的内存命令(0个评论)
    • Linux中grep命令中的10种高级用法浅析(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-11
    • 2017-07
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-03
    • 2022-04
    • 2022-08
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-06
    • 2023-07
    • 2023-10
    • 2023-12
    • 2024-01
    • 2024-04
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客