【Nginx】磁盘文件写入飞地发

【Nginx】磁盘文件写入飞地发

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

文章继续。什么时候Nginx当用户请求一个文件,这将无法读取该文件的内容加载到内存,然后从内存发送,但电话sendfile况下,从内核直接发送出去。这样做显然效率要更高。Nginx也为我们封装好了一系列的接口。以下就来说明怎样发送一个磁盘文件给client。


和从内存直接发送数据最大的不同在于ngx_buf_t缓冲区的设置方法。ngx_buf_t结构体的定义例如以下:
struct ngx_buf_s {
    ....
    off_t            file_pos;     // 文件起始位置
    off_t            file_last;    // 文件结束位置
    ....
    ngx_file_t      *file;     // 引用的文件
    ....
};

ngx_file_t表示一个文件:
typedef struct ngx_file_s        ngx_file_t;
struct ngx_file_s {
    ngx_fd_t                   fd;              // 文件描写叙述符
    ngx_str_t                  name;            // 文件名称
    ngx_file_info_t            info;            // 文件相关信息,相当于stat结构体
 
    off_t                      offset;          // 告诉Nginx处理到文件何处了。一般不使用
    off_t                      sys_offset;      // 文件偏移量,一般不使用
 
    ngx_log_t                 *log;             // 日志对象
 
#if (NGX_HAVE_FILE_AIO)
    ngx_event_aio_t           *aio;
#endif
 
    unsigned                   valid_info:1;    // 未使用
    unsigned                   directio:1;      // 发送大文件时设为1
};

发送响应包体之前就须要对上述部分成员进行设置。以确定须要发送的文件和相关信息。

为了防止内存泄漏,HTTP框架须要在发送响应结束之后关闭文件描写叙述符,这是须要定义一个ngx_pool_cleanup_t结构体:
struct ngx_pool_cleanup_s {
    ngx_pool_cleanup_pt   handler;  // 运行实际清理工作的回调方法
    void                 *data;     // 回调方法的參数
    ngx_pool_cleanup_t   *next;     // 下一个清理对象
};

清理文件句柄的完整代码例如以下:
// 用于告诉HTTP框架,请求结束时调用cln->handler成员函数
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专用于关闭文件句柄
     
ngx_pool_cleanup_file_t  *clnf = cln->data; // cln->data为上述回调函数的參数
clnf->fd = b->file->fd;
clnf->name = b->file->name.data;
clnf->log = r->pool->log;

handler成员设为了ngx_pool_cleanup_file函数,这是Nginx本身内置的一个函数:
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);
    }
}

清理函数主要工作就是关闭文件描写叙述符,它接受的參数为一个ngx_pool_cleanup_file_t指针。该结构体定义例如以下:
typedef struct {
    ngx_fd_t              fd;   // 文件描写叙述符
    u_char               *name; // 文件名称
    ngx_log_t            *log;  // 日志对象
} ngx_pool_cleanup_file_t;

这三个成员和ngx_buf_t.ngx_file_t中的下面三个成员一一相应:
  • ngx_buf_t.ngx_file_t.df
  • ngx_buf_t.ngx_file_t.name
  • ngx_buf_t.ngx_file_t.log
所以我们仅仅须要用ngx_buf_t内的成员对ngx_pool_cleanup_file_t结构体赋值就可以。


程序总体框架和从内存发送数据同样,以下是完整的代码:
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
 
static char *ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
 
static ngx_int_t ngx_http_mytest_handler(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,
        NGX_HTTP_LOC_CONF_OFFSET,
        0,
        NULL
    },
 
    ngx_null_command
};
 
static ngx_http_module_t  ngx_http_mytest_module_ctx =
{
    NULL,                  /* preconfiguration */
    NULL,                  /* postconfiguration */
 
    NULL,                  /* create main configuration */
    NULL,                  /* init main configuration */
 
    NULL,                  /* create server configuration */
    NULL,                  /* merge server configuration */
 
    NULL,                /* create location configuration */
    NULL                   /* merge location configuration */
};
 
ngx_module_t  ngx_http_mytest_module =
{
    NGX_MODULE_V1,
    &ngx_http_mytest_module_ctx,           /* module context */
    ngx_http_mytest_commands,              /* module directives */
    NGX_HTTP_MODULE,                       /* module type */
    NULL,                                  /* init master */
    NULL,                                  /* init module */
    NULL,                                  /* init process */
    NULL,                                  /* init thread */
    NULL,                                  /* exit thread */
    NULL,                                  /* exit process */
    NULL,                                  /* exit master */
    NGX_MODULE_V1_PADDING
};
 
static char *ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
    ngx_http_core_loc_conf_t  *clcf;
 
    // 首先找到mytest配置项所属的配置块,clcf貌似是location块内的数据
    // 结构,事实上不然。它能够是main、srv或者loc级别配置项,也就是说在每一个
    // http{}和server{}内也都有一个ngx_http_core_loc_conf_t结构体
    clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
 
    // http框架在处理用户请求进行到NGX_HTTP_CONTENT_PHASE阶段时。假设
    // 请求的主机域名、URI与mytest配置项所在的配置块相匹配,就将调用我们
    // 实现的ngx_http_mytest_handler方法处理这个请求
    clcf->handler = ngx_http_mytest_handler;
 
    return NGX_CONF_OK;
}
 
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r)
{
    // 必须是GET或者HEAD方法,否则返回405 Not Allowed
    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_buf_t *b;
    b = ngx_palloc(r->pool, sizeof(ngx_buf_t));
 
    u_char* filename = (u_char*)"/tmp/test.txt";   // 要打开的文件名称
    b->in_file = 1;     // 设置为1表示缓冲区中发送的是文件
 
    // 分配代表文件的结构体空间。file成员表示缓冲区引用的文件
    b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
    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;      // name成员表示文件名称称
    b->file->name.len = sizeof(filename) - 1;
    if (b->file->fd <= 0)
        return NGX_HTTP_NOT_FOUND;
 
    r->allow_ranges = 1;    //支持断点续传
 
    // 获取文件长度,ngx_file_info方法封装了stat系统调用
    // info成员就表示stat结构体
    if (ngx_file_info(filename, &b->file->info) == NGX_FILE_ERROR)
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
 
    // 设置缓冲区指向的文件块
    b->file_pos = 0;                        // 文件起始位置
    b->file_last = b->file->info.st_size;   // 文件结束为止
 
    // 用于告诉HTTP框架。请求结束时调用cln->handler成员函数
    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专用于关闭文件句柄
     
    ngx_pool_cleanup_file_t  *clnf = cln->data; // cln->data为上述回调函数的參数
    clnf->fd = b->file->fd;
    clnf->name = b->file->name.data;
    clnf->log = r->pool->log;
 
    // 设置返回的Content-Type
    // 注意,ngx_str_t有一个非常方便的初始化宏
    // ngx_string,它能够把ngx_str_t的data和len成员都设置好
    ngx_str_t type = ngx_string("text/plain");
 
    //设置返回状态码
    r->headers_out.status = NGX_HTTP_OK;
    r->headers_out.content_length_n = b->file->info.st_size;    // 正文长度
    r->headers_out.content_type = type;
 
    // 发送http头部
    rc = ngx_http_send_header(r);
    if (rc == NGX_ERROR || rc > NGX_OK || r->header_only)
        return rc;
 
    // 构造发送时的ngx_chain_t结构体
    ngx_chain_t out;
    out.buf = b;
    out.next = NULL;
 
    //最后一步发送包体,http框架会调用ngx_http_finalize_request方法
    return ngx_http_output_filter(r, &out);
}

依据程序的指示。在/tmp文件夹内创建test.txt文件,内容例如以下:
【Nginx】磁盘文件写入飞地发


执行结果:
【Nginx】磁盘文件写入飞地发

Nginxserver成功返回了test.txt文件内的内容。

參考:
《深入理解Nginx》 P107-P112.

版权声明:本文博客原创文章,博客,未经同意,不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117562.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • cpus Vs cpu period and cpu quota[通俗易懂]

    1.https://docs.oracle.com/cd/E37670_01/E75728/html/section-zb1_wqw_gt.htmlTocontrolacontainer’sCPUusage,youcanusethe–cpu-periodand–cpu-quotaoptionswiththedockercreateanddocker…

    2022年4月13日
    137
  • Java工程师就业前景及薪资水平

    Java工程师就业前景及薪资水平Java工程师就业前景及薪资水平在互联网+的影响下,这几年,中国的互联网行业进入了高速发展的阶段,IT行业成为热门,备受追捧和关注。在全球云计算和移动互联网的产业环境下,想参加Java培训成为Java工程师也是很多小伙伴的追求,那么Java工程师就业前景如何?薪资水平高不高呢?下面就来具体了解一下吧。一、Java工程师就业前景1、Android开发:Android是全球最大的智能手机操作系统,根据StrategyAnalytics研究报告显示,全球智能手机出货量在2016年第三季度达到3.75亿台

    2022年7月9日
    19
  • ubuntu密码设置规则_密码复杂性策略

    ubuntu密码设置规则_密码复杂性策略版本查看cat/etc/issuecat/proc/version内核查看uname-aUbuntu修改密码及密码复杂度策略设置方法一、修改密码1、修改普通用户密码passwd2、修改root用户密码sudopasswdroot默认root用户被禁止登录,如果需要解除限制,修改配置即可sudovim/etc/ssh/sshd_config将默认配置注释掉,添加一行新的配置,默认的配置为允许root登录,但是禁止root用密码登录将默认配置注释掉,添加一行新

    2022年9月29日
    2
  • js刷新当前页面方法「建议收藏」

    js刷新当前页面方法「建议收藏」js刷新当前页面js刷新当前页面在写JS代码时,用到JS来刷新当前页面的方法有几种,比如最常用的reload(),location等reload方法,该方法强迫浏览器刷新当前页面。语法:location.reload([bForceGet])参数:bForceGet,可选参数,默认为false,从客户端缓存里取当前页。true,则以GET方式,从服务端取最新的页面,相当于客户端点击F5(“刷新”)replace方法,该方法通过指定URL替换当前缓存在历史里(客

    2025年7月24日
    3
  • error LNK2019: 无法解析的外部符号_error lnk2019无法解析的外部符号

    error LNK2019: 无法解析的外部符号_error lnk2019无法解析的外部符号废话不多说,直接上错误图片原本是生成一个静态链接库,然后在主函数中进行库函数的调用,编写静态库时,没存在此问题,在主函数中一调用就报错。添加库连接,附加依赖项——也无法彻底解决此问题。原来是从VisualStudio2010开始,VisualStudio要求项目到项目引用。如果你的项目没有项目到项目引用,那么你可能收到此链接器错误。添加项目到项目引用以修复此错误。…

    2022年9月28日
    3
  • HTTP 304状态码的详细讲解

    HTTP304错误状态码的详细讲解304缓存策略及实现网页web常见状态码:200,301,302,304,400,401,404,500

    2022年4月4日
    64

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号