Nginx可以很方便的增加第三方模块,下面以Hello Word为例,简单介绍一下具体步骤。
1. 把模块源代码全部放在同一目录下,比如当前的目录为:/data1/xirui.hw/nginx/nginx-hello-world
2. nginx-hello-world下,编写config文件,config文件是一可执行脚本,开发一个最简单的HTTP模块,至少需要定义以下变量:
ngx_add_addon_name:nginx configure执行时使用,记录当前模块名称
HTTP_MODULES:HTTP模块名称,多个模块名称用空格分开,增加新模块时用“$HTTP_MODULES ngx_http_xx_module”的方式,以免覆盖旧模块
NGX_ADDON_SRCS:指定新增模块源码,多个源码文件用空格分开,可以用$ngx_addon_dir变量 <其为configure时指定的--add-module=PATH参数>
具体如下:
点击(此处)折叠或打开
-
ngx_addon_name="ngx_http_hello_module"
-
HTTP_MODULES="$HTTP_MODULES ngx_http_hello_module"
-
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_hello.c"
3. nginx configure执行时,加入参数--add-module=PATH(新增模块源代码以及config文件存放目录)
-
./configure --prefix=/data1/xirui.hw/nginx/nginx_install --add-module=/data1/xirui.hw/nginx/nginx-hello-world
4. 修改nginx.conf,在http->server配置块中,增加location /test { hello; }配置项
-
http {
-
..........
-
-
server {
-
listen 8080;
-
server_name localhost;
-
-
location /test {
-
hello;
-
}
-
}
-
-
location / {
-
root html;
-
index index.html index.htm;
-
}
-
........
5. 源码编写,具体如下:
点击(此处)折叠或打开
-
#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 char *ngx_http_hello(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
-
-
static ngx_command_t ngx_http_hello_commands[] = {
-
{
-
ngx_string("hello"),
-
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LMT_CONF | NGX_CONF_NOARGS,
-
ngx_http_hello,
-
NGX_HTTP_LOC_CONF_OFFSET,
-
0,
-
NULL
-
},
-
ngx_null_command
-
};
-
-
static ngx_http_module_t ngx_http_hello_module_ctx = {
-
NULL,
-
NULL,
-
NULL,
-
NULL,
-
NULL,
-
NULL,
-
NULL,
-
NULL
-
};
-
-
ngx_module_t ngx_http_hello_module = {
-
NGX_MODULE_V1,
-
&ngx_http_hello_module_ctx,
-
ngx_http_hello_commands,
-
NGX_HTTP_MODULE,
-
NULL,
-
NULL,
-
NULL,
-
NULL,
-
NULL,
-
NULL,
-
NULL,
-
NGX_MODULE_V1_PADDING
-
};
-
-
static char *ngx_http_hello(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_http_hello_handler;
-
-
return NGX_CONF_OK;
-
}
-
-
static ngx_int_t ngx_http_hello_handler(ngx_http_request_t *r)
-
{
-
if (!(r->method & (NGX_HTTP_GET | NGX_HTTP_HEAD))) {
-
return NGX_HTTP_NOT_ALLOWED;
-
}
-
-
ngx_int_t rc = ngx_http_discard_request_body(r);
-
if (rc != NGX_OK) {
-
return rc;
-
}
-
-
ngx_str_t type = ngx_string("text/plain");
-
ngx_str_t response = ngx_string("Hello World!");
-
r->headers_out.status = NGX_HTTP_OK;
-
r->headers_out.content_length_n = response.len;
-
r->headers_out.content_type = type;
-
-
rc = ngx_http_send_header(r);
-
if(rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
-
return rc;
-
}
-
-
ngx_buf_t *b = ngx_create_temp_buf(r->pool,response.len);
-
if (b == NULL) {
-
return NGX_HTTP_INTERNAL_SERVER_ERROR;
-
}
-
-
ngx_memcpy(b->pos, response.data, response.len);
-
b->last = b->pos + response.len;
-
b->last_buf = 1;
-
-
ngx_chain_t out;
-
out.buf = b;
-
out.next = NULL;
-
-
return ngx_http_output_filter(r, &out);
-
}
6. 编译安装
点击(此处)折叠或打开
-
./configure --prefix=/data1/xirui.hw/nginx/nginx_install --add-module=/data1/xirui.hw/nginx/nginx-hello-world
-
make
-
make install
7. 启动Nginx,浏览器输入,可以看到输出
Hello World!
阅读(1256) | 评论(0) | 转发(0) |