nginx模組開發

fangjian1204發表於2014-08-21

開發方法參考淘寶的教程

這個模組的功能是向客戶端傳送一個檔案,類似於網頁上的另存為功能

#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

相關文章