当前位置: 首页 > news >正文

文创做的好的网站推荐企业邮箱正确的写法

文创做的好的网站推荐,企业邮箱正确的写法,国外旅游网站排名,thegem wordpress主题文章目录 什么是过滤模块Nginx相关数据结构介绍ngx_module_t的数据结构ngx_http_module_t数据结构ngx_command_s数据结构 相关宏定义filter#xff08;过滤器#xff09;实现Nginx模块开发流程Nginx 模块执行具体实现流程create_loc_confmerge_loc_confpostconfiguration修改… 文章目录 什么是过滤模块Nginx相关数据结构介绍ngx_module_t的数据结构ngx_http_module_t数据结构ngx_command_s数据结构 相关宏定义filter过滤器实现Nginx模块开发流程Nginx 模块执行具体实现流程create_loc_confmerge_loc_confpostconfiguration修改header信息修改body信息 示例代码编写config文件编译模块到Nginx源码中执行效果 总结 什么是过滤模块 Nignx是一个代理服务器 他前端被客户端请求后端连接服务器。这里涉及的数据处理大概有 客户端请求数据Nginx直接返回handler 模块客户端请求数据Nginx转发给服务器upstream 模块服务器返回数据Nginx转发给客户端filter 模块 Nginx相关数据结构介绍 ngx_module_t的数据结构 struct ngx_module_s {ngx_uint_t ctx_index; //是哪个进程ngx_uint_t index; //进程idchar *name;ngx_uint_t spare0;ngx_uint_t spare1;ngx_uint_t version; //版本号const char *signature; //签名证书void *ctx;//上下文ngx_command_t *commands;//命令ngx_uint_t type;// nginx模块类型ngx_int_t (*init_master)(ngx_log_t *log);//ngx_int_t (*init_module)(ngx_cycle_t *cycle); //模块启动时候ngx_int_t (*init_process)(ngx_cycle_t *cycle); //进程启动时候ngx_int_t (*init_thread)(ngx_cycle_t *cycle);void (*exit_thread)(ngx_cycle_t *cycle);void (*exit_process)(ngx_cycle_t *cycle);void (*exit_master)(ngx_cycle_t *cycle);uintptr_t spare_hook0;uintptr_t spare_hook1;uintptr_t spare_hook2;uintptr_t spare_hook3;uintptr_t spare_hook4;uintptr_t spare_hook5;uintptr_t spare_hook6;uintptr_t spare_hook7; }; typedef struct ngx_module_s ngx_module_t;ngx_http_module_t数据结构 typedef struct {void **main_conf;void **srv_conf;void **loc_conf; } ngx_http_conf_ctx_t;typedef struct {ngx_int_t (*preconfiguration)(ngx_conf_t *cf); // 解析配置文件之前ngx_int_t (*postconfiguration)(ngx_conf_t *cf); // 解析配置文件完成之后 // **_main_ **解析配置文件中http关键字的内部void *(*create_main_conf)(ngx_conf_t *cf); char *(*init_main_conf)(ngx_conf_t *cf, void *conf); // **_srv_ **解析配置文件中server关键字的内部void *(*create_srv_conf)(ngx_conf_t *cf); char *(*merge_srv_conf)(ngx_conf_t *cf, void *prev, void *conf); // **_loc_ **解析配置文件中location关键字的内部void *(*create_loc_conf)(ngx_conf_t *cf);char *(*merge_loc_conf)(ngx_conf_t *cf, void *prev, void *conf); } ngx_http_module_t;ngx_command_s数据结构 struct ngx_command_s {ngx_str_t name;ngx_uint_t type;char *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);ngx_uint_t conf;ngx_uint_t offset;void *post; };相关宏定义 #define NGX_MODULE_V1 \NGX_MODULE_UNSET_INDEX, NGX_MODULE_UNSET_INDEX, \NULL, 0, 0, nginx_version, NGX_MODULE_SIGNATURE#define NGX_MODULE_V1_PADDING 0, 0, 0, 0, 0, 0, 0, 0#define NGX_HTTP_MODULE 0x50545448 /* HTTP 模块*//* 以下宏定义为了去确定该项配置属于哪个类目下 比如service 比如location */ #define NGX_HTTP_MAIN_CONF 0x02000000 #define NGX_HTTP_SRV_CONF 0x04000000 #define NGX_HTTP_LOC_CONF 0x08000000 #define NGX_HTTP_UPS_CONF 0x10000000 #define NGX_HTTP_SIF_CONF 0x20000000 #define NGX_HTTP_LIF_CONF 0x40000000 #define NGX_HTTP_LMT_CONF 0x80000000filter过滤器实现 Nginx模块开发流程 1定义一个模块名ngx_module_t选择好http模块NGX_HTTP_MODULE。 2定义cmd命令有多少条cmd写多少条cmdngx_command_t。 3定义用来解析http blockngx_http_module_t。 4执行过程实现添加模块。 Nginx 模块执行 1初始化。当进程启动的时候进行的模块初始化。 2解析conf文件。解析conf文件中模块的相关命令和设置。 3Nginx启动之后有命令或请求到来时处理请求的流程。 开发模块时需要实现的主要是这三个流程的功能。 具体实现流程 create_loc_conf 内存池中分配一片kong空间用以存储配置文件中指令对应的值 // void *(*create_loc_conf)(ngx_conf_t *cf); // 解析conf文件location关键字之前的动作 void *ngx_http_fly_filter_create_loc_conf(ngx_conf_t *cf) {ngx_http_filter_conf_t *conf ngx_palloc(cf-pool, sizeof(ngx_http_filter_conf_t));if (conf NULL)return NULL;conf-enable NGX_CONF_UNSET;return conf; }merge_loc_conf char *ngx_http_fly_filter_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) {ngx_http_filter_conf_t *prev (ngx_http_filter_conf_t*)parent;//如果prefix中是on那么next-enable的值就为1这个是在ngx_conf_set_flag_slot//函数中设置的即可以理解为将配置//文件中的on或者off转换为nginx内存中的1或者0ngx_http_filter_conf_t *next (ngx_http_filter_conf_t*)child;ngx_conf_merge_value(next-enable, prev-enable, 0);return NGX_CONF_OK; }其中 ngx_conf_merge_value #define ngx_conf_merge_value(conf, prev, default) \if (conf NGX_CONF_UNSET) { \conf (prev NGX_CONF_UNSET) ? default : prev; \postconfiguration 解析完毕conf文件后执行该指令设置运行时候的回调函数使用头插法将header_filter 与 body_filter插入filter队列的头部 // ngx_int_t (*postconfiguration)(ngx_conf_t *cf); // 解析完配置文件之后的动作也就是解析完http关键字模块之后 ngx_int_t ngx_http_fly_filter_init(ngx_conf_t *cf) {// 模块的初始化// http { }// O-O-O-O// 多个模块的头插法取出最前面的模块//top指向第一个next指向第二个ngx_http_next_header_filter ngx_http_top_header_filter;ngx_http_top_header_filter ngx_http_fly_header_filter;ngx_http_next_body_filter ngx_http_top_body_filter;ngx_http_top_body_filter ngx_http_fly_body_filter;return NGX_OK; }修改header信息 这里仅仅修改要回发的内容长度由于修改了body内容那么header中的length字段自然要做出相应的修改 static ngx_str_t prefix ngx_string(h2FLY. /h2); ngx_int_t ngx_http_fly_header_filter(ngx_http_request_t *r) {if (r-headers_out.status ! NGX_HTTP_OK) {// 不正常返回则进行nextreturn ngx_http_next_header_filter(r);}//r-headers_out.content_type.len sizeof(text/html)r-headers_out.content_length_n prefix.len;return ngx_http_next_header_filter(r); }修改body信息 ngx_int_t ngx_http_fly_body_filter(ngx_http_request_t *r, ngx_chain_t *chain) {/** 关于ngx_chain_t* 在nginx中有一个数据链存放要发送的数据。* O-O-O-O* 每次send的是ngx_chain_t中的一个ngx_buf_t*/// 添加一个chain bufferngx_buf_t *b ngx_create_temp_buf(r-pool, prefix.len);b-start b-pos prefix.data;b-last b-pos prefix.len;ngx_chain_t *c1 ngx_alloc_chain_link(r-pool);c1-buf b;c1-next chain;return ngx_http_next_body_filter(r, c1);}示例代码 这里主要实现了在返回的网页中添加一个内容。里面在重点地方附上了详细注释。 ngx_http_filter_module.c #include ngx_config.h #include ngx_http.h #include ngx_core.htypedef struct {ngx_flag_t enable; }ngx_http_filter_conf_t;static ngx_str_t prefix ngx_string(h2FLY. /h2);static ngx_http_output_header_filter_pt ngx_http_next_header_filter; static ngx_http_output_body_filter_pt ngx_http_next_body_filter;// ngx_int_t (*ngx_http_output_header_filter_pt)(ngx_http_request_t *r) // 添加头header ngx_int_t ngx_http_fly_header_filter(ngx_http_request_t *r) {if (r-headers_out.status ! NGX_HTTP_OK) {// 不正常返回则进行nextreturn ngx_http_next_header_filter(r);}//r-headers_out.content_type.len sizeof(text/html)r-headers_out.content_length_n prefix.len;return ngx_http_next_header_filter(r); }// ngx_int_t (*ngx_http_output_body_filter_pt)(ngx_http_request_t *r, ngx_chain_t *chain) // 添加内容body ngx_int_t ngx_http_fly_body_filter(ngx_http_request_t *r, ngx_chain_t *chain) {/** 关于ngx_chain_t* 在nginx中有一个数据链存放要发送的数据。* O-O-O-O* 每次send的是ngx_chain_t中的一个ngx_buf_t*/// 添加一个chain bufferngx_buf_t *b ngx_create_temp_buf(r-pool, prefix.len);b-start b-pos prefix.data;b-last b-pos prefix.len;ngx_chain_t *c1 ngx_alloc_chain_link(r-pool);c1-buf b;c1-next chain;return ngx_http_next_body_filter(r, c1);}// ngx_int_t (*postconfiguration)(ngx_conf_t *cf); // 解析完配置文件之后的动作也就是解析完http关键字模块之后 ngx_int_t ngx_http_fly_filter_init(ngx_conf_t *cf) {// 模块的初始化// http { }// O-O-O-O// 多个模块的头插法取出最前面的模块ngx_http_next_header_filter ngx_http_top_header_filter;ngx_http_top_header_filter ngx_http_fly_header_filter;ngx_http_next_body_filter ngx_http_top_body_filter;ngx_http_top_body_filter ngx_http_fly_body_filter;return NGX_OK; }// void *(*create_loc_conf)(ngx_conf_t *cf); // 解析conf文件location关键字之前的动作 void *ngx_http_fly_filter_create_loc_conf(ngx_conf_t *cf) {ngx_http_filter_conf_t *conf ngx_palloc(cf-pool, sizeof(ngx_http_filter_conf_t));if (conf NULL)return NULL;conf-enable NGX_CONF_UNSET;return conf; }// char *(*merge_loc_conf)(ngx_conf_t *cf, void *prev, void *conf); // 解析完配置文件location关键字之后的动作 // 模块可能在多个地方定义这个函数合并所有的值一起使用 char *ngx_http_fly_filter_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) {ngx_http_filter_conf_t *prev (ngx_http_filter_conf_t*)parent;ngx_http_filter_conf_t *next (ngx_http_filter_conf_t*)child;// 合并enable的值ngx_conf_merge_value(next-enable, prev-enable, 0);return NGX_CONF_OK; }/* struct ngx_command_s { ngx_str_t name; ngx_uint_t type; char *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); ngx_uint_t conf; ngx_uint_t offset; void *post; }; *//* // conf文件命令解析 char *ngx_http_fly_filter_set_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {char *p conf;// 对应 ngx_http_fly_filter_create_loc_conf函数的conf-enable NGX_CONF_UNSET;ngx_flag_t *flag (p cmd-offset);return NGX_CONF_OK; } */// conf文件中的每一行都是一个指令指令 ngx_command_t ngx_http_fly_filter_module_cmd[] {{//命令名称比如listen定义了就可以在conf文件中使用注意不能和其他的起冲突ngx_string(predix),// 指示name命令放的位置在哪里以及可以带多少个参数,NGX_CONF_FLAGE表示开关标志// predix on/offNGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_FLAG,// 命令解析,可以使用nginx内部的也可以自己实现ngx_conf_set_flag_slot,//ngx_http_fly_filter_set_slot,NGX_HTTP_LOC_CONF_OFFSET,// offsetof获取enable在结构体中的偏移位置offsetof(ngx_http_filter_conf_t,enable),NULL,},ngx_null_command };// 用来解析对应的conf文件其实表示的就是模块定义中的上下文 static ngx_http_module_t ngx_http_fly_filter_module_ctx {NULL,ngx_http_fly_filter_init,NULL,NULL,NULL,NULL,ngx_http_fly_filter_create_loc_conf,ngx_http_fly_filter_merge_loc_conf };// 模块定义 ngx_module_t ngx_http_fly_filter_module {NGX_MODULE_V1,ngx_http_fly_filter_module_ctx,ngx_http_fly_filter_module_cmd,// http的ascii值,指示是什么模块NGX_HTTP_MODULE,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NGX_MODULE_V1_PADDING // 填充};编写config文件 创建 touch config内容 ngx_addon_namengx_http_fly_filter_module HTTP_FILTER_MODULES$HTTP_FILTER_MODULES ngx_http_fly_filter_module NGX_ADDON_SRCS$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_filter_module.c包含三部分信息一个是模块的名称这里名称需要与代码中的定义的模块名称ngx_module_t一致第二部分是指定模块的类型和名称这里定义的是一个filter模块最后是指定模块源文件路径。 注意config文件要和模块的代码在相同目录。 编译模块到Nginx源码中 1配置中添加模块 ./configure --prefix/usr/local/nginx --with-http_realip_module --with-http_addition_module --with-http_gzip_static_module --with-http_secure_link_module --with-http_stub_status_module --with-stream --with-pcre/home/fly/workspace/pcre-8.41 --with-zlib/home/fly/workspace/zlib-1.2.11 --with-openssl/home/fly/workspace/openssl-1.1.0g --add-module/mnt/hgfs/sourcecode_learning/ngx_http_filter_module注意模块路径要正确。出现如下表示成功 configuring additional modules adding module in /mnt/hgfs/sourcecode_learning/ngx_http_filter_module ngx_http_fly_filter_module was configured creating objs/Makefile2查看是否添加模块到动态代码中 vim objs/ngx_modules.c3编译 make sudo make install执行效果 编译安装完成后在conf文件中添加模块的开关predix on worker_processes 4;events {worker_connections 1024; }http {upstream backend {server 192.168.7.146:8889;server 192.168.7.146:8890;}server {listen 8888;location / {proxy_pass http://backend;}}server {listen 8889;}server {listen 8890;predix on;}server {listen 8891;} }执行Nginx sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/fly.conf 在网页输入IP和端口执行效果如下 可以看到返回的网页中多出来添加的内容FLY.。 总结 Nginx中http模块非常多每个模块都会有ngx_http_module_t为了防止解析过程出现冲突Nginx编译的时候会把所有的模块都集中起来组织到/obj/ngx_module.c以数组的方式。在编译模块时需要编写config文件这个文件最好不要使用笔记本编辑notepad容易造成编码方式的错误。网页中常见的广告其实里面存储了图片、链接、名称信息等等其实就是通过nginx过滤器模块去实现的。
http://www.zqtcl.cn/news/404586/

相关文章:

  • 网站搭建制作建e室内设计网画图
  • 重庆市建设工程施工安全管理信息网北京seo公司网站
  • 国外做调查问卷的网站建设邮费自己的网站 要不要购买服务器的
  • 网站建设和优化排名四川建设网官网证书查询入口
  • 如何搜名字搜到自己做的网站电子商务平台icp备案证明
  • 网站建设与管理工作内容北京网站建设价
  • 做网站选哪个语言软文营销的方法
  • 青岛正规公司网站建设公司中国建设银行注册网站
  • 免费个人网站平台关键词检索
  • 定制型网站建设推广宁河网站建设
  • 主流网站开发语言有哪些电子邮件营销
  • 扫描二维码进入公司网站怎样做在万网上域名了怎么做网站
  • 销售型网站设计怎么做网站广告位
  • 网站推广的方法ppt购物网站logo
  • 网站关键词分割wordpress为展示的作品投票
  • 建立网站 域名 服务器吗wordpress超链接出错
  • 外贸开发网站建设注册会计师协会
  • 莆田建设网站dw网页设计作品及源码
  • 360免费建站视频淘宝客的网站怎么做
  • 四川自助seo建站短视频推广计划
  • 网站建设案例的公司黄冈网站建设公司
  • 做淘客网站需要营业执照吗制作网站公
  • 手机网站开发的目的鲁班设计远程工作
  • 宿迁网站建设要多少钱高密市住房和城乡建设局网站
  • 咸阳网站建设公司哪家好wordpress访客ip记录
  • 厦门建设银行网站那个网站做效果图电脑配置
  • 人才网站建设医院网站建设的好处
  • 房屋装修网站模板html5做网站
  • 网站建设需要的硬件网站建设知名公司排名
  • 绥化网站建设私自搭建vps犯法吗