作者信息:https://home.cnblogs.com/u/huangjiabobk

在Linux中,如何编写一个Nginx的access模块,要求准许192.168.3.29/24的机器访问,准许10.1.20.6/16这个网段的所 有机器访问,准许34.26.157.0/24这个网段访问,除此之外的机器不准许访问。

如果你确实需要编写一个自定义的 access 模块,以下是一个简化的示例,展示如何实现这个功能。这个示例将帮助你理解 Nginx 模块开发的基本步骤。

1.环境准备

确保你已经安装了 Nginx 的开发环境,包括 Nginx 的源代码和编译工具。

sudo apt-get update
sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev

下载 Nginx 源代码:

wget http://nginx.org/download/nginx-1.24.0.tar.gz
tar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0
2.编写自定义模块代码

创建一个目录来存放你的模块代码,例如 ngx_http_custom_access_module

mkdir ngx_http_custom_access_module
cd ngx_http_custom_access_module

创建模块的源代码文件 ngx_http_custom_access_module.c

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

static ngx_int_t ngx_http_custom_access_handler(ngx_http_request_t *r);

static ngx_http_module_t ngx_http_custom_access_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_custom_access_module = {
    NGX_MODULE_V1,
    &ngx_http_custom_access_module_ctx, /* module context */
    NULL,                              /* 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_custom_access_handler(ngx_http_request_t *r) {
    struct sockaddr_in *sin;
    ngx_str_t allowed_ips[] = {
        ngx_string("192.168.3.29"),
        ngx_string("10.1.20.6"),
        ngx_string("34.26.157.0")
    };
    ngx_uint_t i;

    sin = (struct sockaddr_in *)r->connection->sockaddr;
    ngx_uint32_t client_ip = ntohl(sin->sin_addr.s_addr);

    for (i = 0; i < sizeof(allowed_ips) / sizeof(ngx_str_t); i++) {
        ngx_uint32_t allowed_ip;
        if (inet_pton(AF_INET, allowed_ips[i].data, &allowed_ip) == 1) {
            if (client_ip == ntohl(allowed_ip)) {
                return NGX_OK;
            }
        }
    }

    ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                  "access denied for client %s", r->connection->addr_text->data);
    return NGX_HTTP_FORBIDDEN;
}
3.编译 Nginx 并添加自定义模块

在 Nginx 源代码目录中,运行以下命令来编译 Nginx 并添加你的自定义模块:

./configure --add-module=../ngx_http_custom_access_module
make
sudo make install
4.配置 Nginx 使用自定义模块

编辑 Nginx 配置文件(通常位于 /usr/local/nginx/conf/nginx.conf),在 http 块中添加你的模块:

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            custom_access;
            root   html;
            index  index.html index.htm;
        }
    }
}
5.重新加载 Nginx

重新加载 Nginx 以应用配置更改:

sudo /usr/local/nginx/sbin/nginx -s reload
6.注意事项
  • 这个示例模块仅支持单个 IP 地址的匹配,不支持子网掩码。如果需要支持子网掩码,需要扩展代码逻辑。
  • 编写 Nginx 模块需要对 C 语言和 Nginx 的内部架构有深入的了解。建议在开发自定义模块之前,仔细阅读 Nginx 的开发文档。

综上所述,通过以上步骤,你可以编写一个简单的 Nginx 自定义模块来实现特定的访问控制功能。

posted @ 2025-03-23 22:52  黄嘉波  阅读(42)  评论(0)    收藏  举报
版权声明:原创作品,谢绝转载!否则将追究法律责任。--作者 黄嘉波