Hello World HTTP模

1.构造config

ngx_addon_name=ngx_http_mytest_module

HTTP_MODULES="$HTTP_MODULESngx_http_mytest_module"

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

2.在ngx_http_mytest_module.c中定义mytest模块

#include<ngx_config.h>

#include<ngx_core.h>

#include<ngx_http.h>

static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t* r);

static char* ngx_http_mytest(ngx_conf_t* cf, ngx_command_t* cmd, void* conf);

 //用于定义模块的配置文件參数

static ngx_command_t ngx_http_mytest_commonds[] = {

         { 

                   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

};

//指向ngx_http_module接口(HTTP框架要求)
static ngx_http_module ngx_http_mytest_module_ctx = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }; //定义mytest模块
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 }; <span style="font-family:Calibri;">//当在某个配置块中出现mytest配置项时。nginx会调用ngx_http_mytest方法
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; }
</span>
//在这里处理用户请求,并发送响应
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!"); r->headers_out.status= NGX_HTTP_OK; 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 = 1; ngx_chain_t out; out.buf = b; out.next = NULL; return ngx_http_output_filter(r, &out); }

3.在ngx.conf中http里面默认的server中增加例如以下配置

location /test{

mytest;

}

4.编译安装nginx

./configure --add-module=/home/chen123/nginx/exp2 黄色区域为config和ngx_http_mytest_module.c的安装文件夹)

make

sudo make install

5.启动nginx

sudo /usr/local/nginx/sbin/nginx

6.显示结果例如以下:

版权声明:本文博主原创文章,博客,未经同意不得转载。

Nginx得知——Hello World模的更多相关文章

  1. Nginx得知——流程模型(worker流程)

    流程模型 worker流程 master进程模型核心函数ngx_master_process_cycle()中调用了创建子进程函数ngx_start_worker_processes(),该函数源代码 ...

  2. Apache与Nginx的优缺点比较

    1.nginx相对于apache的优点: 轻量级,同样起web 服务,比apache 占用更少的内存及资源 抗并发,nginx 处理请求是异步非阻塞的,而apache 则是阻塞型的,在高并发下ngin ...

  3. web服务器之nginx与apache

    最近准备架设php的web服务器,以下内容可供参考. 1.nginx相对于apache的优点: 轻量级,同样起web 服务,比apache占用更少的内存及资源 抗并发,nginx 处理请求是异步非阻塞 ...

  4. nginx&apache比较

    1.nginx相对于apache的优点: 轻量级,同样起web 服务,比apache占用更少的内存及资源 抗并发,nginx 处理请求是异步非阻塞的,而apache 则是阻塞型的,在高并发下nginx ...

  5. 转:Apache与Nginx的优缺点比较

    Apache与Nginx的优缺点比较 http://weilei0528.blog.163.com/blog/static/206807046201321810834431/ 1.nginx相对于ap ...

  6. nginx和apache的优缺点比较

    简单的说apache httpd和nginx都是web服务器,但两者适应的场景不同,也就是两者专注于解决不同的问题.apache httpd:稳定.对动态请求处理强,但同时高并发时性能较弱,耗费资源多 ...

  7. Apache与Nginx优缺点比较

    本文来源:收集.整理自互联网 1.nginx相对于apache的优点:  轻量级,同样起web 服务,比apache 占用更少的内存及资源  抗并发,nginx 处理请求是异步非阻塞的,而apache ...

  8. nginx+tomcat负载均衡策略

    測试环境均为本地,測试软件为: nginx-1.6.0,apache-tomcat-7.0.42-1.apache-tomcat-7.0.42-2.apache-tomcat-7.0.42-3 利用n ...

  9. Apache和Nginx的对比

    Apache与Nginx的优缺点比较 1.nginx相对于apache的优点: 轻量级,同样起web 服务,比apache 占用更少的内存及资源 抗并发,nginx 处理请求是异步非阻塞的,而apac ...

随机推荐

  1. 使用 JQueryMobile 点击超链接提示“error loading page” 错误

    使用jquery mobile创建dialog时出现加载错误,“Error Loading Page”. 原因是:jquery mobile页面默认采用ajax方式进行交互,而ajax方式下是不支持f ...

  2. 【C语言疯狂讲义】(八)C语言一维数组

    1.数组的基本概念: 同样类型    若干个     有序 由若干个同样类型的数据组成的有序的集合 有序:存储地址连续 下标连续 数组名:用来存放数组首地址的变量 数组元素:构成数组的每个数据 数组的 ...

  3. sql server2012附加的数据库问题

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjM2NzUxMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  4. 关于windows系统影子账户的问题

    在这之前,需要大家了解几个问题,一个是SID,一个是账号的F值. Windows账户的SID 在Windows系统中,系统会为每个用户账户建立一个唯一的安全标识符(Security Identifie ...

  5. aMule代码分析(2)——CFileDataIO类和CFile类

    aMule中的类很多,Maixee今天选择了两个比较基础的类,均是跟文件操作有关的,分别是CFileDataIO类和CFile类.其中,前者是基类,后者由它派生而出的.具体的继承关系,可以参考这张图: ...

  6. TypeError: Cannot read property &#39;style&#39; of null 错误解决

    错误信息例如以下: JSP代码例如以下: <c:if test ="${not empty excelErrors}"> <div id="excelE ...

  7. OpenLayers学习笔记4——使用jQuery UI实现測量对话框

    OpenLayers学习最好的方式就是跟着其自带的演示样例进行学习,另外对web前端的开发设计要了解,慢慢积累,这样在一般的小项目中应该是足够用了. 本篇參照量測demo实现对话框形式的量測,抛砖引玉 ...

  8. poj1849(求树的直径)

    题目链接:http://poj.org/problem?id=1849 题意:有一颗n个结点的带权的无向树, 在s结点放两个机器人, 这两个机器人会把树的每条边都走一遍, 但是最后机器人不要求回到出发 ...

  9. vb.net版机房收费——助你学会七层架构(一)

    我自己写机房的时候,看非常多高人的博客,各种的借鉴,当初务必的纠结,如今整个机房敲完了,写这篇博客给大家一个总体上的.简单理解的七层,期望大家看完这篇文章之后,不会这个纠结了. 首先大家得看了我的上一 ...

  10. java枚举的使用

    定义枚举 public enum YesOrNo { YES("是") , NO("否") ; private String text ; private in ...