Nginx的第一个模块-HelloWorld
麻雀虽小,五脏俱全,小小的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的更多相关文章
- AndroidStudio第一个项目HelloWorld
实验内容 在Android Studio中创建项目 创建并启动Android模拟器 项目的编译和运行 实验要求 在安装好的AndroidStudio上建立第一个工程 创建并启动Android模拟器 编 ...
- Nginx一致性哈希模块的Lua实现
Nginx一致性哈希模块的Lua重新实现 技术背景: 最近在工作中使用了nginx+redis 的架构,redis在后台做分布式存储,每个redis都存放不同的数据,这些数据都是某门户网站通过Hado ...
- nginx图片过滤处理模块http_image_filter_module安装配置笔记
http_image_filter_module是nginx提供的集成图片处理模块,支持nginx-0.7.54以后的版本,在网站访问量不是很高磁盘有限不想生成多余的图片文件的前提下可,就可以用它实时 ...
- Java 9 揭秘(3. 创建你的第一个模块)
文 by / 林本托 Tips 做一个终身学习的人. 在这个章节中,主要介绍以下内容: 如何编写模块化的Java程序 如何编译模块化程序 如何将模块的项目打包成模块化的JAR文件 如何运行模块化程序 ...
- yum安装下的nginx,如何添加模块,和添加第三方模块
需求:生产有个接口是通过socket通信.nginx1.9开始支持tcp层的转发,通过stream实现的,而socket也是基于tcp通信. 实现方法:Centos7.2下yum直接安装的nginx, ...
- Nginx加载ngx_pagespeed模块,加快网站打开的速度
[页面加速]配置Nginx加载ngx_pagespeed模块,加快网站打开的速度 ngx_pagespeed 是一个 Nginx 的扩展模块,可以加速你的网站,减少页面加载时间,它会自动将一些提升 ...
- 编译nginx平滑添加stream模块
1.操作背景 操作系统版本:CentOS Linux release (Core) nginx版本:1.13.4 nginx从1.9.0版本开始,新增了ngx_stream_core_module模块 ...
- 给已安装的NGINX添加新的模块
给已安装的NGINX添加新的模块 2018-11-16 14:02:45 Visit 0 使用 nginx -V 查看当前nginx的信息,包括版本号和configure编译配置信息 版本号 : ...
- nginx负载均衡fair模块安装和配置
nginx-upstream-fair-master fair模块源码 官方github下载地址:https://github.com/gnosek/nginx-upstream-fair说明:如果从 ...
随机推荐
- paper 114:Mahalanobis Distance(马氏距离)
(from:http://en.wikipedia.org/wiki/Mahalanobis_distance) Mahalanobis distance In statistics, Mahalan ...
- qq
引用:http://blog.sina.com.cn/s/blog_9e2e84050101blqz.html 腾讯QQ使用何种开发平台? 腾讯QQ的开发分客户端软 ...
- 雾里看花般的迷茫--货运APP
移动互联网正在改变我们的生活方式,各种手机APP充斥着人们的生活,物流行业也不例外.货运APP的出现,对于物流行业是一个提升的机会,也是迈向标准化和专业化的一个有效途径. 经过三十多年的发展,我国物流 ...
- html与Android——webView
1 <html> 2 <head> 3 <title>myHtml.html</title> 4 5 <meta http-equiv=" ...
- Spring学习笔记之三----基于Annotation的Spring IOC配置
使用Annotation 来创建Bean有两种方式 在配置类中创建bean(配置类是指标注为@Configuration的类),在配置类中每一个创建bean的方法都应该标注为@Bean,可以在@Bea ...
- Jersey 2 + Maven + Tomcat + IntelliJ IDEA 搭建RESTful服务
本文参考以下内容: [1] Starting out with Jersey & Apache Tomcat using IntelliJ [2] [Jersey]IntelliJ IDEA ...
- 主机和虚拟机不能ping通问题
VMware的三种网络模式附上: 在vmw中,默认有3个虚拟交换机 分别是VMnet0(桥接).VMnet1(主机网络)以及VMnet8(NAT) 还可以根据需要添加VMnet2~VMnet7和VMn ...
- Python企业级开发之一:基础
Python企业级开发相关内容.这里涉及到Python开发过程中的问题以及解决办法.还提供新的开发思路. 脚本开发的一些共同的问题.如:1.对OO的支持不完善,2.问题定位方式给出的信息过于晦涩,3. ...
- Foundation框架-NSString和NSMutableString
可变与不可变的字符串 --1-- Foundation框架介绍 1.1 框架介绍 --2-- NSString 2.1 NSString介绍及使用 2.2 NSString创建方式 2.3 从文件中 ...
- JS调用JCEF方法
坐下写这篇文章的时候,内心还是有一点点小激动的,折腾了一个多星期,踩了一个又一个的坑,终于找到一条可以走通的路,内心的喜悦相信经历过的人都会明白~~~~~今儿个老百姓啊,真呀个真高兴啊,哈哈,好了,废 ...