#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
//当在配置块中出现mytest配置项的时候,就会调用此函数
//此函数用来注册请求到来的实际处理函数 下一个函数
static char *ngx_http_mytest_memory(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *ngx_http_mytest_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
//请求到来且匹配到mytest项的时候实际处理函数
//这个函数在NGX_HTTP_CONTENT_PHASE阶段处理
static ngx_int_t ngx_http_mytest_handler_memory(ngx_http_request_t *r);
static ngx_int_t ngx_http_mytest_handler_file(ngx_http_request_t *r);
//配置项的数组
static ngx_command_t ngx_http_mytest_commands[] = {
{
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_memory,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},
{
ngx_string("myfile"),
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LMT_CONF | NGX_CONF_NOARGS,
ngx_http_mytest_file,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},
ngx_null_command
};
static ngx_http_module_t ngx_http_mytest_module_ctx = {
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
};
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
};
static char *ngx_http_mytest_memory(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_memory;
return NGX_CONF_OK;
}
static char *ngx_http_mytest_file(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_file;
return NGX_CONF_OK;
}
/*
struct ngx_file_s {
ngx_fd_t fd;
ngx_str_t name;
ngx_file_info_t info; //typedef struct stat ngx_file_info_t
off_t offset;
off_t sys_offset;
ngx_log_t *log;
unsigned valid_info:1;
unsigned directio:1;
}
*/
//发送文件
static ngx_int_t ngx_http_mytest_handler_file(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/html");
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_type = type;
//这个缓冲区现在用来保存文件
ngx_buf_t *b;
//给ngx_buf_t分配内存
b = ngx_palloc(r->pool, sizeof(ngx_buf_t));
if(b == NULL) {
return NGX_ERROR;
}
u_char *filename = (u_char *)"/usr/local/nginx/html/mytest.html";
//是否是文件 标志位
b->in_file = 1;
//给文件结构体申请内存
b->file = ngx_palloc(r->pool, sizeof(ngx_file_t));
if(b->file == NULL) {
return NGX_ERROR;
}
//打开文件
//初始化ngx_file_s结构体
b->file->fd = ngx_open_file(filename, NGX_FILE_RDONLY | NGX_FILE_NONBLOCK, NGX_FILE_OPEN, 0);
b->file->log = r->connection->log;
b->file->name.data = filename;
b->file->name.len = sizeof(filename);
//文件打开失败
if(b->file->fd <= 0) {
return NGX_HTTP_NOT_FOUND;
}
//info就是stat结构体
if(ngx_file_info(filename, &b->file->info) == NGX_FILE_ERROR) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
r->headers_out.content_length_n = b->file->info.st_size;
//发送响应头部
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 = b->file->info.st_size; //pos == last 就停止发送
b->last_buf = 1;
//设置发送完毕之后关闭句柄的回调函数
ngx_pool_cleanup_t *cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_pool_cleanup_file_t));
if(cln == NULL) {
return NGX_ERROR;
}
cln->handler = ngx_pool_cleanup_file;
ngx_pool_cleanup_file_t *clnf = cln->data;
clnf->fd = b->file->fd;
clnf->name = b->file->name.data;
clnf->log = r->pool->log;
//发送响应包体
ngx_chain_t out;
out.buf = b;
out.next = NULL;
return ngx_http_output_filter(r, &out);
}
/*
//这个函数定义在/core/ngx_palloc.c中
void ngx_pool_cleanup_file(void *data) {
ngx_pool_cleanup_file_t *c = data;
ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, c->log, 0, "file cleanup: fd:%d", c->fd);
if(ngx_close_file(c->fd) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno, ngx_close_file_n "\"%s\" failed", c->name);
}
}
*/
//此函数用于发送内存数据
static ngx_int_t ngx_http_mytest_handler_memory(ngx_http_request_t *r) {
//方法只能是Get Head
if(!(r->method & (NGX_HTTP_GET | NGX_HTTP_HEAD))) {
return NGX_HTTP_NOT_ALLOWED;
}
//丢弃http请求中请求包体,这样防止客户端发送超时
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;
//设置响应头部Content_Type
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);
}
上述代码文件命名为ngx_http_mytest_module.c
,同时放入任意目录,在目录中添加脚本,命名为config
,这个脚本必须为config
,脚本内容如下:
ngx_addon_name=ngx_http_mytest_module
HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mytest_module.c"
开始编译nginx,注意添加模块路径
./configure --add-module=/root/module
我上面的html
文件在/usr/local/nginx/html/mytest.html
,文件内容随便,一下是我的测试html文件内容:
<html>
<body>
《》《》《》《》《》《》《》
</body>
</html>
重新配置nginx.conf
,在server块中添加下面两个location
location /myfile {
myfile;
}
location /mytest {
mytest;
}
运行nginx,测试内容如下: