Nginx得知——Hello World模
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模的更多相关文章
- Nginx得知——流程模型(worker流程)
流程模型 worker流程 master进程模型核心函数ngx_master_process_cycle()中调用了创建子进程函数ngx_start_worker_processes(),该函数源代码 ...
- Apache与Nginx的优缺点比较
1.nginx相对于apache的优点: 轻量级,同样起web 服务,比apache 占用更少的内存及资源 抗并发,nginx 处理请求是异步非阻塞的,而apache 则是阻塞型的,在高并发下ngin ...
- web服务器之nginx与apache
最近准备架设php的web服务器,以下内容可供参考. 1.nginx相对于apache的优点: 轻量级,同样起web 服务,比apache占用更少的内存及资源 抗并发,nginx 处理请求是异步非阻塞 ...
- nginx&apache比较
1.nginx相对于apache的优点: 轻量级,同样起web 服务,比apache占用更少的内存及资源 抗并发,nginx 处理请求是异步非阻塞的,而apache 则是阻塞型的,在高并发下nginx ...
- 转:Apache与Nginx的优缺点比较
Apache与Nginx的优缺点比较 http://weilei0528.blog.163.com/blog/static/206807046201321810834431/ 1.nginx相对于ap ...
- nginx和apache的优缺点比较
简单的说apache httpd和nginx都是web服务器,但两者适应的场景不同,也就是两者专注于解决不同的问题.apache httpd:稳定.对动态请求处理强,但同时高并发时性能较弱,耗费资源多 ...
- Apache与Nginx优缺点比较
本文来源:收集.整理自互联网 1.nginx相对于apache的优点: 轻量级,同样起web 服务,比apache 占用更少的内存及资源 抗并发,nginx 处理请求是异步非阻塞的,而apache ...
- 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 ...
- Apache和Nginx的对比
Apache与Nginx的优缺点比较 1.nginx相对于apache的优点: 轻量级,同样起web 服务,比apache 占用更少的内存及资源 抗并发,nginx 处理请求是异步非阻塞的,而apac ...
随机推荐
- c++windows内核编程笔记day12 硬盘逻辑分区管理、文件管理、内存管理
windows系统磁盘文件存储: 分区格式:NTFS / FAT32 GetSystemDirectory();//获取系统路径 GetWindowsDirectory();//获取windows路径 ...
- Struts工作机制图+OGNL+EL+值栈(Map,对象栈)
struts 值栈 通过get set方法 方便的获取,设置属性值 比如从jsp页面传来的參数...从Action设置jsp所要回显的内容 注意EL表达式,struts2对request进 ...
- [Cocos2d-x]布局与定位
游戏中,精灵的位置由Position与AnchorPoint同时决定. Scene 锚点 (0,0) 不启用锚点 CCNode锚点 (0,0) 不启用锚点 CCLayer锚点 (0,0) 不启用锚点 ...
- 开源 java CMS - FreeCMS2.3 留言管理
原文地址:http://javaz.cn/site/javaz/site_study/info/2015/22027.html 项目地址:http://www.freeteam.cn/ 留言管理 管理 ...
- MVC应用程序与单选列表
原文:MVC应用程序与单选列表 前几天,Insus.NET有在MVC应用程序中,练习了<MVC应用程序,动态创建单选列表(RadioButtonList)>http://www.cnblo ...
- hdu1251(Trie树)
传送门:统计难题 分析:Trie树入门题,随便写写练下手感,统计每个节点被多少单词经过就可以了. #include <iostream> #include <cstdio> # ...
- LeetCode——Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- 秒杀多线程第二篇 多线程第一次亲热接触 CreateThread与_beginthreadex本质差别
本文将带领你与多线程作第一次亲热接触,并深入分析CreateThread与_beginthreadex的本质差别,相信阅读本文后你能轻松的使用多线程并能流畅准确的回答CreateThread与_beg ...
- PHP实现插入排序算法
插入排序(Insertion Sort),是一种较稳定.简单直观的排序算法.插入排序的工作原理,是通过构建有序序列,对于未排序的数据,在有序序列中从后向前扫描,找到合适的位置并将其插入.插入排序,在最 ...
- Oracle JDBC版本区别(转)
oracle\product\11.2.0\dbhome_1\jdbc\lib ojdbc5.jar ojdbc5dms.jar ojdbc5dms_g.jar ojdbc5_g.jar ojdbc6 ...