Nginx configuration parsing

The first problem: the location of configuration items

If you want to know where the configuration item should be, you need to understand the fact that nginx configuration is provided for whom? We always say that Nginx is a modular program, it is divided into modules, it is natural to think that these configuration items are provided for the use of each module, each module has its own support for the configuration. Further, we can guess that the main determinant of where a configuration item resides is where the module that needs it puts it!

So how does an Nginx module determine where its configuration items are located? Ngx_command_t represents the configuration items required by a module. In each module, an array of type ngx_command_t is implemented. Why arrays? The reason is very simple, a module generally requires more than one configuration item……

Now what does ngx_command_t look like

struct ngx_command_s {
    ngx_str_t             name;
    ngx_uint_t            type;
    char* (*set) (ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
    ngx_uint_t            conf;
    ngx_uint_t            offset;
    void                 *post;
};
Copy the code

Here are the meanings of the variables in the above structure:

field meaning
name The name of the configuration item, for exampleworker_processEtc.
type Determines the properties of your parameters, such as where they appear, how many parameters they have, etc
set When nginx reads the value of the currently specified configuration item from the configuration file, it calls the function pointed to by the function pointer for processing
conf This has to do with how the configuration is organized in memory. To find out where the configuration is stored in memory based on this identifier, you don’t need to understand the meaning of this item, which will be covered later
offset The configuration item of a module is usually stored in a structure body, where offset is the byte offset of the variable storing the configuration item in the whole structure (it is used externally to obtain the value of the configuration item)
post (Generally not needed, later have a chance to talk about)

Ok, now let’s review what our problem was: how is the location of configuration items determined? Why are some configuration items outside the file and others inside a configuration block?

At present, we can give a general and preliminary answer to this question:

Each module provides related configuration items for flexible implementation of its own functions. This configuration item is described by the ngx_command_t structure. Because a module typically requires more than one configuration item, nginx specifies that the module must provide an ngX_command_t array. To specify the location of a single configuration item in the configuration file, you only need to set the ngx_command_t->type value to a specific value.

Below, I’ll give the value of type that confirms the configuration item’s location in the configuration file:

Type the values meaning
NGX_MAIN_CONF Configuration items appear in the global configuration and do not belong to any {} configuration block
NGX_EVENT_CONF The configuration item appears in the events{} block
NGX_MAIL_MAIN_CONF Configuration items appear in mail{} or iMAP {} blocks
NGX_MAIL_SRV_CONF The configuration item appears in the Server {} block, but the Server {} block must belong to either the Mail {} block or the IMAP {} block
NGX_HTTP_MAIN_CONF Configuration items appear in HTTP {} blocks
NGX_HTTP_SRV_CONF The configuration item appears in the Server {} block, but the Server {} block must belong to the HTTP {} block
NGX_HTTP_LOC_CONF The configuration item appears in the location{} block, however the location{} block must belong to the HTTP {} block
NGX_HTTP_UPS_CONF The configuration item appears in the upstream{} block, however, the upstream{} block must belong to the HTTP {} block
NGX_HTTP_SIF_CONF The configuration item appears in the if{} block within the Server {} block, which must belong to the HTTP block; currently only the rewrite module uses this type
NGX_HTTP_LIF_CONF The configuration item appears in the if{} block inside the location{} block, which must belong to the HTTP block; currently only the rewrite module uses this type
NGX_HTTP_LMT_CONF Configuration items appear in the limit_except{} block, however the limit_except block must belong to HTTP {}

Nginx’s handling of the above values in the source code is not discussed in this chapter, because it involves how nginx organizes the configuration file in memory, which will be explained in detail in later chapters. Finally, I’ll take a look at the ngx_command_t array defined by a few random examples of nginx modules:

  • ngx_core_module
static ngx_command_t  ngx_core_commands[] = {

    { ngx_string("daemon"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_FLAG,
      ngx_conf_set_flag_slot,
      0,
      offsetof(ngx_core_conf_t, daemon),
      NULL },

    { ngx_string("master_process"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_FLAG,
      ngx_conf_set_flag_slot,
      0,
      offsetof(ngx_core_conf_t, master),
      NULL },

    { ngx_string("timer_resolution"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_msec_slot,
      0,
      offsetof(ngx_core_conf_t, timer_resolution),
      NULL },

    { ngx_string("pid"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_str_slot,
      0,
      offsetof(ngx_core_conf_t, pid),
      NULL },

    { ngx_string("lock_file"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_str_slot,
      0,
      offsetof(ngx_core_conf_t, lock_file),
      NULL },

    { ngx_string("worker_processes"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
      ngx_set_worker_processes,
      0.0.NULL },

    { ngx_string("debug_points"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_enum_slot,
      0,
      offsetof(ngx_core_conf_t, debug_points),
      &ngx_debug_points },

    { ngx_string("user"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE12,
      ngx_set_user,
      0.0.NULL },

    { ngx_string("worker_priority"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
      ngx_set_priority,
      0.0.NULL },

    { ngx_string("worker_cpu_affinity"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_1MORE,
      ngx_set_cpu_affinity,
      0.0.NULL },

    { ngx_string("worker_rlimit_nofile"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_num_slot,
      0,
      offsetof(ngx_core_conf_t, rlimit_nofile),
      NULL },

    { ngx_string("worker_rlimit_core"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_off_slot,
      0,
      offsetof(ngx_core_conf_t, rlimit_core),
      NULL },

    { ngx_string("worker_shutdown_timeout"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_msec_slot,
      0,
      offsetof(ngx_core_conf_t, shutdown_timeout),
      NULL },

    { ngx_string("working_directory"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_str_slot,
      0,
      offsetof(ngx_core_conf_t, working_directory),
      NULL },

    { ngx_string("env"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
      ngx_set_env,
      0.0.NULL },

    { ngx_string("load_module"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
      ngx_load_module,
      0.0.NULL },

      ngx_null_command
};
Copy the code
  • ngx_http_access_module
static ngx_command_t  ngx_http_access_commands[] = {

    { ngx_string("allow"),
      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF
                        |NGX_CONF_TAKE1,
      ngx_http_access_rule,
      NGX_HTTP_LOC_CONF_OFFSET,
      0.NULL },

    { ngx_string("deny"),
      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF
                        |NGX_CONF_TAKE1,
      ngx_http_access_rule,
      NGX_HTTP_LOC_CONF_OFFSET,
      0.NULL },

      ngx_null_command
};
Copy the code

Configuration parsing series:

Learning with Problems –Nginx Configuration Parsing (Introduction)

Nginx configuration With Problems to Learn