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 ...
随机推荐
- OpenRisc-32-ORPSoC烧写外部spi flash
引言 经过前面的分析和介绍,我们对ORPSoC的启动过程(http://blog.csdn.net/rill_zhen/article/details/8855743)和 ORpSoC的debug子系 ...
- Linux ssh密钥自动登录(转)
在开发中,经常需要从一台主机ssh登陆到另一台主机去,每次都需要输一次login/Password,很繁琐.使用密钥登陆就可以不用输入用户名和密码了 实现从主机A免密码登陆到主机B,需要以下几个步骤: ...
- 下载jdk文件后缀是.gz而不是.tar.gz怎么办
用chrom浏览器下载了linux版的jdk,发现文件后缀是.gz,没看过这玩意,一打开,还是一个.gz文件,原本以为是新文件后缀呢.那个百度google啊. . ..最后都没发现有这方面的资料啊.. ...
- 鸟哥Linux私房菜知识点总结6到7章
近期翻看了一本<鸟哥的Linux私房菜>.这是一本基础的书,万丈高楼平地起.会的不多但能够学.这是我整理的一些知识点.尽管非常基础.希望和大家共同交流. 第6章主机规划与磁盘分区 1.在进 ...
- 文件操作ofstream,open,close,ifstream,fin,依照行来读取数据, fstream,iosin iosout,fio.seekg(),文件写入和文件读写,文件拷贝和文件
1.ofstream,open,close 写入文件 #include<iostream> #include<fstream> using namespace std; ...
- Python使用时间戳
1.将字符串的时间转换为时间戳 方法: a = "2013-10-10 23:40:00" 将其转换为时间数组 importtime timeArray = time.strpti ...
- 服务器编程入门(3)TCP协议详解
问题聚焦: 本节从如下四个方面讨论TCP协议: TCP头部信息:指定通信的源端端口号.目的端端口号.管理TCP连接,控制两个方向的数据流 TCP状态转移过程:TCP连接的任意一 ...
- 项目中那些事|ListView中嵌套ListView问题
要在一个ListView中放入另一个ListView,也即在一个ListView的每个 item 中放入另外一个ListView.但刚开始的时候,会发现放入的子ListView会显示不完全(我这里只显 ...
- HashMap源码解读(转)
http://www.360doc.com/content/10/1214/22/573136_78188909.shtml 最近朋友推荐的一个很好的工作,又是面了2轮没通过,已经是好几次朋友内推没过 ...
- 采用DWR、maven保存数据到数据库
一.原理: Ajax是时下比较流行的一种web界面设计新思路,其核心思想是从浏览器获取XMLHttp对象与服务器端进行交互. DWR(Direct Web Remoting)就是实现了这种Ajax技术 ...