麻雀虽小,五脏俱全,小小的Hello World盛行于程序世界,就在于其代码虽短,但要真正运行起来,需要我们略通基本语法,稍懂编译运行环境,知晓操作过程,最后,还有一颗持之以恒,不怕折腾的心。前一阵子跑通了Nginx的Hello World程序,今天重温了一遍就顺便写篇博客,记录下来,好记性不如烂笔头,方便以后查阅。

首先在着手操作之前,需要安装好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文件,编写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 ./nginx 

若在nginx已经启动的情况下添加的模块,那么可以使用下面的命令来平滑重启Nginx,使用新的配置。

sudo ./nginx -s reload

经过上面的一系列操作,基本上就大功告成了,使用浏览器访问http://localhost/hello_world,就可以看到下面的效果。 

Nginx的第一个模块-HelloWorld的更多相关文章

  1. AndroidStudio第一个项目HelloWorld

    实验内容 在Android Studio中创建项目 创建并启动Android模拟器 项目的编译和运行 实验要求 在安装好的AndroidStudio上建立第一个工程 创建并启动Android模拟器 编 ...

  2. Nginx一致性哈希模块的Lua实现

    Nginx一致性哈希模块的Lua重新实现 技术背景: 最近在工作中使用了nginx+redis 的架构,redis在后台做分布式存储,每个redis都存放不同的数据,这些数据都是某门户网站通过Hado ...

  3. nginx图片过滤处理模块http_image_filter_module安装配置笔记

    http_image_filter_module是nginx提供的集成图片处理模块,支持nginx-0.7.54以后的版本,在网站访问量不是很高磁盘有限不想生成多余的图片文件的前提下可,就可以用它实时 ...

  4. Java 9 揭秘(3. 创建你的第一个模块)

    文 by / 林本托 Tips 做一个终身学习的人. 在这个章节中,主要介绍以下内容: 如何编写模块化的Java程序 如何编译模块化程序 如何将模块的项目打包成模块化的JAR文件 如何运行模块化程序 ...

  5. yum安装下的nginx,如何添加模块,和添加第三方模块

    需求:生产有个接口是通过socket通信.nginx1.9开始支持tcp层的转发,通过stream实现的,而socket也是基于tcp通信. 实现方法:Centos7.2下yum直接安装的nginx, ...

  6. Nginx加载ngx_pagespeed模块,加快网站打开的速度

    [页面加速]配置Nginx加载ngx_pagespeed模块,加快网站打开的速度   ngx_pagespeed 是一个 Nginx 的扩展模块,可以加速你的网站,减少页面加载时间,它会自动将一些提升 ...

  7. 编译nginx平滑添加stream模块

    1.操作背景 操作系统版本:CentOS Linux release (Core) nginx版本:1.13.4 nginx从1.9.0版本开始,新增了ngx_stream_core_module模块 ...

  8. 给已安装的NGINX添加新的模块

    给已安装的NGINX添加新的模块 2018-11-16 14:02:45   Visit  0 使用 nginx -V 查看当前nginx的信息,包括版本号和configure编译配置信息 版本号 : ...

  9. nginx负载均衡fair模块安装和配置

    nginx-upstream-fair-master fair模块源码 官方github下载地址:https://github.com/gnosek/nginx-upstream-fair说明:如果从 ...

随机推荐

  1. http 中定义的八种请求的介绍

    在http1.1协议中,共定义了8种可以向服务器发起的请求(这些请求也叫做方法或动作),本文对这八种请求做出简要的介绍: 1.PUT:put的本义是推送 这个请求的含义就是推送某个资源到服务器,相当于 ...

  2. 如何查看oracle数据库的所有的关键字

    管理员账户登录后,执行以下命令:  select * from v$reserved_words 附上参考: NOMONITORINGRECORDS_PER_BLOCKCASCADEDYNAMIC_S ...

  3. WdatePicker 没有权限 不能执行已释放 Script 的代码

    提示 拒绝访问 或 没有权限 或 ' Window.document 或 '$dp' 为空或不是对象 $dp.dd is undefined 之类的错误 SCRIPT70: 没有权限 WdatePic ...

  4. 006_Salesforce Sharing 使用说明

    Salesforce Sharing 使用说明 背景说明:Salesforce共享实施记录和其它数据时,需要员工之间共享或多个用户在一个组织间的共享.然而,共享这些数据是有风险的,尤其是当它涉及到敏感 ...

  5. iOS开发零碎知识点

    记录一些常用和不常用的iOS知识点,防止遗忘丢失.(来源为收集自己项目中用到的或者整理看到博客中的知识点),如有错误,欢迎大家批评指正:如有好的知识点,也欢迎大家联系我,添加上去.谢谢! 一.调用代码 ...

  6. MVC Razor模板引擎 @RenderBody、@RenderPage、@RenderSection及Html.RenderPartial、Html.RenderAction

    一.Views文件夹 -> Shared文件夹下的 _Layout.cshtml 母版页 @RenderBody 当创建基于_Layout.cshtml布局页面的视图时,视图的内容会和布局页面合 ...

  7. vs2010取消显示所有文件 崩溃

    这属于vs2010的bug,以下是微软官方对些问题的回应: i am still investigating the problem, but looks like the issue is caus ...

  8. [转]hibernateTools工具安装及使用总结(eclipse 3.6)

    转载地址:http://blueblood79.iteye.com/blog/773177 最近项目采用flex+spring+hibernate的框架开发,之前虽说有多年的Java开发经验了,但是一 ...

  9. WebForm Repeater Response以及 地址栏

    Repeater重复器: Repeater中有五个模板,这里需要注意的是4个 <HeaderTemplate> - 开头,只执行一次的内容 <ItemTemplate> - 需 ...

  10. Python安装时报缺少DLL的解决办法

    准备开始学习Python,但是刚准备环境搭建时就遇到了下面的错误: 仔细的看了看,说是缺少DLL. 对于这个问题的解决办法: 方法一: 1. 在安装包上点右键以管理员身份运行2. C:\Users\用 ...