nginx hello模块代码
// ngx_http_mytest_module.c
#include "ngx_core.h"
#include "ngx_string.h"
#include "ngx_buf.h"
#include "ngx_conf_file.h"
#include "ngx_http.h" // 必须在ngx_http_request.h之前包含该文件
#include "ngx_http_request.h"
#include "ngx_http_config.h"
#include "ngx_http_core_module.h" // 返回值如NGX_HTTP_OK(200)、NGX_HTTP_NOT_FOUND(404)、NGX_HTTP_INTERNAL_SERVER_ERROR(500)、NGX_OK(0)、NGX_ERROR(-1)等
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r)
{
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!\n");
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = response.len;
r->headers_out.content_type = type; /* 发送HTTP响应时需要执行发送HTTP头部(和响应行)和发送HTTP包体两步操作,此为第一步
该函数会首先调用所有的HTTP过滤模块共同处理r->headers_out中定义的HTTP响应头部,全部处理完毕后才会序列化为TCP字节流发送到客户端 */
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 = 1; ngx_chain_t out;
out.buf = b;
out.next = NULL; return ngx_http_output_filter(r, &out); // 发送HTTP响应包体
} 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; // HTTP框架接收完HTTP请求的头部后,将调用该函数 return NGX_CONF_OK;
} 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, 0, NULL},
ngx_null_command
}; static ngx_http_module_t ngx_http_mytest_module_ctx = {
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
}; 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
};
nginx hello模块代码的更多相关文章
- nginx事件模块分析(一)
nginx ngx_events_module模块分析 ngx_events_module模块是核心模块之一,它是其它所有事件模块的代理模块.nginx在启动时只与events模块打交道,而由even ...
- nginx自定义模块编写-实时统计模块--转载
原文:http://www.vimer.cn/2012/05/nginx%E8%87%AA%E5%AE%9A%E4%B9%89%E6%A8%A1%E5%9D%97%E7%BC%96%E5%86%99- ...
- nginx -- handler模块(100%)
handler模块简介 相信大家在看了前一章的模块概述以后,都对nginx的模块有了一个基本的认识.基本上作为第三方开发者最可能开发的就是三种类型的模块,即handler,filter和load-ba ...
- Nginx Http模块开发
关于Nginx Http模块开发的文章非常少,只有Emiler的那篇关于Http模块的文章,但是那篇文章里面,并没有说到事件型的模块如何进行开发.而且文章里面提到的内容实在是让人有点意犹未尽.因此,对 ...
- nginx自定义模块编写-根据post参数路由到不同服务器
nginx可以轻松实现根据不同的url 或者 get参数来转发到不同的服务器,然而当我们需要根据http包体来进行请求路由时,nginx默认的配置规则就捉襟见肘了,但是没关系,nginx提供了强大的自 ...
- Nginx学习笔记六Nginx的模块开发
1.Nginx配置文件主要组成:main(全局配置)这部分的指令将影响其他所有部分.server(虚拟主机配置)这部分指令主要用于指定虚拟主机域名,IP和端口.upstream(主要为反向代理,负载均 ...
- Nginx限速模块初探
Nginx限速模块分为哪几种?按请求速率限速的burst和nodelay参数是什么意思?漏桶算法和令牌桶算法究竟有什么不同?本文将带你一探究竟.我们会通过一些简单的示例展示Nginx限速模块是如何工作 ...
- [转帖]Nginx rewrite模块深入浅出详解
Nginx rewrite模块深入浅出详解 https://www.cnblogs.com/beyang/p/7832460.html rewrite模块(ngx_http_rewrite_modul ...
- Nginx详解七:Nginx基础篇之Nginx官方模块
Nginx官方模块 --with-http_stub_status_module:Nginx的客户端状态,用于监控连接的信息,配置语法如下:配置语法:stub_status;默认状态:-配置方法:se ...
随机推荐
- Dull Chocolates Gym - 101991D 离散化 前缀和
题目链接:https://vjudge.net/problem/Gym-101991D 具体思路:首先看数据范围,暴力肯定不可以,可以下离散化,然后先求出离散化后每一个点到(1,1)的符合题目的要求的 ...
- 使用ctypes在Python中调用C++动态库
使用ctypes在Python中调用C++动态库 入门操作 使用ctypes库可以直接调用C语言编写的动态库,而如果是调用C++编写的动态库,需要使用extern关键字对动态库的函数进行声明: #in ...
- inet_addr_onlink
/* 根据指定设备的ip配置块,判断地址a,b是否在同一子网 */ /* --邻居项要求,在同一子网中的两个设备, 至少有一个接口有相同的子网配置, --也就是说对端的in_dev->ifa_l ...
- javaScript-继承2种方式
1.组合继承 组合继承带来的问题很明细就是父类的构造函数会调用两次,如: function Person(name, age, sex) { this.name = name; this.age = ...
- Oracle中的dual
简介,摘自百度百科: Oracle提供的最小的表,不论进行何种操作(不要删除记录),它都只有一条记录——'X'. 例如:执行select * from dual,里面只有一条记录:执行insert i ...
- LAMP结合discuz论坛的配置
一.安装discuz ---->//download discuz; [root@localhost ~]# mkdir /data/www [root@localhost ~]# cd /da ...
- hdu 5692(dfs序+线段树,好题)
Snacks Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- 获取ios设备系统信息的方法 之 [UIDevice currentDevice]
获取iphone的系统信息使用[UIDevice currentDevice],信息如下: [[UIDevice currentDevice] systemName]:系统名称,如iPhone OS ...
- 在JAVA中生成RSA秘钥对实现SSH互信
https://blog.csdn.net/u014196729/article/details/51496262 https://blog.csdn.net/u013066244/article/d ...
- centos7 mongodb3.4 安装
上传tgz 安装包 [root@localhost install_pack]# ll total 274840 -rw-r--r--. 1 root root 9393241 Jun 2 14:36 ...