Nginx: Start Process

First, the master boot process

  1. flow chart
    Turn map
  2. The main function
  • ngx_get_options
    parse command parameters
  • ngx_process_options
    configuration prefix, the prefix, configuration files, configuration parameters string
  • ngx_add_inherited_sockets
    carrying out a smooth upgrade, will be passed to the new process by "NGINX" environment variable listening fd, for cycle- new process initialization> listening structures.
  • ngx_init_cycle
    • When the update server and the time zone
      TP = ngx_timeofday ();
      the tp-> = 0 sec;
      ngx_time_update ();

    • Assign a new pool and Cycle
      pool = ngx_create_pool (NGX_CYCLE_POOL_SIZE, log);
      Cycle = ngx_pcalloc (pool, the sizeof (ngx_cycle_t));

    • Initial configuration prefix, the prefix, configuration files, configuration parameters string from the old copy of some data inside old_cycle cycle objects to
      initialize an array of paths
      ngx_array_init (& cycle-> paths, pool , n, sizeof (ngx_path_t *);
      save all nginx directory to be operated, if the directory does not exist, it is created, the creation fails nginx fails to start, call ngx_add_path add .eg.proxy_temp_path as defined directory to store temporary files bearing receives from the proxy server to the data, the path needs to be added to the array path .

    • 初始化config_dump数组
      ngx_array_init(&cycle->config_dump, pool, 1, sizeof(ngx_conf_dump_t);
      ngx_rbtree_init(&cycle->config_dump_rbtree, &cycle->config_dump_sentinel,
      ngx_str_rbtree_insert_value);

    • Open_files initialize an array
      ngx_list_init (& cycle-> open_files, pool , n, sizeof (ngx_open_file_t);
      save the file to open nginx, call ngx_conf_open_file add .eg.nginx log file path interface needs to be saved in the array.

    • Initialization shared_memory list
      ngx_list_init (& cycle-> shared_memory, pool , n, sizeof (ngx_shm_zone_t);
      save the shared memory information nginx to use, the interface call ngx_shared_memory_add added.

    • According listening old_cycle, and initialize the new cycle-> listening.
      IF (ngx_array_init (& cycle-> Listening, the pool, n-, the sizeof (ngx_listening_t))
      = NGX_OK!)
      {
      ngx_destroy_pool (the pool);
      return NULL;
      }

    • Initialization resuable_connections_queue queue.
      ngx_queue_init (& cycle-> reusable_connections_queue);

    • Call the core module of create_conf (), the configuration is stored in an array conf_ctx cycle, the key is the index number compiled kernel modules.
      IF (module-> create_conf) {
      RV = module-> create_conf (Cycle);
      IF (RV == NULL) {
      ngx_destroy_pool (the pool);
      return NULL;
      }
      // [core module] configured as an index for the module id
      cycle-> conf_ctx [cycle-> modules [I] -> index] = RV;
      }

    • Configuration parsing

    • Each file and the list traversal open_files open
      File [I] = .FD ngx_o
      pen_file (File [I] .name.data,
      NGX_FILE_APPEND,
      NGX_FILE_CREATE_OR_OPEN,
      NGX_FILE_DEFAULT_ACCESS);

    • Create a shared memory and initialize (compare the old and new shared_memory the list, the same shared memory retention, old different shared memory is released and the new is created).

    • Create a new listening socket on old_cycle existing sockets inherit, to be non-existent New.
      IF (! ngx_open_listening_sockets (Cycle) = NGX_OK) {
      GOTO failed;}

    • Submit a new cycle configuration, and call all init_module modules (actually only ngx_event_core_module module defines the callback, that only ngx_event_module_init () is called).
      IF (! ngx_init_modules (Cycle) = NGX_OK) {
      / * * fatal /
      Exit (. 1);
      }

    • Old_cycle release excess shared_memory.

    • Old_cycle release listening socket is not in use.

    • Old_cycle release excess open_files.

    • old_cycle cleanup.

Two, master process

  1. ngx_master_process_cycle process
  • 信号屏蔽
    if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) {
    ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
    “sigprocmask() failed”);
    }

    sigemptyset(&set);

  • Modify process name
    ngx_setproctitle (title);

  • 启动worker进程
    ngx_start_worker_processes(cycle, ccf->worker_processes,
    NGX_PROCESS_RESPAWN);
    ngx_start_cache_manager_processes(cycle, 0);

  • Enter the main loop

  1. The main loop flow chart
    Turn map

Three, worker process

  1. ngx_worker_process_init initialization
  • Initializing variables change into
    ngx_set_environment (cycle, NULL);

  • Set process priority
    setpriority (PRIO_PROCESS, 0, ccf-> priority);

  • Number setting file handle limit
    setrlimit (RLIMIT_NOFILE, & rlmt);

  • Provided core_file file
    setrlimit (RLIMIT_CORE, & rlmt);

  • User group
    setgid (CCF> Group);
    initgroups (CCF> username, CCF> Group);
    the setuid (CCF> User);

  • 设置cpu亲和
    ngx_get_cpu_affinity(worker);
    ngx_setaffinity(cpu_affinity, cycle->log);

  • Disposed signal shielding
    sigemptyset (& SET);
    the sigprocmask (SIG_SETMASK, & SET, NULL);

  • 调用init_process()
    cycle->modules[i]->init_process(cycle);

  • channel setting off others fd [1], retention of others fd [0] to communicate with each other. Own fd [1] message received master process. channel events added epoll.

    for (n = 0; n <ngx_last_process; n ++) {
    close (ngx_processes [n] .channel [1]);
    }
    Close (ngx_processes [ngx_process_slot] .channel [0]);
    ngx_add_channel_event (Cycle, ngx_channel, NGX_READ_EVENT,
    ngx_channel_handler)

Guess you like

Origin blog.csdn.net/u013032097/article/details/91426784