准备工作
安装以下工具:
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文件,输入以下代码:
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;
events {
use select;
worker_connections 128;
}
http {
server {
listen 80;
server_name localhost;
location /test {
test;
}
}
}
执行nginx -t测试配置文件。
执行nginx。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。