从零手写实现 nginx-21-modules 模块

实现一个Nginx模块涉及多个步骤,包括模块初始化、配置解析、日志记录等。以下是一个简单的Nginx模块骨架,它可以作为开始手写模块的起点。




#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
 
// 模块的 context 结构体
typedef struct {
    ngx_str_t test_str;
} ngx_http_mytest_conf_t;
 
// 解析配置项的回调函数
static char *ngx_http_mytest(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_mytest_handler;
 
    // 获取配置项参数
    ngx_str_t *value = cf->args->elts;
    ngx_http_mytest_conf_t *lcf = conf;
    lcf->test_str = value[1];
 
    return NGX_CONF_OK;
}
 
// 模块处理请求的回调函数
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r) {
    if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
        return NGX_HTTP_NOT_ALLOWED;
    }
 
    ngx_int_t rc = ngx_http_discard_request_body(r);
    if (rc != NGX_OK) {
        return rc;
    }
 
    // 设置响应头
    ngx_str_t *test_str = ...; // 从配置中获取字符串
    ngx_table_elt_t *h = ngx_list_push(&r->headers_out.headers);
    if (h == NULL) {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }
 
    h->key = ngx_http_mytest_header_name;
    h->value = *test_str;
    h->hash = 1; // 计算hash值
 
    // 发送响应
    ngx_int_t status = NGX_HTTP_OK;
    ngx_str_t response = ngx_http_mytest_response;
    ngx_buf_t *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->memory = 1;
    b->last_buf = 1;
 
    r->headers_out.status = status;
    r->headers_out.content_length_n = response.len;
    r->headers_out.content_type_len = sizeof("text/plain") - 1;
    ngx_str_set(&r->headers_out.content_type, "text/plain");
 
    return ngx_http_send_header(r) == NGX_ERROR
        ? NGX_HTTP_INTERNAL_SERVER_ERROR
        : ngx_http_output_filter(r, b);
}
 
// 模块的定义
ngx_module_t ngx_http_mytest_module = {
    ..., // 模块的各种方法
    ngx_http_mytest_create_conf, // 创建配置结构体的方法
    ngx_http_mytest_init_conf // 初始化配置结构体的方法
};
 
// 配置指令结构体
static ngx_command_t ngx_http_

评论已关闭

推荐阅读

Vue中使用mind-map实现在线思维导图
2024年08月04日
VUE
Web前端最全Vue实现免密登录跳转的方式_vue怎么样不登录返回首页,最强技术实现
2024年08月04日
VUE
vue3 项目搭建教程(基于create-vue,vite,Vite + Vue)
2024年08月04日
VUE
Vue-颜色选择器实现方案——>Vue-Color( 实战*1+ Demo*7)
2024年08月04日
VUE
Vue项目卡顿慢加载?这些优化技巧告诉你!_vue数据多渲染卡顿
2024年08月04日
VUE
vue中的keep-alive详解与应用场景
2024年08月04日
VUE
Vue、React实现excel导出功能(三种实现方式保姆级讲解)
2024年08月04日
vue-office/docx插件实现docx文件预览
2024年08月04日
VUE
java调用js文件的两种方法(支持V8引擎)
2024年08月04日
JavaScript:解决计算精度问题/mathjs/bignumber.js/big.js/decimal.js
2024年08月04日
两周从爬虫小白变大神 _yjs_js_security_passport
2024年08月04日
JS笔记(对象、函数、数组)
2024年08月04日
Markdown.js:强大的纯JavaScript Markdown解析器
2024年08月04日
Vue项目:js模拟点击a标签下载文件并重命名,URL文件地址下载方法、请求接口下载文件方法总结。
2024年08月04日
vue 父组件怎么获取子组件里面的data数据
2024年08月04日
VUE
个人开发实现AI套壳网站快速搭建(Vue+elementUI+SpringBoot)
2024年08月04日
el-table 表格封装并改造实现单元格可编辑
2024年08月04日
none
nodejs环境下创建vue项目、SSH密钥登陆!!!
2024年08月04日
vue+quill+element-ui实现视频、图片上传及缩放保姆级教程,轻松使用富文本
2024年08月04日
【three.js】22. Imported Models导入模型
2024年08月04日