向nginx中添加第一个最简单的hello world模块

一、编写ngx_http_mytest_module模块

1. ngx_http_mytest_module.c

 #include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h> //mytest module define static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t* r)
{
//不是GET或者HEAD方法,返回405
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!");
//设置http状态码
r->headers_out.status = NGX_HTTP_OK;
//设置响应Content-Type
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;
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 = ; //设置响应内容到chain中
ngx_chain_t out;
out.buf = b;
out.next = NULL; return ngx_http_output_filter(r, &out);
} //set handler
static char* ngx_http_mytest(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_mytest_handler;
return NGX_CONF_OK;
} //conf structure
static ngx_command_t ngx_http_mytest_commands[] =
{
{
ngx_string("mytest"),
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF |
NGX_HTTP_LMT_CONF | NGX_CONF_NOARGS,
ngx_http_mytest,
NGX_HTTP_LOC_CONF_OFFSET,
,
NULL
},
ngx_null_command
}; //module ctx
static ngx_http_module_t ngx_http_mytest_module_ctx = { NULL, NULL, NULL, NULL, NULL, NULL,NULL, NULL}; //mytest module
ngx_module_t ngx_http_mytest_module = {
NGX_MODULE_V1,
&ngx_http_mytest_module_ctx,
ngx_http_mytest_commands,
NGX_HTTP_MODULE,
NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NGX_MODULE_V1_PADDING
};

ngx_http_mytest_module.c

2.config

 ngx_addon_name=ngx_http_mytest_module
HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mytest_module.c"

config

二、安装nginx

 ./configure --prefix=/usr/local/nginx  --add-module=/usr/local/src/ngx-mytest-module/

三、修改配置文件

 location /test {
mytest;
}

四、启动nginx,并通过浏览器访问

五、查看日志文件

倒数第二行对/test的响应。

六、对过程一些问题的总结

1. 执行configure后,objs文件夹下ngx_modules.c文件中只有3部分:

  1)extern模块

  

  2)ngx_modules数组

  

  

  3)ngx_module_names数组

  

2. nginx自定义模块需要添加的头文件

  

3. 如果主机不能访问虚拟机web服务器,可能是需要关闭防火墙

 iptables -F
iptables -P INPUT ACCEPT

nginx,hello World!的更多相关文章

  1. 看完这篇还不了解 Nginx,那我就哭了!

    作者:蔷薇Nina www.cnblogs.com/wcwnina/p/8728391.html 想必大家一定听说过 Nginx,若没听说过它,那么一定听过它的"同行"Apache ...

  2. 就是要让你搞懂Nginx,这篇就够了!

    开源Linux 长按二维码加关注~ 作者:渐暖° 出处:blog.csdn.net/yujing1314/article/details/107000737 来源:公众号51CTO技术栈 Nginx ...

  3. 【转】linux 编译安装nginx,配置自启动脚本

    linux 编译安装nginx,配置自启动脚本 本文章来给各位同学介绍一篇关于linux 编译安装nginx,配置自启动脚本教程,希望有需要了解的朋友可一起来学习学习哦. 在公司的suse服务器装ng ...

  4. Startssl 现在就启用 HTTPS,免费的!

    为什么要使用HTTPS 主要是为了安全,虽然没有100%的安全,但是我们可以尽量提高安全级别,目前大型网站都已经使用HTTPS了 注册StartSSL 注册页面  选择国家 和 输入 邮箱 他们会通过 ...

  5. 话说Centos下nginx,php,mysql以及phpmyadmin的配置

    大话centos下部署phalcon框架 Centos还是ubuntu? 当我沿用这个标题的时候,心里在想"我能说我之前用的windows吗?",windows下xampp,wam ...

  6. CentOS源码安装搭建LNMP全过程(包括nginx,mysql,php,svn)【转】

    转自:http://blog.csdn.net/yanzi1225627/article/details/49123659 服务器环境为:CentOS6.5 64位 目标:搭建LNMP(Linux + ...

  7. nginx,wsgi,flask之间的关系

    之前看写flask 应用的一些疑问,百度上的答案解释的不错,这里记着以后可以看看Web 服务器层对于传统的客户端 - 服务器架构,客户端向服务器发送请求,服务器接收请求,处理请求,最后给客户端返回请求 ...

  8. PHP学习笔记--1,不总结,不掌握,不明白!

    不总结,不掌握,不明白! 前言: 学php一开始就是语法,变量,数组,函数,OOP(面向对象[封装,继承,多态,抽象]) 这些都是最基础的东西,但你还要懂一些在实际开发中要用的东西,比如基本的HTML ...

  9. 阿里云上,Ubuntu下配置Nginx,在tomcat中加了https协议就不可以了

    问题 阿里云上,Ubuntu服务器,本来部署的是tomcat,并且使用了https 协议.后来为了静态资源分离集成了 nginx,nginx代理跳转到 tomcat.刚开始直接访问http 网址发现, ...

随机推荐

  1. 微信小程序问题---数据传输长度为 1275870 已经超过最大长度 1048576

    开发微信小程序时,遇到数据传输长度为 1095538 已经超过最大长度 1048576的问题. 这是setData时操作数据过大导致,一般出现在请求返回数据过大,我们又将这个数据一次性用setData ...

  2. hbuilder 打包 vueAPP

    1:设置状态栏颜色 在manifest.json 找到 plus 下添加 "statusbar": { "immersed": true/*沉浸式状态栏*/ 设 ...

  3. 3D Math Keynote 2

    [3D Math Keynote 2] 1.方向(diretion),指的是前方朝向.方位(orientation),指的是head.pitch.roll. 2.欧拉角的缺点: 1)给定方位的表达式不 ...

  4. C#中生成GUID的四种格式

    var uuid = Guid.NewGuid().ToString(); // 9af7f46a-ea52-4aa3-b8c3-9fd484c2af12 var uuidN = Guid.NewGu ...

  5. windows搭建zabbix agent

    1.下载和解压zabbix agent 地址: http://www.zabbix.com/downloads/2.4.4/zabbix_agents_2.4.4.win.zip解压:conf目录存放 ...

  6. ExpandableListView解析JSON数据

    效果图:                                       说明:刚开始使用这个控件我花费了3天的时间,但是一直都没有达到预期的效果,要么就是直接全部不显示,要么就是数据累加 ...

  7. FPGA驱动步进电机

    步进电机 步进电机是将电脉冲信号转变为角位移或线位移的开环控制电机,是现代数字程序控制系统中的主要执行元件,应用极为广泛.在非超载的情况下,电机的转速.停止的位置只取决于脉冲信号的频率和脉冲数,而不受 ...

  8. jquery-confirm使用方法

    简要教程 jquery-confirm是一款功能强大的jQuery对话框和确认框插件.它提供多种内置的主题效果,可以实现ajax远程加载内容,提供动画效果和丰富的配置参数等.它的特点还有: 可以使用键 ...

  9. python完整课程

    python完整课程 python课程概述 python课程大纲 链接:http://www.cnblogs.com/lx63blog/articles/9054294.html 课程结构: 1.安装 ...

  10. 解决在vscode中eslint在vue后缀文件中保存时无法自动格式化的问题

    在setting.json中加入如下内容 { "eslint.autoFixOnSave": true, "eslint.validate": [ " ...