开发方法參考淘宝的教程

这个模块的功能是向client发送一个文件,类似于网页上的另存为功能

#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h> static ngx_int_t ngx_http_file_init(ngx_conf_t *cf);
void* ngx_http_file_create_loc_conf(ngx_conf_t *cf);
static char* ngx_http_file_name(ngx_conf_t* cf,ngx_command_t* cmd,void* conf); typedef struct
{
ngx_str_t file_name;
}ngx_http_file_loc_conf_t; static ngx_http_module_t ngx_http_file_module_ctx =
{
NULL,
ngx_http_file_init, /* postconfiguration */
NULL,
NULL,
NULL,
NULL,
ngx_http_file_create_loc_conf, /* create location configuration */
NULL
}; static ngx_command_t ngx_http_file_commands[] = {
{
ngx_string("file_name"),
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS|NGX_CONF_TAKE1,/* 接收0个或1个參数 */
ngx_http_file_name,
NGX_HTTP_LOC_CONF_OFFSET, /* 配置项的级别 */
offsetof(ngx_http_file_loc_conf_t, file_name), /*file_name配置项在ngx_http_file_loc_conf_t中的偏移位置*/
NULL,
},
ngx_null_command
}; /* 模块的定义 */
ngx_module_t ngx_http_file_module =
{
NGX_MODULE_V1,
&ngx_http_file_module_ctx, /* 模块上下文,即一些回调函数 */
ngx_http_file_commands, /* 配置项解析 */
NGX_HTTP_MODULE,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NGX_MODULE_V1_PADDING
}; static ngx_int_t ngx_http_file_handler(ngx_http_request_t* r)
{
ngx_http_core_loc_conf_t *clcf = ngx_http_get_module_loc_conf(r,ngx_http_core_module);
ngx_http_file_loc_conf_t* file_conf = ngx_http_get_module_loc_conf(r,ngx_http_file_module);//获得配置项结构体
if(file_conf -> file_name.len == 0)
{
ngx_log_error(NGX_LOG_EMERG, r->connection->log, 0,"file_name is empty");
return NGX_DECLINED;
} ngx_open_file_info_t of;//文件信息结构体
ngx_str_t path = file_conf->file_name;
ngx_memzero(&of, sizeof(ngx_open_file_info_t));
if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)!= NGX_OK)
{
ngx_log_error(NGX_LOG_ERR,r->connection->log,of.err, "%s \"%s\" failed", of.failed, path.data);
} ngx_int_t rc = ngx_http_discard_request_body(r);//丢弃包体
if(rc != NGX_OK)return rc; r -> headers_out.status = NGX_HTTP_OK;
r -> headers_out.content_length_n = of.size;
r -> headers_out.last_modified_time = of.mtime;
if(ngx_http_set_etag(r) != NGX_OK)return NGX_HTTP_INTERNAL_SERVER_ERROR;
if(ngx_http_set_content_type(r) != NGX_OK)return NGX_HTTP_INTERNAL_SERVER_ERROR; r -> allow_ranges = 1;
/* 先申请发送包体的缓冲区空间,申请成功后再发送包头 */
ngx_buf_t* b = ngx_pcalloc(r -> pool,sizeof(ngx_buf_t));
if(b == NULL)return NGX_HTTP_INTERNAL_SERVER_ERROR;
b -> file = ngx_pcalloc(r -> pool,sizeof(ngx_file_t));
if(b->file == NULL)return NGX_HTTP_INTERNAL_SERVER_ERROR; rc = ngx_http_send_header(r); /* 发送头部 */
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) return rc; b -> file_pos = 0;
b -> file_last = of.size;
b -> in_file = b -> file_last ? 1 : 0;
b -> last_buf = (r == r -> main) ? 1 : 0;
b -> last_in_chain = 1;
b -> file -> name = path;
b -> file -> fd = of.fd; ngx_chain_t out;
out.buf = b;
out.next = NULL; return ngx_http_output_filter(r,&out); /*把产生的内容传递给兴许的filter去处理 */ } /* 创建配置项结构体 */
void* ngx_http_file_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_file_loc_conf_t* local_conf = ngx_pcalloc(cf->pool,sizeof(ngx_http_file_loc_conf_t));
if(local_conf == NULL)return NULL;
ngx_str_null(&local_conf->file_name);
return local_conf;
} /* cf: 该參数里面保存从配置文件读取到的原始字符串以及相关的一些信息
* cmd: 这个配置指令相应的ngx_command_t结构,即上面定义的数组中的第一个元素
* conf: 就是定义的存储这个配置值的结构体,即上面的ngx_http_file_loc_conf_t*/
static char* ngx_http_file_name(ngx_conf_t* cf,ngx_command_t* cmd,void* conf)
{
/* cf表示已经解析好的參数信息,将当中的数据取出来赋给自定义的配置项结构体中的相应成员 */
char* rv = ngx_conf_set_str_slot(cf,cmd,conf);
return rv;
} static ngx_int_t ngx_http_file_init(ngx_conf_t *cf)
{
ngx_http_core_main_conf_t* cmcf = ngx_http_conf_get_module_main_conf(cf,ngx_http_core_module);//获得核心配置文件
ngx_http_handler_pt* h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);//将handler增加handlers链表
if(h == NULL)return NGX_ERROR;
*h = ngx_http_file_handler;//对该handler方法进行赋值
return NGX_OK;
}

用法:

编译:还须要在file_module目录中加入config文件

./configure --prefix=/home/fangjian/study/code/nginx-1.4.4/nginx --add-module=/home/fangjian/study/code/nginx-1.4.4/fangjian_module/file_module

make

make install

然后在nginx.conf中加入       location /file {file_name   index.html; }启动nginx,在浏览器中输入127.0.0.1/file

nginx模块开发的更多相关文章

  1. 【转】Nginx模块开发入门

    转自: http://kb.cnblogs.com/page/98352/ 结论:对Nginx模块开发入门做了一个helloworld的示例,简单易懂.也有一定的深度.值得一看. Nginx模块开发入 ...

  2. Nginx模块开发入门

    前言 Nginx是当前最流行的HTTP Server之一,根据W3Techs的统计,目前世界排名(根据Alexa)前100万的网站中,Nginx的占有率为6.8%.与Apache相比,Nginx在高并 ...

  3. [转] Nginx模块开发入门

    前言 Nginx是当前最流行的HTTP Server之一,根据W3Techs的统计,目前世界排名(根据Alexa)前100万的网站中,Nginx的占有率为6.8%.与Apache相比,Nginx在高并 ...

  4. Nginx模块开发入门(转)

    前言 Nginx是当前最流行的HTTP Server之一,根据W3Techs的统计,目前世界排名(根据Alexa)前100万的网站中,Nginx的占有率为6.8%.与Apache相比,Nginx在高并 ...

  5. Nginx模块开发入门(转)

    前言 Nginx是当前最流行的HTTP Server之一,根据W3Techs的统计,目前世界排名(根据Alexa)前100万的网站中,Nginx的占有率为6.8%.与Apache相比,Nginx在高并 ...

  6. 解剖Nginx·模块开发篇(1)跑起你的 Hello World 模块!

    1 学习 Nginx 模块开发需要有哪些准备? 需要的预备知识不多,有如下几点: 有过一些 C 语言的编程经历: 知道 Nginx 是干嘛的,并有过编写或改写 Nginx 的配置文件的经历. OK,就 ...

  7. FW: Nginx模块开发入门

    前言 Nginx是当前最流行的HTTP Server之一,根据W3Techs的统计,目前世界排名(根据Alexa)前100万的网站中,Nginx的占有率为6.8%.与Apache相比,Nginx在高并 ...

  8. linux下nginx模块开发入门

    本文模块编写参考http://blog.codinglabs.org/articles/intro-of-nginx-module-development.html 之前讲了nginx的安装,算是对n ...

  9. nginx模块开发篇 (阿里著作)

    背景介绍 nginx历史 使用简介 nginx特点介绍 nginx平台初探(100%) 初探nginx架构(100%) nginx基础概念(100%) connection request 基本数据结 ...

  10. 转:nginx模块开发——handler(一)

    handler模块简介 相信大家在看了前一章的模块概述以后,都对nginx的模块有了一个基本的认识.基本上作为第三方开发者最可能开发的就是三种类型的模块,即handler,filter和load-ba ...

随机推荐

  1. 【BZOJ 1433】[ZJOI2009]假期的宿舍

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把每个人都分为左边和右边两个人 xi,yi 如果第i个人不回家或者是外校学生 那么它可以和他认识的人连一条容量为1的边(前提是这个认 ...

  2. [React] Integration test a React component that consumes a Render Prop

    In this lesson, I use Enzyme and Jest's Snapshot functionality to write an integration test for a co ...

  3. 【MFC设置静态文本框背景为透明】

    视图类中加入OnCtlColor()函数: IDC_STATIC1为静态文本框ID HBRUSH CAngleView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT n ...

  4. 自己主动化測试程序之中的一个自己定义键盘的模拟測试程序(C语言)

    一.測试程序编写说明 我们做的终端设备上运行的是QT应用程序.使用自己定义的键盘接口.经过測试人员长时间的人机交互測试,来确认系统的功能是否满足需求. 如今须要编写一个自己主动化的測试程序,能够依照预 ...

  5. 《深入理解Android 卷III》第五章 深入理解Android输入系统

    <深入理解Android 卷III>即将公布.作者是张大伟.此书填补了深入理解Android Framework卷中的一个主要空白.即Android Framework中和UI相关的部分. ...

  6. 【手势交互】9. PS Move

    索尼研发体感控制技术已有10年,在过去那么多年里.尝试了3D摄像头.超声波和电磁感应等各种技术.最后还是觉得眼下的MOVE所使用的技术最为合适.PS Move是索尼于2010年9月份推出.用来让PS3 ...

  7. 什么是域名的TTL值? ——一条域名解析记录在DNS缓存服务器中的存留时间

    什么是域名的TTL值? 转自:http://hizip.net/index.php/archives/20/TTL(Time-To-Live),就是一条域名解析记录在DNS服务器中的存留时间.当各地的 ...

  8. html 移动端关于长按图片弹出保存问题

    在做html5项目的时候有个需求是要拖动一个图片,但是又不要用户长时间按着弹出保存框.首先想到的就是在点图片的时候阻止默认事件的发生: js停止冒泡· function myfn(e){ window ...

  9. 一袭白衣一 IDEA的破解安装以及汉化

    DEA是一款比eclipse用起来更好用的一款代码编辑器,本人之前也是一直在用eclipse来写代码,后来发现了IDEA用起来会更顺手,所以又转用IDEA了,今天给大家分享一下IDEA的下载安装破解以 ...

  10. lsof 命令简介

    losf 命令可以列出某个进程打开的所有文件信息.打开的文件可能是普通的文件,目录,NFS文件,块文件,字符文件,共享库,常规管道,明明管道,符号链接,Socket流,网络Socket,UNIX域So ...