Source code :nginx 1.13.0-release
 
One 、 Preface
      When analyzing source code , You can often see something like #if (NGX_PCRE) .... #endif This is a code snippet , This design can be done without changing the source code , Through the simple definition of macro to achieve the function of opening and closing , But in nginx/src No macro was found in the directory NGX_PCRE Corresponding #define sentence .
      Introduce before event Module time , speak of init_cycle Pair of functions cycle It's initialized , One of the most important steps is to include all module Copy the array of information to this cycle In the corresponding structure (nginx/src/core/ngx_module.c), The function used in the file contains module An array of names ngx_module_names There is no definition in the source code 、 initialization .
      The answer to these two questions should be in the right nginx Source code is executed before compilation ./auto/configure command , Because the output of the command shows some functions 、 Header file detection , So we will focus on nginx/auto/configure In file .
 
Two 、configure Script
      because nginx With a wide range of functional options , Therefore, experienced users will use direct source code compilation 、 How to install . Before compiling , You need to compile the source code by executing the following command .

cd nginx; ./auto/configure --with-pcre && make
among ./auto/configure --with-pcre It needs to be enabled in the source code NGX_PCRE macro , But how to achieve it ?
     
      open nginx/auto/configure file , Found that the file is a shell Script , And called some other files

################## nginx/auto/configure #######################
#!/bin/sh # Copyright (C) Igor Sysoev
# Copyright (C) Nginx, Inc. LC_ALL=C
export LC_ALL # perform auto/options Commands in files , In a row “.” In the current sh Execution in the environment auto/options
# Commands in files ( And source The command works the same ) differ sh The command causes the creation of child processes , because configure
# The command in the file is related to options All the orders are in the same sh In the environment , So variables are visible to each other in both files
. auto/options # Set a variable , And leave it empty
. auto/init # Initialize variables such as :NGX_AUTO_HEADERS_H=$NGX_OBJS/ngx_auto_headers.h
. auto/sources # Set up the core module 、 The source file corresponding to the platform code test -d $NGX_OBJS || mkdir -p $NGX_OBJS echo > $NGX_AUTO_HEADERS_H
echo > $NGX_AUTOCONF_ERR echo "#define NGX_CONFIGURE \"$NGX_CONFIGURE\"" > $NGX_AUTO_CONFIG_H if [ $NGX_DEBUG = YES ]; then
have=NGX_DEBUG . auto/have # Set up NGX_DEBUG=1
fi ..... . auto/cc/conf # Check compiler related options if [ "$NGX_PLATFORM" != win32 ]; then
. auto/headers # Check the relevant header file , And output the result to ngx_auto_headers.h In file
fi . auto/os/conf # Check the functions required by the system platform if [ "$NGX_PLATFORM" != win32 ]; then
. auto/unix # Check unix Some files in the environment 、 function
fi . auto/threads # Statistics should be compiled to nginx Module information in , Create and initialize ngx_module_t *ngx_modules[] and
#char *ngx_module_names[] In two arrays ( These two arrays are in init_cycle In the called ) Deposit
# To the nginx/objs/ngx_modules.c In file
. auto/modules
. auto/lib/conf ....... # Defining variables NGX_SBIN_PATH The value of is "\"$NGX_SBIN_PATH\""
have=NGX_SBIN_PATH value="\"$NGX_SBIN_PATH\"" . auto/define
have=NGX_CONF_PATH value="\"$NGX_CONF_PATH\"" . auto/define
have=NGX_PID_PATH value="\"$NGX_PID_PATH\"" . auto/define ......
      A brief introduction to nginx/auto/configure Something in the file ,configure Not all of the work is concentrated within the document , It provides a framework , The specific work is left to auto/threads、auto/headers Wait for the file to finish , And adopt . auto/conf This way of calling , In this way, variables can be shared ; This approach simplifies configure Compilation of documents , There are also different types of inspection work , Easy to write 、 maintain . Let's answer the two questions raised in the first part :
  1. NGX_PCRE Macro definition , This kind of macro can be defined in nginx/objs/ngx_auto_config.h see , This document was created by have=$ngx_have_feature . auto/have Such statements generate .

    ################ nginx/auto/have ##############
    cat << END >> $NGX_AUTO_CONFIG_H #ifndef $have
    #define $have 1
    #endif END
      In file << The sign is to tell shell Standard input comes from a pair of separators ( You can make the string 、 Number and so on , Just make sure the beginning and the end are the same ) In the middle ; therefore ,have The command in the file is to use cat Put the separator END Between 5 The line content is appended to $NGX_AUTO_CONFIG_H In file . The effect is as follows :

#ifndef NGX_PCRE
#define NGX_PCRE 1
#endif
      This answers the first question .
 
  1. auto/configure There's a line in the file . auto/modules, This file defines how to register to nginx The information of each module in and the corresponding source file , Then traverse the file containing all the variables that define the module name modules, Automatic generation ngx_module_t *ngx_modules[] and char *ngx_module_names[] Two arrays , And written to the  $NGX_MODULES_C In file . This explains where the two arrays are defined in the second question .

    ############## nginx/auto/modules ################
    ......
    modules="$modules $MISC_MODULES" cat << END > $NGX_MODULES_C #include <ngx_config.h>
    #include <ngx_core.h> $NGX_PRAGMA END # Declare a module as a global variable
    for mod in $modules
    do
    echo "extern ngx_module_t $mod;" >> $NGX_MODULES_C
    done # Define and initialize ngx_module_t *ngx_modules[] Array , Then the output is redirected to $NGX_MODULES_C
    echo >> $NGX_MODULES_C
    echo 'ngx_module_t *ngx_modules[] = {' >> $NGX_MODULES_C for mod in $modules
    do
    echo " &$mod," >> $NGX_MODULES_C
    done cat << END >> $NGX_MODULES_C
    NULL
    }; END # Define and initialize char *ngx_module_names[] Array , Then the output is redirected to $NGX_MODULES_C
    echo 'char *ngx_module_names[] = {' >> $NGX_MODULES_C for mod in $modules
    do
    echo " \"$mod\"," >> $NGX_MODULES_C
    done cat << END >> $NGX_MODULES_C
    NULL
    }; END
    .......

    nginx/auto/modules This file generates two arrays for cycle The initialization , So if a module developed by a developer is to be added to nginx in , Be sure to change nginx/auto/modules This file , Otherwise it won't be compiled to nginx Medium ( Of course it won't work ).

3、 ... and 、 Method 、 Function check validation
      because nginx Cross platform features , So we added a lot of functions on different platforms ( These platform related functions 、 Method is also defined by macro to decide whether to call ),nginx stay configure We need to find out the supporting methods in the current environment 、 function , And then we put these supporting methods 、 Functions are defined in a macro way .

################# nginx/auto/unix ###############
......
# Define the current feature The elements of
ngx_feature="poll()"
ngx_feature_name=
ngx_feature_run=no
ngx_feature_incs="#include <poll.h>"
ngx_feature_path=
ngx_feature_libs=
# Test current feature Whether the code snippet 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"
# Use the variables defined above to test poll() This function feature Is it available
. auto/feature # If the above test results show that the feature Unavailable , Set the corresponding macro to NONE
if [ $ngx_found = no ]; then
EVENT_POLL=NONE
fi
......

################# nginx/auto/feature ###############
...... # utilize cat The order will END The content between the separators ( And testing feature A simple C Program )
# writes $NGX_AUTOTEST.c In file
cat << END > $NGX_AUTOTEST.c #include <sys/types.h>
$NGX_INCLUDE_UNISTD_H
$ngx_feature_incs int main(void) {
$ngx_feature_test;
return 0;
} END # Link will be compiled $NGX_AUTOTEST.c The command assigned to 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= # perform ngx_test Variable directed compilation 、 Link command , Finish right $NGX_AUTOTEST.c Compilation of 、 Link generation executable NGX_AUTOTEST
eval "/bin/sh -c \"$ngx_test\" >> $NGX_AUTOCONF_ERR 2>&1" # Check to see if the executable program was generated successfully $NGX_AUTOTEST
if [ -x $NGX_AUTOTEST ]; then
# according to feature The type of , Using different schemes to verify feature The feasibility of
case "$ngx_feature_run" in
yes)
# /bin/sh is used to intercept "Killed" or "Abort trap" messages
# Execute the corresponding executable program , And reorients the output
if /bin/sh -c $NGX_AUTOTEST >> $NGX_AUTOCONF_ERR 2>&1; then
# Successful implementation , take ngx_found set yes, It means that we should feature You can use
echo " found"
ngx_found=yes # call auto/have file , stay $NGX_AUTO_CONFIG_H In file , Set the feature The value of the corresponding macro is 1( Enable this feature)
if test -n "$ngx_feature_name"; then
have=$ngx_have_feature . auto/have
fi else
echo " found but is not working"
fi
;;
......
    nginx This kind of demo Authentication mechanism , It can check whether the current system has corresponding methods 、 function , It also validates the method of ownership 、 Does the function provide the desired functionality . This situation also reminds us that , function nginx Production environment and compilation of nginx Keep the development environment consistent .

nginx Source code analysis ——configure More about scripts

  1. nginx Source code analysis - Source structure

    This article mainly introduces nginx Source directory structure . Program compilation process . How to build learning nginx And so on . This article and the following nginx Source code analysis article is based on nginx At present (2009-02-27) Stable version of 0.6.35 The analysis carried out , The ...

  2. nginx Source code analysis of network initialization

    nginx As a high performance HTTP The server , The processing of the network is its core , Understanding the initialization of the network helps deepen the understanding of nginx Understanding of network processing , This article mainly through nginx Source code to analyze its network initialization . Read initialization information from configuration file And the net ...

  3. nginx Source code analysis -- Use GDB debugging (strace、 pstack )

    nginx Source code analysis -- Use GDB debugging (strace.  pstack ) http://blog.csdn.net/scdxmoe/article/details/49070577

  4. Nginx Source code analysis :3 See how startup and process work

    Editor's note : Sharing and disseminating articles of typical significance in the field of architecture , This article is shared by Chen Ke in the high availability architecture group . Please indicate the official account number from the high availability architecture 「ArchNotes」.   Reading guide : Many engineers and architects want to know and master high performance servers ...

  5. nginx Source code analysis thread pool details

    nginx Source code analysis thread pool details One . Preface      nginx It's a multi process model ,master and worker It's mainly through pipe Pipeline communication , The advantage of multi process is that each process does not affect each other . But often people ask ,n ...

  6. Nmap Source code analysis ( The script engine )

    Nmap Provides a powerful scripting engine (NSE), To support the passage of Lua Programming to extend Nmap The function of . At present, the script library already contains 300 Several commonly used Lua Script , Assist to complete Nmap The host found . Port scanning . Service detection . The operating system detects four basic functions ...

  7. nginx Source code analysis :module Mechanism

    according to nginx Official documents , Add one module Introduction to , When we need to add a module when , You need to do the following : 1. For the sake of module Create a new directory . 2. Add one config file , One module Core code source file . ...

  8. Nginx Source code analysis -- Array ( turn )

    Original address :http://blog.csdn.net/marcky/article/details/5747431 remarks : Here's about Nginx The analysis of source code is based on Taobao open source project Tengine. Nginx Medium to array ...

  9. [nginx] nginx Source code analysis --SNI Performance analysis

    Concept We already know what is SNI, And how to configure SNI. [nginx] nginx Use SNI Functional approach problem By looking at the configuration file , You can find , For each SSL/TLS link , nginx Will be dynamic search ( ...

Random recommendation

  1. map function ----filter function

    # map function l = (1,2,4,5,6,7,8,9,) print(list(map(lambda x:x**2,l)))# Use list type ((map function (lambda  Anonymous function definition x value :x* ...

  2. itput

    This website is also very good , There's a lot of Finance it Knowledge is free to download , Download verification code t7QA, The name is new communism

  3. Android Implementation of access to application related information list method

    This paper is about Androdi How to get the list of mobile apps , Like getting Android The software properties of the application . Size and application path . Application name, etc , Get all installed Android Application list , Including those that have been unloaded , But there is no application to clear data ...

  4. NIO The Yijinjing in

    Zero ingenuity Reprint please indicate the original source , thank you ! Preface < Yijinjing >. The world's martial arts come out of Shaolin , And Yijinjing is the treasure of Shaolin Temple . If you learn the Yijinjing, you can learn other martial arts easily , Only a few people have learned the whole essence of it . You Tanzhi just happened to learn ...

  5. ( Reprint ) Depth analysis | Adaptive normalization of differentiable learning (Switchable Normalization)

    Depth analysis | Adaptive normalization of differentiable learning (Switchable Normalization) author : volare . Ren Jiamin . Peng Zhanglin To write : Wu Lingyun . Zhang ruimao . Shao Wenqi . Wang Xinjiang from : You know . Reference to the original paper arXiv:180 ...

  6. Big data technology - MapReduce Of Shuffle And tuning

    Let's learn about this chapter MapReduce Medium Shuffle The process ,Shuffle It happened in map Output to reduce Input process , Its Chinese explanation is “ Shuffle ”, As the name suggests, the process involves the redistribution of data , The main ...

  7. null And undefined The difference between

    null and undefined yes JavaScript Two of the five basic data types . null It's a special value , But we often misunderstand it , Sometimes we associate it with another data type undefined The meanings of are confused with each other . First of all, let's understand ...

  8. Servlet A few key knowledge points of

    1.ServletConfig ServletConfig yes Servlet Configuration file for . Corresponding to web.xml Medium <servlet></servlet> label .ServletConf ...

  9. iOS Development -- Use VVDocumenter-Xcode Add code comments

    Developing Java In the process of coding , We just need to Eclipse Knock on /** You can generate fields . Method , Simple and convenient . stay Xcode If you want to add a document comment , It takes a lot of time , There is no simple . A quick way to do this ? I searched the Internet for ...

  10. 【BZOJ 4180】 4180: String count (SAM+ Two points + Matrix multiplication )

    4180: String count Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 164  Solved: 75 Description SD There is a god named Oxe ...