nginx中ngx_cycle_s的conf_ctx初始化

  • Post author:
  • Post category:其他


ngx_cycle_t

是结构体ngx_cycle_s的别名

typedef struct ngx_cycle_s           ngx_cycle_t;

结构体ngx_cycle_s的定义

struct ngx_cycle_s {
    void                  ****conf_ctx;
    ngx_pool_t               *pool;

    ngx_log_t                *log;
    ngx_log_t                 new_log;

    ngx_uint_t                log_use_stderr;  /* unsigned  log_use_stderr:1; */

    ngx_connection_t        **files;
    ngx_connection_t         *free_connections;
    ngx_uint_t                free_connection_n;

    ngx_module_t            **modules;
    ngx_uint_t                modules_n;
    ngx_uint_t                modules_used;    /* unsigned  modules_used:1; */

    ngx_queue_t               reusable_connections_queue;
    ngx_uint_t                reusable_connections_n;
    time_t                    connections_reuse_time;

    ngx_array_t               listening;
    ngx_array_t               paths;

    ngx_array_t               config_dump;
    ngx_rbtree_t              config_dump_rbtree;
    ngx_rbtree_node_t         config_dump_sentinel;

    ngx_list_t                open_files;
    ngx_list_t                shared_memory;

    ngx_uint_t                connection_n;
    ngx_uint_t                files_n;

    ngx_connection_t         *connections;
    ngx_event_t              *read_events;
    ngx_event_t              *write_events;

    ngx_cycle_t              *old_cycle;

    ngx_str_t                 conf_file;
    ngx_str_t                 conf_param;
    ngx_str_t                 conf_prefix;
    ngx_str_t                 prefix;
    ngx_str_t                 error_log;
    ngx_str_t                 lock_file;
    ngx_str_t                 hostname;
};

conf_ctx

创建

在ngx_init_cycle中创建conf_ctx,分配内存。创建ngx_max_module个void* 内存块

cycle->conf_ctx = ngx_pcalloc(pool, ngx_max_module * sizeof(void *));
    if (cycle->conf_ctx == NULL) {
        ngx_destroy_pool(pool);
        return NULL;
    }

第一次赋值

模块类型为CORE_MODULE,调用核心模块的create_conf方法来分配内存

for (i = 0; cycle->modules[i]; i++) {
        if (cycle->modules[i]->type != NGX_CORE_MODULE) {
            continue;
        }

        module = cycle->modules[i]->ctx;

        if (module->create_conf) {
            rv = module->create_conf(cycle);
            if (rv == NULL) {
                ngx_destroy_pool(pool);
                return NULL;
            }
            cycle->conf_ctx[cycle->modules[i]->index] = rv;
        }
    }

核心模块中有create_conf的模块有

模块

create_conf方法

ngx_core_module

ngx_core_module_create_conf

ngx_openssl_module

ngx_openssl_create_conf

ngx_google_perftools_module

ngx_google_perftools_create_conf

ngx_regex_module

ngx_regex_create_conf

ngx_thread_pool_module

ngx_thread_pool_create_conf

解析模块类型为CORE_MODULE且命令类型为NGX_MAIN_CONF

模块

命令名

命令处理

ngx_core_module

daemon

ngx_conf_set_flag_slot

master_process

ngx_conf_set_flag_slot

timer_resolution

ngx_conf_set_msec_slot

pid

ngx_conf_set_str_slot

lock_file

ngx_conf_set_str_slot

worker_processes

ngx_set_worker_processes

debug_points

ngx_conf_set_enum_slot

user

ngx_set_user

worker_priority

ngx_set_priority

worker_cpu_affinity

ngx_set_cpu_affinity

worker_rlimit_nofile

ngx_conf_set_num_slot

worker_rlimit_core

ngx_conf_set_off_slot

worker_shutdown_timeout

ngx_conf_set_msec_slot

working_directory

ngx_conf_set_str_slot

env

ngx_set_env

load_module

ngx_load_module

ngx_events_module

events

ngx_events_block

ngx_openssl_module

ssl_engine

ngx_openssl_engine

ngx_google_perftools_module

google_perftools_profiles

ngx_conf_set_str_slot

ngx_http_module

http

ngx_http_block

ngx_errlog_module

error_log

ngx_error_log

ngx_mail_module

mail

ngx_mail_block

ngx_regex_module

pcre_jit

ngx_conf_set_flag_slot

ngx_stream_module

stream

ngx_stream_block

ngx_thread_pool_module

thread_pool

ngx_thread_pool

第二次赋值

是在ngx_conf_handler中的判断命令类型是否是NGX_MAIN_CONF,此时(((void **) cf->ctx)[cf->cycle->modules[i]->index])是对应模块的配置信息的地址,其存的值是NULL。

通过命令接口的set方法来设置conf

ngx_conf_handler

if (cmd->type & NGX_DIRECT_CONF) {
                conf = ((void **) cf->ctx)[cf->cycle->modules[i]->index];

            } else if (cmd->type & NGX_MAIN_CONF) {
                conf = &(((void **) cf->ctx)[cf->cycle->modules[i]->index]);

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

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

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

根据命令类型是否是NGX_DIRECT_CONF还是NGX_MAIN_CONF,模块中有命令类型是NGX_DIRECT_CONF则其子接口中都有create_conf方法。

ngx_core_module核心模块中的命令都是NGX_DIRECT_CONF类型

ngx_events_module是如何分配conf_ctx的

其内存布局如下

事件相关模块分配结构

模块

方法

最终创建配置

ngx_event_core_module

ngx_event_core_create_conf

ngx_event_conf_t

ngx_devpoll_module

ngx_devpoll_create_conf

ngx_devpoll_conf_t

ngx_epoll_module

ngx_epoll_create_conf

ngx_epoll_conf_t

ngx_eventport_module

ngx_eventport_create_conf

ngx_eventport_conf_t

ngx_iocp_module

ngx_iocp_create_conf

ngx_iocp_conf_t

ngx_kqueue_module

ngx_kqueue_create_conf

ngx_kqueue_conf_t

ngx_poll_module

NULL

ngx_select_module

NULL

ngx_poll_module(win32)

NULL

ngx_select_module(win32)

NULL

ngx_http_module是如何分配conf_ctx的

其内存布局如下

http相关模块分配结构

模块

方法

最终创建配置

ngx_http_core_module

ngx_http_core_create_main_conf

ngx_http_core_main_conf_t

ngx_http_core_create_srv_conf

ngx_http_core_srv_conf_t

ngx_http_core_create_loc_conf

ngx_http_core_loc_conf_t

ngx_http_addition_filter_module

NULL

NULL

ngx_http_addition_create_conf

ngx_http_addition_conf_t

ngx_http_auth_basic_module

NULL

NULL

ngx_http_auth_basic_create_loc_conf

ngx_http_auth_basic_loc_conf_t

ngx_http_auth_request_module

NULL

NULL

ngx_http_auth_request_create_conf

ngx_http_auth_request_conf_t

ngx_http_autoindex_module

NULL

NULL

ngx_http_autoindex_create_loc_conf

ngx_http_autoindex_loc_conf_t

ngx_http_browser_module

NULL

NULL

ngx_http_browser_create_conf

ngx_http_browser_conf_t

ngx_http_charset_filter_module

ngx_http_charset_create_main_conf

ngx_http_charset_main_conf_t

NULL

ngx_http_charset_create_loc_conf

ngx_http_charset_loc_conf_t

ngx_http_chunked_filter_module

NULL

NULL

NULL

ngx_http_copy_filter_module

NULL

NULL

ngx_http_copy_filter_create_conf

ngx_http_copy_filter_conf_t

ngx_http_dav_module

NULL

NULL

ngx_http_dav_create_loc_conf

ngx_http_dav_loc_conf_t

ngx_http_degradation_module

ngx_http_degradation_create_main_conf

ngx_http_degradation_main_conf_t

NULL

ngx_http_degradation_create_loc_conf

ngx_http_degradation_loc_conf_t

ngx_http_empty_gif_module

NULL

NULL

NULL

ngx_http_fastcgi_module

ngx_http_fastcgi_create_main_conf

ngx_http_fastcgi_main_conf_t

NULL

ngx_http_fastcgi_create_loc_conf

ngx_http_fastcgi_loc_conf_t

ngx_http_flv_module

NULL

NULL

NULL

ngx_http_geoip_module

ngx_http_geoip_create_conf

ngx_http_geoip_conf_t

NULL

NULL

ngx_http_geo_module

NULL

NULL

NULL

ngx_http_grpc_module

NULL

NULL

ngx_http_grpc_create_loc_conf

ngx_http_grpc_loc_conf_t

ngx_http_gunzip_filter_module

NULL

NULL

ngx_http_gunzip_create_conf

ngx_http_gunzip_conf_t

ngx_http_gzip_filter_module

NULL

NULL

ngx_http_gzip_create_conf

ngx_http_gzip_conf_t

ngx_http_gzip_static_module

NULL

NULL

ngx_http_gzip_static_create_conf

ngx_http_gzip_static_conf_t

ngx_http_headers_filter_module

NULL

NULL

ngx_http_headers_create_conf

ngx_http_headers_conf_t

ngx_http_header_filter_module

NULL

NULL

NULL

ngx_http_image_filter_module

NULL

NULL

ngx_http_image_filter_create_conf

ngx_http_image_filter_conf_t

ngx_http_index_module

NULL

NULL

ngx_http_index_create_loc_conf

ngx_http_index_loc_conf_t

ngx_http_limit_conn_module

NULL

NULL

ngx_http_limit_conn_create_conf

ngx_http_limit_conn_conf_t

ngx_http_limit_req_module

NULL

NULL

ngx_http_limit_req_create_conf

ngx_http_limit_req_conf_t

ngx_http_log_module

ngx_http_log_create_main_conf

ngx_http_log_main_conf_t

NULL

ngx_http_log_create_loc_conf

ngx_http_log_loc_conf_t

ngx_http_map_module

ngx_http_map_create_conf

ngx_http_map_conf_t

NULL

NULL

ngx_http_memcached_module

NULL

NULL

ngx_http_memcached_create_loc_conf

ngx_http_memcached_loc_conf_t

ngx_http_mirror_module

NULL

NULL

ngx_http_mirror_create_loc_conf

ngx_http_mirror_loc_conf_t

ngx_http_mp4_module

NULL

NULL

ngx_http_mp4_create_conf

ngx_http_mp4_conf_t

ngx_http_not_modified_filter_module

NULL

NULL

NULL

ngx_http_perl_module

ngx_http_perl_create_main_conf

ngx_http_perl_main_conf_t

NULL

ngx_http_perl_create_loc_conf

ngx_http_perl_loc_conf_t

ngx_http_postpone_filter_module

NULL

NULL

NULL

ngx_http_proxy_module

ngx_http_proxy_create_main_conf

ngx_http_proxy_main_conf_t

NULL

ngx_http_proxy_create_loc_conf

ngx_http_proxy_loc_conf_t

ngx_http_random_index_module

NULL

NULL

ngx_http_random_index_create_loc_conf

ngx_http_random_index_loc_conf_t

ngx_http_range_header_filter_module

NULL

NULL

NULL

ngx_http_range_body_filter_module

NULL

NULL

NULL

ngx_http_realip_module

NULL

NULL

ngx_http_realip_create_loc_conf

ngx_http_realip_loc_conf_t

ngx_http_referer_module

NULL

NULL

ngx_http_referer_create_conf

ngx_http_referer_conf_t

ngx_http_rewrite_module

NULL

NULL

ngx_http_rewrite_create_loc_conf

ngx_http_rewrite_loc_conf_t

ngx_http_scgi_module

ngx_http_scgi_create_main_conf

ngx_http_scgi_main_conf_t

NULL

ngx_http_scgi_create_loc_conf

ngx_http_scgi_loc_conf_t

ngx_http_secure_link_module

NULL

NULL

ngx_http_secure_link_create_conf

ngx_http_secure_link_conf_t

ngx_http_slice_filter_module

NULL

NULL

ngx_http_slice_create_loc_conf

ngx_http_slice_loc_conf_t

ngx_http_split_clients_module

NULL

NULL

NULL

ngx_http_ssi_filter_module

ngx_http_ssi_create_main_conf

ngx_http_ssi_main_conf_t

NULL

ngx_http_ssi_create_loc_conf

ngx_http_ssi_loc_conf_t

ngx_http_ssl_module

NULL

ngx_http_ssl_create_srv_conf

ngx_http_ssl_srv_conf_t

NULL

ngx_http_static_module

NULL

NULL

NULL

ngx_http_stub_status_module

NULL

NULL

NULL

ngx_http_sub_filter_module

NULL

NULL

ngx_http_sub_create_conf

ngx_http_sub_loc_conf_t

ngx_http_try_files_module

NULL

NULL

ngx_http_try_files_create_loc_conf

ngx_http_try_files_loc_conf_t

ngx_http_upstream_module

ngx_http_upstream_create_main_conf

ngx_http_upstream_main_conf_t

NULL

NULL

ngx_http_upstream_hash_module

NULL

ngx_http_upstream_hash_create_conf

ngx_http_upstream_hash_srv_conf_t

NULL

ngx_http_upstream_ip_hash_module

NULL

NULL

NULL

ngx_http_upstream_keepalive_module

NULL

ngx_http_upstream_keepalive_create_conf

ngx_http_upstream_keepalive_srv_conf_t

NULL

ngx_http_upstream_least_conn_module

NULL

NULL

NULL

ngx_http_upstream_random_module

NULL

ngx_http_upstream_random_create_conf

ngx_http_upstream_random_srv_conf_t

NULL

ngx_http_upstream_zone_module

NULL

NULL

NULL

ngx_http_userid_filter_module

NULL

NULL

ngx_http_userid_create_conf

ngx_http_userid_conf_t

ngx_http_uwsgi_module

ngx_http_uwsgi_create_main_conf

ngx_http_uwsgi_main_conf_t

NULL

ngx_http_uwsgi_create_loc_conf

ngx_http_uwsgi_loc_conf_t

ngx_http_v2_filter_module

NULL

NULL

NULL

ngx_http_v2_module

ngx_http_v2_create_main_conf

ngx_http_v2_main_conf_t

ngx_http_v2_create_srv_conf

ngx_http_v2_srv_conf_t

ngx_http_v2_create_loc_conf

ngx_http_v2_loc_conf_t

ngx_http_write_filter_module

NULL

NULL

NULL

ngx_http_xslt_filter_module

ngx_http_xslt_filter_create_main_conf

ngx_http_xslt_filter_main_conf_t

NULL

ngx_http_xslt_filter_create_conf

ngx_http_xslt_filter_loc_conf_t



版权声明:本文为wuli2496原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。