Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of configure script in nginx

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the example analysis of the configure script in nginx, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Nginx Source Code Analysis-- configure script

I. Preface

When analyzing the source code, you can often see something like # if (NGX_PCRE). The code snippet such as # endif can be designed to open and close the function by simply defining the macro without changing the source code, but the # define statement corresponding to the macro NGX_PCRE is never found in the nginx/src directory.

In the previous introduction to the event module, we talked about initializing cycle in the init_cycle function, in which a very important step is to copy the array containing all module information into the corresponding structure of the cycle (nginx/src/core/ngx_module.c). The array ngx_module_names containing the module name used in the file function has not been defined or initialized in the source code.

The answer to the above two questions should be executed in the. / auto/configure command before compiling the nginx source code, because the output of this command shows the detection of some functions and header files, so focus on the analysis in the nginx/auto/configure file.

II. Configure script

Because nginx has a wealth of functional options, experienced users will use direct source code compilation and installation. Before compilation, you need to complete the compilation of the source code by executing the following command.

Cd nginx;. / auto/configure-- with-pcre & & make

Among them. / auto/configure-- with-pcre just needs to enable the NGX_ PCR macro in the source code, but how do you do it?

Open the nginx/auto/configure file, find that it is a shell script, and call some other files

# nginx/auto/configure #! / bin/sh # Copyright (C) Igor Sysoev# Copyright (C) Nginx, Inc. LC_ALL=Cexport LC_ALL # execute the command in the auto/options file, "." It means that executing the command in the auto/options# file in the current sh environment (the same effect as the source command) is different from the sh command will lead to the creation of a child process. Because the command in the configure# file and the command in options are in the same sh environment, the variables are visible in both files. Auto/options # sets the variable and leaves it empty. Auto/init # initializes some variables such as: NGX_AUTO_HEADERS_H=$NGX_OBJS/ngx_auto_headers.h. Auto/sources # set the source file test-d $NGX_OBJS for the core module and platform code | | mkdir-p $NGX_OBJS echo > $NGX_AUTO_HEADERS_Hecho > $NGX_AUTOCONF_ERR echo "# define NGX_CONFIGURE\" $NGX_CONFIGURE\ "> $NGX_AUTO_CONFIG_H if [$NGX_DEBUG = YES]; then have=NGX_DEBUG. Auto/have # sets NGX_DEBUG=1fi. . Auto/cc/conf # check compiler related options if ["$NGX_PLATFORM"! = win32]; then. Auto/headers # examines the relevant header files and outputs the results to the ngx_auto_headers.h file fi. Auto/os/conf # check the function required by the system platform if ["$NGX_PLATFORM"! = win32]; then. Auto/unix # check some files and functions in unix environment, fi. Auto/threads # counts the module information to be compiled into nginx, create and initialize the ngx_module_t * ngx_modules [] and # char * ngx_module_names [] arrays (which are called in init_cycle) and store # in the previously created nginx/objs/ngx_modules.c file. Auto/modules. Auto/lib/conf. # define the value of the variable NGX_SBIN_PATH as "\" $NGX_SBIN_PATH\ "" have=NGX_SBIN_PATH value= "\" $NGX_SBIN_PATH\ "". Auto/define have=NGX_CONF_PATH value= "\" $NGX_CONF_PATH\ ". Auto/definehave=NGX_PID_PATH value= "\" $NGX_PID_PATH\ ". Auto/define.

The above briefly introduces some contents of the nginx/auto/configure file, configure does not concentrate all the work inside the file, but provides a framework, the specific work is handed over to auto/threads, auto/headers and other files to complete, and adopted. Auto/conf is called so that variables can be shared, which not only simplifies the writing of configure files, but also separates different types of checking work, which is easy to write and maintain. Here are two questions raised in the first part:

NGX_PCRE macro definition, this kind of macro definition can be seen in nginx/objs/ngx_auto_config.h, this file is created by have=$ngx_have_feature. Generated by statements like auto/have.

# nginx/auto/have # cat > $NGX_AUTO_CONFIG_H # ifndef $have#define $have 1#endif END

$NGX_MODULES_Cdone # defines and initializes the ngx_module_t * ngx_modules [] array in the file, and then the output is redirected to $NGX_MODULES_Cecho > > $NGX_MODULES_Cecho 'ngx_module_t * ngx_modules [] = {' > > $NGX_MODULES_C for mod in $modulesdo echo "& $mod," > > $NGX_MODULES_Cdone cat > $NGX_MODULES_C NULL} END # defines and initializes the char * ngx_module_names [] array, and then redirects the output to $NGX_MODULES_Cecho 'char * ngx_module_names [] = {' > $NGX_MODULES_C for mod in $modulesdo echo "\" $mod\ "," > > $NGX_MODULES_Cdone cat > $NGX_MODULES_C NULL}; END.

The two arrays generated by the nginx/auto/modules file are used for initializing cycle, so if developers want to add modules to nginx, be sure to modify the nginx/auto/modules file, otherwise it will not be compiled into nginx (of course it won't work).

Third, checking and verifying methods and functions

Due to the cross-platform nature of nginx, many functions located on different platforms have been added (these platform-related functions and methods are also called by macro definition). When nginx configure, you need to find out the methods and functions supported in the current environment, and then determine these supported methods and functions by macro definition.

# nginx/auto/unix #. # define each element of the current feature ngx_feature= "poll ()" ngx_feature_name=ngx_feature_run=nongx_feature_incs= "# include" ngx_feature_path=ngx_feature_libs=# to test whether the current feature is available ngx_feature_test= "int n; struct pollfd pl; pl.fd = 0 Pl.events = 0; pl.revents = 0; n = poll (& pl, 1,0); if (n =-1) return 1 "# uses the variables defined above to test whether the feature of the poll () function is available. Auto/feature # if the above test results show that the feature is not available, set the corresponding macro to NONEif [$ngx_found = no]; then EVENT_POLL=NONEfi.# nginx/auto/feature #. # use the cat command to write the contents between the END delimiters (a simple C program combined with the code snippet of the test feature) # to the $NGX_AUTOTEST.c file cat $NGX_AUTOTEST.c # include $NGX_INCLUDE_UNISTD_H$ngx_feature_incs int main (void) {$ngx_feature_test; return 0 } END # assigns the command to compile the link $NGX_AUTOTEST.c to the ngx_test variable ngx_test= "$CC $CC_TEST_FLAGS $CC_AUX_FLAGS $ngx_feature_inc_path\-o $NGX_AUTOTEST $NGX_AUTOTEST.c $NGX_TEST_LD_OPT $ngx_feature_libs" ngx_feature_inc_path= # to execute the compile and link instructions pointed to by the ngx_test variable Complete the compilation of $NGX_AUTOTEST.c and link to generate the executable program NGX_AUTOTESTeval "/ bin/sh-c\" $ngx_test\ "> > $NGX_AUTOCONF_ERR 2 > & 1" # check whether the executable program $NGX_AUTOTESTif [- x $NGX_AUTOTEST] is generated successfully Then # according to the type of feature, use different schemes to verify the feasibility of feature case "$ngx_feature_run" in yes) # / bin/sh is used to intercept "Killed" or "Abort trap" messages # execute the corresponding executable program, and redirect the output if / bin/sh-c $NGX_AUTOTEST > > $NGX_AUTOCONF_ERR 2 > & 1 If then # executes successfully, set ngx_found to yes, which means that the feature can call the auto/have file with echo "found" ngx_found=yes #. In the $NGX_AUTO_CONFIG_H file, set the value of the feature corresponding to if test to 1 (enable the feature) if test-n "$ngx_feature_name"; then have=$ngx_have_feature. Auto/have fi else echo "found but is not working" fi;.

This demo verification mechanism of nginx not only checks whether the current system has the corresponding methods and functions, but also verifies whether the methods and functions provide the desired functions. This situation also reminds us that the production environment in which nginx is running is consistent with the development environment in which nginx is compiled.

Thank you for reading this article carefully. I hope the article "sample Analysis of configure Scripts in nginx" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report