开发者社区> 问答> 正文

如何在Windows平台下开发Nginx模块?

如何在Windows平台下开发Nginx模块?

展开
收起
OSC开源社区 2024-05-31 15:07:40 13 0
1 条回答
写回答
取消 提交回答
  • 准备工作

    安装以下工具:
    VC2008。
    Mingw,假设安装在c:\mingw目录。
    SVN客户端。

    获取源代码

    使用SVN从svn://svn.nginx.org/nginx/trunk获取Nginx源代码,假设保存在c:\nginx目录。

    编写模块

    创建模块目录c:\ngx_test_module。

    在模块目录下新建config文件,输入以下代码:
    ngx_addon_name=ngx_test_module
    HTTP_MODULES="$HTTP_MODULES ngx_test_module"
    NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_test_module.c"

    在模块目录下新建ngx_test_module.c文件,输入以下代码:

    include

    include

    include

    static char ngx_test(ngx_conf_t cf, ngx_command_t cmd, void conf);

    static ngx_command_t ngx_test_commands[] = {

    { ngx_string("test"),
      NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
      ngx_test,
      0,
      0,
      NULL },
    
    ngx_null_command
    

    };

    static ngx_http_module_t ngx_test_module_ctx = {
    NULL,
    NULL,

    NULL,
    NULL,
    
    NULL,
    NULL,
    
    NULL,
    NULL
    

    };

    ngx_module_t ngx_test_module = {
    NGX_MODULE_V1,
    &ngx_test_module_ctx,
    ngx_test_commands,
    NGX_HTTP_MODULE,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NGX_MODULE_V1_PADDING
    };

    static ngx_str_t ngx_test_type = ngx_string("text/plain");

    static ngx_int_t
    ngx_test_handler(ngx_http_request_t *r)
    {
    ngx_http_complex_value_t cv;

    if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
        return NGX_HTTP_NOT_ALLOWED;
    }
    
    ngx_memzero(&cv, sizeof(ngx_http_complex_value_t));
    
    ngx_str_set(&cv.value, "test!");
    
    return ngx_http_send_response(r, NGX_HTTP_OK, &ngx_test_type, &cv);
    

    }

    static char
    ngx_test(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_test_handler;
    
    return NGX_CONF_OK;
    

    }

    编译

    进入Visual Studio Command Prompt。
    执行c:\mingw\msys\1.0\msys.bat进入Shell环境。
    执行rm -f /bin/link.exe来删除link.exe,避免和VC的冲突。
    切换到Nginx源代码目录。
    执行patch -p1 < nginx-windows-20120214.patch打补丁。
    执行configure脚本
    ./auto/configure --with-cc=cl --prefix= --without-http_rewrite_module --without-http_gzip_module --with-debug --add-module=../ngx_test_module
    执行make
    nmake -f objs/Makefile

    运行

    切换到objs目录。
    创建conf和logs目录。
    在conf目录新建nginx.conf文件,输入以下代码:
    daemon off;
    master_process off;
    worker_processes 1;

    error_log logs/error.log debug;

    events {
    use select;
    worker_connections 128;
    }

    http {
    server {
    listen 80;
    server_name localhost;

        location /test {
            test;
        }
    }
    

    }

    执行nginx -t测试配置文件。
    执行nginx。

    2024-05-31 15:22:34
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
TAKING WINDOWS 10 KERNEL 立即下载
CentOS Nginx PHP JAVA 多语言镜像使用手 立即下载
CentOS Nginx PHP JAVA多语言镜像使用手册 立即下载