分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

1. nginx添加自定义http模块(简单)

发布时间:2023-09-06 02:30责任编辑:熊小新关键词:nginxhttp

 步骤

1. 新建模块目录
2. 添加模块配置文件
3. 编写模块源码文件
4. 在主配置文件中配置访问location
5. 编译加入模块文件
6. 测试

新建模块目录

mkdir /opt/nginx/ext/hello_world/

  

添加模块配置文件

vim /opt/nginx/ext/hello_world/config# 添加如下内容ngx_addon_name=ngx_http_hello_world_moduleHTTP_MODULES="$HTTP_MODULES ngx_http_hello_world_module"NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_hello_world_module.c"CORE_LIBS="$CORE_LIBS -lpcre"# ngx_addon_name 表示模块名称# HTTP_MODULES 表示定义HTTP_MODULES模块,多个模块以空格分开,为了不覆盖之前的模块,使用$HTTP_MODULES代表已经存在的# NGX_ADDON_SRCS 代表模块代码路径# CORE_LIBS 代表模块需要的库文件 

编写模块文件

文件直接放在模块目录下即可(vim /opt/nginx/ext/hello_world/ngx_http_hello_world_module.c)

定义HTTP模块

static ngx_command_t ngx_http_hello_world_commands[] = { ??{ ???ngx_string("hello_world"), ???NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LMT_CONF | \ ??????NGX_CONF_NOARGS, ???ngx_http_hello_world, ???NGX_HTTP_LOC_CONF_OFFSET, ???0, ?????NULL ?}, ???ngx_null_command};// hello_world: 这个与nginx.conf中定义的location中的名字一致// ?NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LMT_CONF | NGX_CONF_NOARGS: 指定出现的位置,可以出现在location {}中// nginx_http_hello_world: 这个ngx_command_t中的set成员,当某块位置出现hello_world时候,nginx将会启动ngx_http_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 */};ngx_module_t ngx_http_hello_world_module = { ??NGX_MODULE_V1, ?&ngx_http_hello_world_module_ctx, ?ngx_http_hello_world_commands, ?NGX_HTTP_MODULE, ?NULL, ?NULL, ?NULL, ?NULL, ?NULL, ?NULL, ?NULL, ?NGX_MODULE_V1_PADDING};// NGX_MODULE_V1 代表版本号// ngx_http_hello_world_module_ctx 代表模块上下文结构,HTTP框架要求,固定用法// ngx_http_hello_world_commands 代表模块的配置文件参数,这个在nginx.conf文件解析// NGX_HTTP_MODULE 代表这是一个http模块

  

定义模块触发函数

static char * ngx_http_hello_world(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){ ???ngx_http_core_loc_conf_t *clcf ; ???clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); ???clcf->handler = ngx_http_hello_world_handler; ???return NGX_CONF_OK;}// ngx_http_conf_get_module_loc_conf: 这个函数会首先找到hello_world配置所属的配置块// handler: http框架在处理用户请求进行到NGX_HTTP_CONTENT_PHASE阶段,如果请求的主机域名,URL和hello_world相匹配,那么就调用这handler指向的方法ngx_http_hello_world_handler

  

编写请求处理方法handler

static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r){ ???ngx_int_t rc = ngx_http_discard_request_body(r); ???if (rc != NGX_OK) { ???????return rc; ???} ???ngx_str_t type = ngx_string("text/plain"); ???ngx_str_t response = ngx_string("Hello World"); ???r->headers_out.status = NGX_HTTP_OK; ???r->headers_out.content_length_n = response.len; ???r->headers_out.content_type = type; ???rc = ngx_http_send_header(r);//发送头部 ???if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { ???????return rc; ???} ???ngx_buf_t *b; ???b = ngx_create_temp_buf(r->pool, response.len);//异步发送,要用堆内存空间 ???if (b == NULL) { ???????return NGX_HTTP_INTERNAL_SERVER_ERROR; ???} ???ngx_memcpy(b->pos, response.data, response.len); ???b->last = b->pos + response.len; ???b->last_buf = 1; ???ngx_chain_t out; ???out.buf = b; ???out.next = NULL; ???return ngx_http_output_filter(r, &out);//向用户发送响应包}

  

完整的代码汇总

#include <ngx_config.h>#include <ngx_core.h>#include <ngx_http.h>static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t *r);static char* ngx_http_hello_world(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);static ngx_command_t ngx_http_hello_world_commands[] = {{ ???ngx_string("hello_world"), ???NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LMT_CONF | ????NGX_CONF_NOARGS, ???ngx_http_hello_world, ???NGX_HTTP_LOC_CONF_OFFSET, ???0, ???NULL ???}, ???ngx_null_command};static ngx_http_module_t ngx_http_hello_world_module_ctx = { ???NULL, ???NULL, ???NULL, ???NULL, ???NULL, ???NULL, ???NULL, ???NULL};ngx_module_t ngx_http_hello_world_module = { ???NGX_MODULE_V1, ???&ngx_http_hello_world_module_ctx, ???ngx_http_hello_world_commands, ???NGX_HTTP_MODULE, ???NULL, ???NULL, ???NULL, ???NULL, ???NULL, ???NULL, ???NULL, ???NGX_MODULE_V1_PADDING};static char *ngx_http_hello_world(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ???ngx_http_core_loc_conf_t *clcf; ???clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); ???clcf->handler = ngx_http_hello_world_handler; ???return NGX_CONF_OK;}static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t *r) { ???ngx_int_t rc = ngx_http_discard_request_body(r); ???if (rc != NGX_OK) { ???return rc; ???} ???ngx_str_t type = ngx_string("text/plain"); ???ngx_str_t response = ngx_string("Hello World"); ???r->headers_out.status = NGX_HTTP_OK; ???r->headers_out.content_length_n = response.len; ???r->headers_out.content_type = type; ???rc = ngx_http_send_header(r); ???if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { ???return rc; ???} ???ngx_buf_t *b; ???b = ngx_create_temp_buf(r->pool, response.len); ???if (b == NULL) { ???return NGX_HTTP_INTERNAL_SERVER_ERROR; ???} ???ngx_memcpy(b->pos, response.data, response.len); ???b->last = b->pos + response.len; ???b->last_buf = 1; ???ngx_chain_t out; ???out.buf = b; ???out.next = NULL; ???return ngx_http_output_filter(r, &out);}

  

在主配置文件中配置访问location

vim /etc/nginx添加 location / {hello_world};server {...location / { ??????????hello_world; ???????} ???????????...}

  

编译加入模块文件

./configure --add-module=/opt/nginx/ext/hello_world/make && make install

  

测试

/usr/local/nginx/sbin/nginxcurl http://localhost

  

1. nginx添加自定义http模块(简单)

原文地址:https://www.cnblogs.com/tanghe1219/p/10286559.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved