当前位置: 首页 > news >正文

ngx_conf_parse

配置文件 


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    #include       mime.types;
    #default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    #sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    #keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

 

    } else if (cf->conf_file->file.fd != NGX_INVALID_FILE) {

        type = parse_block;
    for ( ;; ) {
        rc = ngx_conf_read_token(cf);

接下来略过注释读取 35行的

    server {

以 { 结束

接下来 :

        rc = ngx_conf_handler(cf, rc);

处理读取的 token


进入 ngx_conf_handler 

    name = cf->args->elts;

    found = 0;

此时要 处理的指令是 server

    for (i = 0; cf->cycle->modules[i]; i++) {

        cmd = cf->cycle->modules[i]->commands;
        if (cmd == NULL) {
            continue;
        }

        for ( /* void */ ; cmd->name.len; cmd++) {

            if (name->len != cmd->name.len) {
                continue;
            }

            if (ngx_strcmp(name->data, cmd->name.data) != 0) {
                continue;
            }

遍历,查找 server 指令

直到

i=9

modules[9]->name=ngx_http_core_module

然后通过语法检查

            else if (cf->ctx) {
                confp = *(void **) ((char *) cf->ctx + cmd->conf);

                if (confp) {
                    conf = confp[cf->cycle->modules[i]->ctx_index];
                }
            }

 此时 

cmd->conf=0
ctx_index=0

 

rv = cmd->set(cf, cmd, conf);

此时是

ngx_http_core_module 模块的

server 指令的set 函数指针

 


ngx_http_core_module

定义在 src/http/ngx_http_core_module.c

ngx_module_t  ngx_http_core_module = {
    NGX_MODULE_V1,
    &ngx_http_core_module_ctx,             /* module context */
    ngx_http_core_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
};

ngx_http_core_commands

static ngx_command_t  ngx_http_core_commands[] = {

...
    { ngx_string("server"),
      NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
      ngx_http_core_server,
      0,
      0,
      NULL },
...

set 是 ngx_http_core_server

ngx_http_core_server-CSDN博客


            if (rv == NGX_CONF_OK) {
                return NGX_OK;
            }

返回 NGX_OK 


 回到 ngx_conf_parse

rc = ngx_conf_read_token(cf);

继续读取 token

接下来读取的是

117 行的

}

rc == NGX_CONF_BLOCK_DONE 

        if (rc == NGX_CONF_BLOCK_DONE) {

            if (type != parse_block) {
                ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "unexpected \"}\"");
                goto failed;
            }

            goto done;
        }

 

done:

    if (filename) {
        if (cf->conf_file->buffer->start) {
            ngx_free(cf->conf_file->buffer->start);
        }

        if (ngx_close_file(fd) == NGX_FILE_ERROR) {
            ngx_log_error(NGX_LOG_ALERT, cf->log, ngx_errno,
                          ngx_close_file_n " %s failed",
                          filename->data);
            rc = NGX_ERROR;
        }

        cf->conf_file = prev;
    }

    if (rc == NGX_ERROR) {
        return NGX_CONF_ERROR;
    }

    return NGX_CONF_OK;
}

此时

filename=NULL

返回 NGX_CONF_OK

相关文章:

  • xpp3_min dowload (xpp_3的安装)
  • MySQL聚簇索引和非聚簇索引 通俗易懂
  • 【C++游戏引擎开发】第1周《线性代数》(1):环境配置与基础矩阵类设计
  • pyqt 信号与槽
  • 生物中心论
  • mysqlworkbench导入.sql文件
  • Linux应用:线程基础
  • MATLAB中iscell函数用法
  • 内嵌式触摸显示器在工业视觉设备中的应用
  • python策略模式
  • OpenBMC:BmcWeb 生效路由2 Trie字典树
  • 《Tr0ll2 靶机渗透实战:弱口令+SUID+两种缓冲区溢出+ 提权完整+fcrackzip暴力破解+shellshock漏洞+脏牛三种root提权复盘》
  • 企业级大模型微调
  • SAP-ABAP:SAP IDoc技术详解:架构、配置与实战
  • 若依专题——基础应用篇
  • 如何实现多维度风险排查与合规管理?
  • 如何防御TCP洪泛攻击
  • 考研408-数据结构完整代码 线性表的链式存储结构 - 单链表
  • 多网络选择路由(windows环境)
  • 分布式共识算法解密:从Paxos到Raft的演进之路
  • 关于网站建设中原创文章的一些想法/西安做网站哪家好
  • 织梦网站地图生成/网站关键词搜索排名
  • 网站推广费用/现在有什么推广平台
  • 深圳设计网站建设公司/seo教程seo官网优化详细方法
  • 网站详情页怎么做/社群推广平台
  • 侦探公司做网站的资料/磁力宝最佳搜索引擎入口