以下是一个简单的示例,演示如何创建一个名为 "hello" 的 Nginx 模块,在请求处理阶段输出 "Hello, Nginx!"。

步骤 1: 创建模块源码文件

创建一个 .c 文件,例如 ngx_http_hello_module.c,包含以下源代码:
// ngx_http_hello_module.c

#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>

static ngx_int_t ngx_http_hello_handler(ngx_http_request_t *r);

static ngx_command_t ngx_http_hello_commands[] = {
    { ngx_string("hello_directive"),
      NGX_HTTP_LOC_CONF | NGX_CONF_FLAG,
      ngx_conf_set_flag_slot,
      NGX_HTTP_LOC_CONF_OFFSET,
      offsetof(ngx_http_hello_conf_t, enable),
      NULL },
    ngx_null_command
};

static ngx_http_module_t ngx_http_hello_module_ctx = {
    NULL,                           /* preconfiguration */
    NULL,                           /* postconfiguration */

    NULL,                           /* create main configuration */
    NULL,                           /* init main configuration */

    NULL,                           /* create server configuration */
    NULL,                           /* merge server configuration */

    ngx_http_hello_create_loc_conf,  /* create location configuration */
    ngx_http_hello_merge_loc_conf    /* merge location configuration */
};

ngx_module_t ngx_http_hello_module = {
    NGX_MODULE_V1,
    &ngx_http_hello_module_ctx,    /* module context */
    ngx_http_hello_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_handler(ngx_http_request_t *r) {
    ngx_buf_t *b;
    ngx_chain_t out;

    // 创建输出缓冲区
    b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
    if (b == NULL) {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    // 设置输出缓冲区
    b->pos = (u_char *) "Hello, Nginx!";
    b->last = b->pos + sizeof("Hello, Nginx!") - 1;
    b->memory = 1;
    b->last_buf = 1;

    // 将输出缓冲区添加到输出链表
    out.buf = b;
    out.next = NULL;

    // 设置响应头
    ngx_str_set(&r->headers_out.content_type, "text/plain");
    r->headers_out.status = NGX_HTTP_OK;
    r->headers_out.content_length_n = sizeof("Hello, Nginx!") - 1;

    // 发送响应
    ngx_http_send_header(r);
    return ngx_http_output_filter(r, &out);
}

static void *ngx_http_hello_create_loc_conf(ngx_conf_t *cf) {
    ngx_http_hello_conf_t *conf;

    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_hello_conf_t));
    if (conf == NULL) {
        return NGX_CONF_ERROR;
    }

    conf->enable = NGX_CONF_UNSET;

    return conf;
}

static char *ngx_http_hello_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) {
    ngx_http_hello_conf_t *prev = parent;
    ngx_http_hello_conf_t *conf = child;

    ngx_conf_merge_value(conf->enable, prev->enable, 0);

    return NGX_CONF_OK;
}

步骤 2: 修改 nginx.conf 配置文件

在 nginx.conf 文件中,添加你的模块配置和挂载位置。示例:
http {
    # ...

    server {
        listen 80;
        server_name localhost;

        location / {
            hello_directive on;  # 启用模块处理
            hello;
        }
    }

    # ...
}

步骤 3: 编译 Nginx

确保在编译 Nginx 时包含你的模块源码文件。可以通过在 configure 脚本中使用 --add-module 选项来指定:
./configure --add-module=/path/to/your/module
make
sudo make install

步骤 4: 启动 Nginx
sudo nginx

步骤 5: 测试

访问 http://localhost/,你应该会在浏览器中看到 "Hello, Nginx!" 的输出。

这是一个简单的示例,你可以根据需要进行扩展和调整。


转载请注明出处:http://www.zyzy.cn/article/detail/10148/Nginx