nginx在cygwin情况下hello,world

    技术2026-01-10  1

     

          最近看了一些nginx运行hello,world的资料,然后自己总结了一下。

    这里只介绍使用cygwin的情况。

          在windows平台上装好cygwin以后,运行cygwin,如果想进windows下的C,D,E,F目录,则必须cd /cygdrive,进入以后用ls命令就可以看见C,D,E,F盘了,然后下好nginx源码。

          准备工作做好以后,就可以开始自己的hello,world模块了,首先建一个目录,用来存放自己的模块,eg:/cygdrive/e/hello_world

    首先创建一个config文件,这个文件被nginx,configure的时候识别,并告知把这个模块编译了。

    eg:

        ngx_addon_name=ngx_hello_world_module HTTP_AUX_FILTER_MODULES="$HTTP_MODULES ngx_hello_world_module" NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_hello_world_module.c" 注意:模块的名字和c文件的位置 

     

     

    下面就是ngx-hello_world_module.c

    #include <ngx_core.h> #include <ngx_http.h> #include <nginx.h> //这些头文件不用管是否存在,当configure运行,记载这个模块的时候,就做了这件事,如果要单独 检查这个模块是否能编译通过,则必须加上头文件,在nginx源码里面有。 static char *ngx_hello_world(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); //回调函数 static ngx_int_t ngx_hello_world_get_output(ngx_http_request_t *r, char *out_buf); static ngx_int_t ngx_hello_world_handler(ngx_http_request_t *r); //事件处理函数 /* Commands */ static ngx_command_t ngx_hello_world_commands[] = { { ngx_string("hello_world"), //这个名字很重要,当在nginx.conf配置位置的时候必须对应 NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS, ngx_hello_world, //回调函数 NGX_HTTP_LOC_CONF_OFFSET, 0, NULL }, ngx_null_command }; static ngx_http_module_t ngx_hello_world_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 */ }; /* hook */ ngx_module_t ngx_hello_world_module = { NGX_MODULE_V1, &ngx_hello_world_module_ctx, /* module context */ ngx_hello_world_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_hello_world_get_output(ngx_http_request_t *r, char *out_buf){ sprintf(out_buf, "%s", "Hello World!"); return NGX_OK; } static ngx_int_t ngx_hello_world_handler(ngx_http_request_t *r) //模块的核心部分 { ngx_int_t rc; ngx_buf_t *b; ngx_chain_t out; /* Http Output Buffer */ char out_buf[20] = {0}; if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) { return NGX_HTTP_NOT_ALLOWED; } rc = ngx_http_discard_request_body(r); if (rc != NGX_OK && rc != NGX_AGAIN) { return rc; } //定制头信息 r->headers_out.content_type.len = sizeof("text/html") - 1; r->headers_out.content_type.data = (u_char *) "text/html"; //制定要恢复的头部 if (r->method == NGX_HTTP_HEAD) { rc = ngx_http_send_header(r); if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { return rc; } } //获取输出的body ngx_hello_world_get_output(r, out_buf); b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); if (b == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } out.buf = b; out.next = NULL; b->pos = (u_char *)out_buf; //内容 b->last = (u_char *)out_buf + strlen(out_buf); b->memory = 1; b->last_buf = 1; r->headers_out.status = NGX_HTTP_OK; r->headers_out.content_length_n = strlen(out_buf); rc = ngx_http_send_header(r); if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { return rc; } return ngx_http_output_filter(r, &out); } static char * ngx_hello_world(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_http_core_loc_conf_t *clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); /* register hanlder */ clcf->handler = ngx_hello_world_handler; return NGX_CONF_OK; } 

    注意:在C文件中的最后一行必须回车

     

    进入nginx目录

    ./configure --prefix=/cygdrive/d/server/nginx --with-cc-opt="-D FD_SETSIZE=2048"

    --prefix指定nginx生成目录,还要log,配置文件等。

    make && make install

     

    如果不加–with-cc-opt=”-D FD_SETSIZE=2048”的话,你装好nginx后运行会出现 

     2496#0: the maximum number of files supported by select() is 64的错误提示,

     这表示FD_SETSIZE的值比nginx配置文件中worker_connections指令所指定的值,你可以把nginx.conf里的

     worker_connections选项改小一些,比如44,加了–with-cc-opt=”-DFD_SETSIZE=2048”后就不会碰到这问题 

     

    最后启动nginx以后,在浏览器里面输入http://localhost/hello,就能看见hello,world

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    最新回复(0)