16.Nginx解析配置文件之ngx_conf_parse

本文探讨Nginx配置文件解析过程,重点关注ngx_conf_parse函数的角色,它如何匹配配置项并进行赋值操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上一篇讲的ngx_conf_read_token用于从配置文件内容中提取token,通常在一个配置行结束或者一个配置块开始时返回。

提取了这些token后,我们还需要找到对应的选项并且进行赋值,这就是ngx_conf_parse的作用。


/* core/ngx_conf_file.c */

/* The ten fixed arguments */
static int argument_number[] = {
    NGX_CONF_NOARGS,
    NGX_CONF_TAKE1,
    NGX_CONF_TAKE2,
    NGX_CONF_TAKE3,
    NGX_CONF_TAKE4,
    NGX_CONF_TAKE5,
    NGX_CONF_TAKE6,
    NGX_CONF_TAKE7
};

char *ngx_conf_parse(ngx_conf_t *cf, ngx_str_t *filename)
{
    int               m, rc, found, valid;
    char             *rv;
    void             *conf, **confp;
    ngx_fd_t          fd;
    ngx_str_t        *name;
    ngx_conf_file_t  *prev;
    ngx_command_t    *cmd;

#if (NGX_SUPPRESS_WARN)
    fd = NGX_INVALID_FILE;
    prev = NULL;
#endif

    if (filename) {

        // 以只读模式打开配置文件
        fd = ngx_open_file(filename->data, NGX_FILE_RDONLY, NGX_FILE_OPEN);
        if (fd == NGX_INVALID_FILE) {
            // 如果打开失败, 直接返回NGX_CONF_ERROR
            ngx_log_error(NGX_LOG_EMERG, cf->log, ngx_errno,
                          ngx_open_file_n " %s failed", filename->data);
            return NGX_CONF_ERROR;
        }
        // prev用来保存当前cf的conf_file指针
        prev = cf->conf_file;
        // 从内存池cf->pool申请一块ngx_conf_file_t大小的内存空间, 并使cf->conf_file指向它
        if (!(cf->conf_file = ngx_palloc(cf->pool, sizeof(ngx_conf_file_t)))) {
            return NGX_CONF_ERROR;
        }

        // ngx_fd_info宏就是调用fstat从文件描述符获取文件状态
        // 这里就是获取配置文件的文件状态, 并保存在cf->conf_file->file.info中
        if (ngx_fd_info(fd, &cf->conf_file->file.info) == -1) {
            ngx_log_error(NGX_LOG_EMERG, cf->log, ngx_errno,
                          ngx_fd_info_n " %s failed", filename->data);
        }

        // 从内存池cf->pool中申请一块大小为1024字节的内存空间, 用来作为cf->conf_file的缓冲区,
        // 接下来配置文件的内容就会读取至该缓冲区, 这也意味着每次我们最多只能从配置文件中读取1024字节的内容
        if (!(cf->conf_file->buffer = ngx_create_temp_buf(cf->pool, 1024))) {
            return NGX_CONF_ERROR;
        }

        cf->conf_file->file.fd = fd;
        cf->conf_file->file.name.le
/opt/nginx-sticky-module-ng-master/ngx_http_sticky_module.c: 在函数‘ngx_http_sticky_header_filter’中: /opt/nginx-sticky-module-ng-master/ngx_http_sticky_module.c:475:63: 错误:‘ngx_http_upstream_headers_in_t’没有名为‘cookies’的成员 if (ngx_http_parse_set_cookie_lines(&r->upstream->headers_in.cookies, &ctx->sticky_conf->cookie_name, &transfer_cookie) == NGX_DECLINED) ^ /opt/nginx-sticky-module-ng-master/ngx_http_sticky_module.c:475:3: 错误:传递‘ngx_http_parse_set_cookie_lines’的第 2 个参数时在不兼容的指针类型间转换 [-Werror] if (ngx_http_parse_set_cookie_lines(&r->upstream->headers_in.cookies, &ctx->sticky_conf->cookie_name, &transfer_cookie) == NGX_DECLINED) ^ In file included from /opt/nginx-sticky-module-ng-master/ngx_http_sticky_module.c:9:0: src/http/ngx_http.h:113:18: 附注:需要类型‘struct ngx_table_elt_t *’,但实参的类型为‘struct ngx_str_t *’ ngx_table_elt_t *ngx_http_parse_set_cookie_lines(ngx_http_request_t *r, ^ /opt/nginx-sticky-module-ng-master/ngx_http_sticky_module.c:475:3: 错误:提供给函数‘ngx_http_parse_set_cookie_lines’的实参太少 if (ngx_http_parse_set_cookie_lines(&r->upstream->headers_in.cookies, &ctx->sticky_conf->cookie_name, &transfer_cookie) == NGX_DECLINED) ^ In file included from /opt/nginx-sticky-module-ng-master/ngx_http_sticky_module.c:9:0: src/http/ngx_http.h:113:18: 附注:在此声明 ngx_table_elt_t *ngx_http_parse_set_cookie_lines(ngx_http_request_t *r,
最新发布
06-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值