Nginx 模块开发与扩展

Module Development and Extensions

概述

Nginx的模块化架构支持功能扩展。本文介绍常用第三方模块、动态模块加载和基础模块开发。

1. 常用第三方模块

1.1 Echo模块

load_module modules/ngx_http_echo_module.so;

server {
    listen 80;
    server_name echo.example.com;

    location /echo {
        echo "Hello from Nginx Echo Module!";
        echo "Current time: $time_local";
    }

    location /delay {
        echo_sleep 2;
        echo "Delayed response";
    }
}

1.2 Headers-More模块

load_module modules/ngx_http_headers_more_filter_module.so;

server {
    listen 80;
    server_name headers.example.com;

    more_set_headers 'Server: CustomServer/1.0';
    more_clear_headers 'X-Powered-By';

    location / {
        more_set_headers 'X-Custom-Header: Value';
        root /var/www/html;
    }
}

2. 动态模块管理

2.1 模块管理脚本

#!/bin/bash
# nginx-module-manager.sh

enable_module() {
    local module=$1
    sed -i "1i load_module modules/$module;" /etc/nginx/nginx.conf
    nginx -t && echo "Module $module enabled"
}

disable_module() {
    local module=$1
    sed -i "/load_module.*$module/d" /etc/nginx/nginx.conf
    nginx -t && echo "Module $module disabled"
}

list_modules() {
    echo "Available modules:"
    ls /usr/lib/nginx/modules/*.so
    echo "Enabled modules:"
    grep "load_module" /etc/nginx/nginx.conf
}

case "$1" in
    "enable") enable_module "$2" ;;
    "disable") disable_module "$2" ;;
    "list") list_modules ;;
esac

3. Lua脚本集成

3.1 基础Lua应用

http {
    lua_shared_dict cache 10m;

    server {
        listen 80;
        server_name lua.example.com;

        location /hello {
            content_by_lua_block {
                ngx.say("Hello from Lua!")
                ngx.say("Time: ", os.date())
            }
        }

        location /cache {
            content_by_lua_block {
                local cache = ngx.shared.cache
                local key = "data"
                local value = cache:get(key)

                if not value then
                    value = "Generated at " .. os.date()
                    cache:set(key, value, 60)
                end

                ngx.say("Cached data: " .. value)
            }
        }
    }
}

4. 自定义模块开发

4.1 Hello World模块

// 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"),
        NGX_HTTP_LOC_CONF | NGX_CONF_NOARGS,
        ngx_http_hello,
        0,
        0,
        NULL
    },
    ngx_null_command
};

static ngx_int_t
ngx_http_hello_handler(ngx_http_request_t *r)
{
    ngx_buf_t *b;
    ngx_chain_t out;
    ngx_str_t response = ngx_string("Hello World from custom module!");

    r->headers_out.content_type_len = sizeof("text/plain") - 1;
    r->headers_out.content_type.data = (u_char *) "text/plain";
    r->headers_out.status = NGX_HTTP_OK;
    r->headers_out.content_length_n = response.len;

    b = ngx_calloc_buf(r->pool);
    b->pos = response.data;
    b->last = response.data + response.len;
    b->memory = 1;
    b->last_buf = 1;

    out.buf = b;
    out.next = NULL;

    ngx_http_send_header(r);
    return ngx_http_output_filter(r, &out);
}

4.2 编译和测试

# 编译模块
./configure --add-module=/path/to/hello_module
make && sudo make install

# 测试脚本
#!/bin/bash
test_module() {
    nginx -t && echo "Config OK"
    curl http://localhost/hello
}

小结

掌握了:

  1. 第三方模块的安装和使用
  2. 动态模块管理
  3. Lua脚本集成
  4. 自定义模块开发基础
  5. 模块测试和调试
powered by Gitbook© 2025 编外计划 | 最后修改: 2025-08-29 15:40:15

results matching ""

    No results matching ""