向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. Django启动报错笔记

    NO.1: You have 15 unapplied migration(s). Your project may not work properly until you apply the mig ...

  2. python bmp转jpg 且灰度图转彩色

    今天在简书,上看到了一个 bmp转jpg的代码,便记录一下. # coding:utf-8 import os from PIL import Image # bmp 转换为jpg,灰度图转RGB d ...

  3. EOS源码

    [EOS源码] 1.在 libraries/chain/include/eosio/chain/ 目录下. permission_level 定义如下:   account_name.permissi ...

  4. LVS(一):基本概念和三种模式

    网站架构中,负载均衡技术是实现网站架构伸缩性的主要手段之一.所谓"伸缩性",是指可以不断向集群中添加新的服务器来提升性能.缓解不断增加的并发用户访问压力. 负载均衡有好几种方式:h ...

  5. 【相关网站 - 02】- Java 好文博客

    一.源码分析博客 还有这种操作?浅析为什么要看源码 你觉得什么才是 Java 的基础知识? 1. JDK 2. Mybatis 3. Spring 4. Sring Boot 5. Spring Cl ...

  6. vue-实现全选单选

    在获取列表页面数据时,通过forEach遍历存储数据的对象,给对象中添加一个selected变量,值为布尔值. 点击全选时,通过遍历将对象中selected的布尔值改变 点击单选时,被点中的通过筛选加 ...

  7. jqgrid点击搜索无法重置参数问题

    var searchClick=false;//判断是否是第一次点击搜索 //当搜索按钮被单击时触发 function searchData(){ //创建jqGrid组件 console.log(' ...

  8. ABAP的匹配

    ABAP的匹配 通配符 字符串操作中的通配符 *:多位字符的通配符 +:一位字符的通配符 #:字符操作中的转义符 REPORT ztest_placeholder. DATA:l_name(8) TY ...

  9. STL基础复习

    stl容器:vector,deque,list,map/multimap,set 特殊容器:stack,queue,priority_queue 通用操作 size()  返回当前容器元素数量 emp ...

  10. 服务监控-zabbix监控指标

    1.cpu unitzation 监控cpu的整体状态. 使用Zabbix查看CPU利用率,会有下面几个值: CPU idle time:空闲的cpu时间比[简称id] CPU user time:用 ...