Nginx上的第一个HelloWorld

前言(废话):

刚刚看完 深入理解nginx模块开发与架构解析的http处理模块,想想先写一个http处理模块的hellowrold,也算是入门了吧。

安装完nginx就可以搞了,安装配置请参考Nginx安装部署

第一步:编写config文件

在nginx源代码根目录下创建文件夹ngx_http_hello_world_module文件夹,进入该目录,创建文件配置文件config,在config文件中写入下列代码: 

ngx_addon_name=ngx_http_hello_world_module
HTTP_MODULES="$HTTP_MODULES ngx_http_hello_world_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_hello_world_module.c"
config是一个shell脚本,我们需要在其中定义三个变量,ngx_addon_name为要开发的模块名称:ngx_http_hello_world_module;HTTP_MODULES表示的是所有的HTTP模块的名称,通过shell 语句"$HTTP_MODULES ngx_http_hello_world_module"将我们的模块名称添加到HTTP_MODULES变量中;NGX_ADDON_SRCS表示的是新增模块的源代码路径,这个例子中NGX_ADDON_SRCS的值就是我们创建的文件夹ngx_http_hello_world_module路径。

第二步:添加配置项
向nginx中的配置文件/usr/local/nginx/conf/nginx.conf中添加下列配置项:

location /hello_world 
	{
            hello_world testing!!!;
        }
这个配置项添加在localhost server下,当我们使用 浏览器 访问http://localhost/hello_world时,就会在浏览器中显示字符“hello_world, testing!!!”。

第三步:编写模块文件

创建ngx_http_hello_world_module.c文件(文件放在刚才创建的创建的文件夹ngx_http_hello_world_module路径),编写HelloWorld程序运行需要的给各个模块,具体代码如下:

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

typedef struct 		//配置项结构体 – 每个模块相关的配置项集中管理用的结构体
{
    ngx_str_t output_words;
} ngx_http_hello_world_loc_conf_t;

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

static void* ngx_http_hello_world_create_loc_conf(ngx_conf_t* cf);

static char* ngx_http_hello_world_merge_loc_conf(ngx_conf_t* cf, void* parent, void* child);

static ngx_command_t ngx_http_hello_world_commands[] = 
{	//commands结构体,设定配置项特定的处理方式,定义该处理项出现时的处理函数
    {
	//配置项的名字
	ngx_string("hello_world"), 
	//NGX_HTTP_LOC_CONF表示指令在位置配置部分出现是合法的 ,NGX_CONF_TAKE1: 指令读入一个参数
        NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
	//遇到该指令名字时调用的函数	
        ngx_http_hello_world, 
	//存储位置,配置结构体+offset指示变量的具体存储位置
        NGX_HTTP_LOC_CONF_OFFSET,
        offsetof(ngx_http_hello_world_loc_conf_t, output_words),
        NULL
    },
    ngx_null_command
};

//实现ngx_http_module_t接口,来管理http模块的配置项,在http框架初始化时,会调用该模块定义的回调方法
static ngx_http_module_t ngx_http_hello_world_module_ctx = 
{
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    ngx_http_hello_world_create_loc_conf,
    ngx_http_hello_world_merge_loc_conf
};

//定义ngx_module_t模块,主要工作是利用前面定义的ngx_http_hello_world_module_ctx和ngx_http_hello_world_commands
//来对其中的ctx和commands成员变量进行赋值
ngx_module_t ngx_http_hello_world_module = {
    NGX_MODULE_V1,
    &ngx_http_hello_world_module_ctx,
    ngx_http_hello_world_commands,
    NGX_HTTP_MODULE,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NGX_MODULE_V1_PADDING
};

//处理请求的回调函数
static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t* r) {
    ngx_int_t rc;
    ngx_buf_t* b;
    ngx_chain_t out[2];

    ngx_http_hello_world_loc_conf_t* hlcf;
    hlcf = ngx_http_get_module_loc_conf(r, ngx_http_hello_world_module);

    r->headers_out.content_type.len = sizeof("text/plain") - 1;
    r->headers_out.content_type.data = (u_char*)"text/plain";

    b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));

    out[0].buf = b;
    out[0].next = &out[1];
    //建立ngx_buf_t,直接指向原内存地址,不对数据进行复制,节省内存
    b->pos = (u_char*)"Hello World";
    b->last = b->pos + sizeof("Hello World") - 1;
    b->memory = 1;

    b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));

    out[1].buf = b;
    out[1].next = NULL;

    b->pos = hlcf->output_words.data;
    b->last = hlcf->output_words.data + (hlcf->output_words.len);
    b->memory = 1;
    b->last_buf = 1;

    r->headers_out.status = NGX_HTTP_OK;
    r->headers_out.content_length_n = hlcf->output_words.len + sizeof("hello_world, ") - 1;
    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[0]);
}

static void* ngx_http_hello_world_create_loc_conf(ngx_conf_t* cf) {
    ngx_http_hello_world_loc_conf_t* conf;

    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_hello_world_loc_conf_t));
    if (conf == NULL) {
        return NGX_CONF_ERROR;
    }
    conf->output_words.len = 0;
    conf->output_words.data = NULL;

    return conf;
}

static char* ngx_http_hello_world_merge_loc_conf(ngx_conf_t* cf, void* parent, void* child) 
{
    ngx_http_hello_world_loc_conf_t* prev = parent;
    ngx_http_hello_world_loc_conf_t* conf = child;
    ngx_conf_merge_str_value(conf->output_words, prev->output_words, "Nginx");
    return NGX_CONF_OK;
}

static char* ngx_http_hello_world(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);
    	//将ngx_http_core_loc_conf_t结构体的回调函数设为ngx_http_hello_world_handle,
 	//在NGX_HTTP_CONTENT_PHASE阶段,如果请求的主机名,URI与配置项所在的配置块相匹配时,就调用该回调方法
    	clcf->handler = ngx_http_hello_world_handler;	
    	ngx_conf_set_str_slot(cf, cmd, conf);
    	return NGX_CONF_OK;
}

第四步: 添加模块并编译

在编写好我们自己的模块后,需要将其添加到Nginx中并进行编译,具体方法为进入Nginx源代码根目录,执行命令:

./configure --add-module=./ngx_http_hello_world_module
sudo make
sudo make install
这三个命令分别完成模块的添加,代码编译,模块安装等功能,添加模块时,在--add-module=后面指定模块的文件路径。

第五步:重启Nginx

接下来,启动Nginx就可以通过浏览器测试Hello World模块的效果。
nginx默认安装路径为/usr/local/nginx,进入该目录下的sbin文件中,该文件包含了nginx的可执行文件.启动nginx服务。
sudo /usr/local/nginx/sbin/nginx 
若在nginx已经启动的情况下添加的模块,那么可以使用下面的命令来平滑重启Nginx,使用新的配置。
sudo /usr/local/nginx/sbin/nginx -s reload
第六步:测试
经过上面的一系列操作,基本上就大功告成了,使用浏览器访问http://localhost/hello_world,就可以页面上显示:

Hello Worldtesting!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值