当前位置:web 服务器 > Nginx >>

给nginx lua模块添加sendfile函数可代替X-Accel-Redirect

nginx发送静态文件,速度极快,Nginx中的x-sendfile机制需要依靠 X-Accel-Redirect 特性实现,不过经过我的测试,不能满足我的需求,我 要用lua来处理业务逻辑, 然后发送文件内容,一开始用如下方式来实现, 这种方式如果文件小, 到无所谓, 但是当文件很大时, 对性能的影响非常大。
 
[cpp] 
local file = io.open(filePath, "rb")  
  
  
local size = file:seek("end")  
ngx.header["Content-Length"] = size  
file:seek("set", 0)   
data = file:read("*a")  
  
ngx.print(data)  
ngx.flush(true)  
  
file:close()  
 
众所周知, linux内核里有sendfile函数,可以0拷贝发送文件,于是在网上找资料,找到的都是同样的一个文章,都是介绍Nginx的 X-Accel-Redirect 如何使用的, 经过测试 X-Accel-Redirect ,不能满足我的需求。
 
介绍 X-Accel-Redirect 的官方文章地址为 : http://wiki.nginx.org/XSendfile
 
 
 
最后没办法, 只能从源码入手了。
 
 
 
参考了 ngx_http_static_module.c  这个发送静态文件的模块源码, 决定实现一个lua的接口, 可以让lua直接调用sendfile函数, 发送文件内容。
 
 
 
print 函数在 ngx_http_lua_log.c 中实现, 我也把sendfile函数放这里吧, 直接用 ngx_lua的框架。
 
[cpp]  
void  
ngx_http_lua_inject_log_api(lua_State *L)  
{  
    ngx_http_lua_inject_log_consts(L);  
  
    lua_pushcfunction(L, ngx_http_lua_ngx_log);  
    lua_setfield(L, -2, "log");  
  
    lua_pushcfunction(L, ngx_http_lua_print);  
    lua_setglobal(L, "print");  
  
    lua_pushcfunction(L, ngx_http_lua_sendfile); //添加的内容  
    lua_setglobal(L, "sendfile");//添加的内容  
}  
 
上面的代码里 lua_pushcfunction 就是把函数的指针压入堆栈,lua_setglobal 就是用来把 "sendfile"压入堆栈的, 并且设置的是全局函数,全局函数的话, 在lua里调用就是直接 sendfile(),  如果是用 lua_setfield 来压入堆栈的话, 那在lua里就得用  ngx.sendfile () 这样的形式来调用了。  反正是两种都可以, 随便你了。
 
 
下面贴出 ngx_http_lua_sendfile 函数的实现 :
 
[cpp]  
static int ngx_http_lua_sendfile(lua_State *L)  
{  
    u_char                    *last, *location;  
    size_t                     root, len;  
    ngx_http_request_t        *r;  
    ngx_str_t                  path;  
    ngx_int_t                  rc;  
    ngx_uint_t                 level;  
    ngx_log_t                 *log;  
    ngx_buf_t                 *b;  
    ngx_chain_t                out;  
    ngx_open_file_info_t       of;  
    ngx_http_core_loc_conf_t  *clcf;  
    int                        offset;  
    int                        bytes;  
    char                      *filename;  
    int                        nargs;  
  
    lua_pushlightuserdata(L, &ngx_http_lua_request_key);  
    lua_rawget(L, LUA_GLOBALSINDEX);  
    r = lua_touserdata(L, -1);  
    lua_pop(L, 1);  
  
    if (r == NULL)   
    {  
        luaL_error(L, "no request object found");  
    return 1;  
    }  
  
   
    nargs = lua_gettop(L);  
  
    filename = (char *) lua_tolstring(L, 1, &len);  
    offset   = lua_tonumber(L, 2);  
    bytes    = lua_tonumber(L, 3);  
  
    log = r->connection->log;  
  
    path.len = ngx_strlen(filename);  
  
    path.data = ngx_pnalloc(r->pool, path.len + 1);  
    if (path.data == NULL) {  
        return 0;  
    }  
  
    (void) ngx_cpystrn(path.data, (u_char *) filename, path.len + 1);  
  
    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "ngx send lua filename: \"%s\"", filename);  
  
    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);  
  
    ngx_memzero(&of, sizeof(ngx_open_file_info_t));  
  
    of.read_ahead = clcf->read_ahead;  
    of.directio = clcf->directio;  
    of.valid = clcf->open_file_cache_valid;  
    of.min_uses = clcf->open_file_cache_min_uses;  
    of.errors = clcf->open_file_cache_errors;  
    of.events = clcf->open_file_cache_events;  
  
    if (ngx_http_set_disable_symlinks(r, clcf, &path, &of) != NGX_OK)   
    {  
        return 0;//NGX_HTTP_INTERNAL_SERVER_ERROR;  
    }  
  
    if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool) != NGX_OK)  
    {  
        switch (of.err)   
    {  
  
        case 0:  
            return 0;//NGX_HTTP_INTERNAL_SERVER_ERROR;  
  
        case NGX_ENOENT:  
        case NGX_ENOTDIR:  
        case NGX_ENAMETOOLONG:  
  
            level = NGX_LOG_ERR;  
            rc =
补充:软件开发 , C++ ,
Apache
IIS
Nginx
Tomcat
如果你遇到web 服务器难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,