search
HomeBackend DevelopmentPHP TutorialAnatomy of Nginx automatic script (1) Parsing configuration option script auto/options

解剖 Nginx ·自动脚本篇(1)解析配置选项脚本 auto/options
  • Author: Poechant
  • Blog: blog.CSDN.net/Poechant
  • Email: zhongchao.ustc#gmail.com (#->@)
  • Date: March 4th, 2012
  • Copyright ? 柳大·Poechant
  • 在安装Nginx之前(即运行make脚本之前),首先是进行安装的配置准备,包括环境检查及生成文件。这些工作是由自动脚本完成的。和绝大多数软件一样,Nginx的自动脚本的入口,同样是名为configure的文件。除了configure,其他的自动脚本都在auto目录下。通过分析configure脚本源码,我们可以看到,configure首先运行了auto目录下的几个自动脚本,如下:
    <code>. auto/options
    . auto/init
    . auto/sources
    </code>
    其中通过运行auto/options脚本,来设定配置选项。下面将逐步分析auto/options脚本是如何工作的。1 读取configure配置参数开始先声明了 N 多变量,然后最主要的部分从这段开始:
    <code>opt=
    
    for option
        do
        ...
    done
    </code>
    这段实际上是处理运行./configure的时候携带的参数选项,for循环每次对应一个参数选项 option。要注意for循环体上面有一个全局的opt变量。这个循环体内的第一个语句是最重要是,它是:
    <code>opt="$opt `echo $option | sed -e \"s/\(--[^=]*=\)\(.* .*\)/\1'\2'/\"`"
    </code>
    通过循环运行该语句后,opt的值就是一个由空格来分隔的参数列表。然后在循环体中接下来是一个case-esac,用来得到参数值,如下:
    <code>case "$option" in
        -*=*) value=`echo "$option" | sed -e 's/[-_a-zA-Z0-9]*=//'` ;;
           *) value="" ;;
    esac
    </code>
    其含义是将value赋值为参数选项值,如果选项值不与-*=*的模式匹配,则value值为""。接下来的case-esac语句用来匹配参数的类型。
    <code>    case "$option" in
            --help)                          help=yes                   ;;
            --prefix=)                       NGX_PREFIX="!"             ;;
            --prefix=*)                      NGX_PREFIX="$value"        ;;
            --sbin-path=*)                   NGX_SBIN_PATH="$value"     ;;
            --conf-path=*)                   NGX_C     ;;
            ...
        esac
    </code>
    各匹配的分支语句中进行配置变量的赋值。这些变量在auto/options脚本的最开始处赋以默认值,其中那些模块配置变量被赋以YES的表示默认开启,赋以NO的表示默认关闭。但它们开启与否由这个auto/options中的case-esac语句来决定。还有一些是安装相关的选项变量也在这里被赋值,比如:
  • prefix参数值被赋予NGX_PREFIX
  • sbin-path参数值被赋予NGX_SBIN_PATH
  • conf-path参数值被赋予NGX_CONF_PATH
  • error-log-path参数值被赋予NGX_ERROR_LOG_PATH
  • pid-path参数值被赋予NGX_PID_PATH
  • lock-path参数值被赋予NGX_LOCK_PATH
  • 如果option并不符合预设的这些匹配,也就是用户使用configure脚本的时候携带的参数错误,则auto/options会匹配该语句:
    <code>*)
        echo "$0: error: invalid option \"$option\""
        exit 1
    </code>
    从而提示用户参数错误,并使脚本退出运行。经过多次循环,for-do-done就结束。2 设定NGX_CONFIGURE变量处理完所有option后,opt就如我们上面提到的,成为由空格分割的配置项值,并被赋给NGX_CONFIGURE变量:
    <code>NGX_C
    </code>
    3 是否显示configure的帮助信息再看下面这句:
    <code>if [ $help = yes ]; then
    cat </code>
    默认情况下$help变量值在初始化时就为no。如果configure选项中指定了help参数,则$help参数为yes,则会运行cat命令,显示大段的帮助信息,然后退出。4 是否关闭 HTTP 功能默认情况下HTTP的一些基本功能是被开启的,如果用户指定了--without-http参数,则变量HTTP会被赋值为NO,则下面这段代码if-fi中的语句会被执行:
    <code>if [ $HTTP = NO ]; then
        HTTP_CHARSET=NO
        HTTP_GZIP=NO
        HTTP_SSI=NO
        HTTP_USERID=NO
        HTTP_ACCESS=NO
        HTTP_STATUS=NO
        HTTP_REWRITE=NO
        HTTP_PROXY=NO
        HTTP_FASTCGI=NO
    fi
    </code>
    5 是否指定运行于 Windows 平台如果显式指定了--crossbuild参数,则变量NGX_PLATFORM会被赋予当前for-do-done循环中的"$value"值,也就是--crossbuild的参数值,一般在考虑在Windows平台使用时才会用到,看下面的语句:
    <code>if [ ".$NGX_PLATFORM" = ".win32" ]; then
        NGX_WINE=$WINE
    fi
    </code>
    如果指定--crossbuild=win32,则NGX_WINE就会被赋值了。6 Nginx 配置文件路径在加载configure的参数时,如果没有指定了--conf-path参数,则$NGX_CONF_PATH变量是没有值的,则下面的语句会为NGX_CONF_PATH赋以conf/nginx.conf的缺省值。不过我在想老毛子 Igor Sysoev 同学完全可以在auto/options开始处和其他参数一样先指定NGX_CONF_PATH的默认值。
    <code>NGX_C/nginx.conf}
    </code>
    然后获取配置文件所在目录的:
    <code>NGX_C $NGX_CONF_PATH`
    </code>
    如果指定参数--conf-path=/home/michael/nginx/conf/nginx.conf,则NGX_CONF_PREFIX的值就是/home/michael/nginx/conf7 Nginx 进程 ID 文件和锁文件路径下面是同样的方式初始化NGX_PID_PATHNGX_LOCK_PATH,分别对应configure参数--pid-path--lock-path,其缺省值分别为logs/nginx.pidlogs/nginx.lock
    <code>NGX_PID_PATH=${NGX_PID_PATH:-logs/nginx.pid}
    NGX_LOCK_PATH=${NGX_LOCK_PATH:-logs/nginx.lock}
    </code>
    8 错误日志文件路径如果指定了参数--error-log-pathNGX_ERROR_LOG_PATH变量的值会被指定,根据下面的语句,如果指定的是stderr则将NGX_ERROR_LOG_PATH修改为空,即不需要错误日志文件。如果不是标准输出,且其值为空,则设置为缺省值logs/error.log
    <code>if [ ".$NGX_ERROR_LOG_PATH" = ".stderr" ]; then
        NGX_ERROR_LOG_PATH=
    else
        NGX_ERROR_LOG_PATH=${NGX_ERROR_LOG_PATH:-logs/error.log}
    fi
    </code>
    9 HTTP 相关各路径
    <code>NGX_HTTP_LOG_PATH=${NGX_HTTP_LOG_PATH:-logs/access.log}
    NGX_HTTP_CLIENT_TEMP_PATH=${NGX_HTTP_CLIENT_TEMP_PATH:-client_body_temp}
    NGX_HTTP_PROXY_TEMP_PATH=${NGX_HTTP_PROXY_TEMP_PATH:-proxy_temp}
    NGX_HTTP_FASTCGI_TEMP_PATH=${NGX_HTTP_FASTCGI_TEMP_PATH:-fastcgi_temp}
    NGX_HTTP_UWSGI_TEMP_PATH=${NGX_HTTP_UWSGI_TEMP_PATH:-uwsgi_temp}
    NGX_HTTP_SCGI_TEMP_PATH=${NGX_HTTP_SCGI_TEMP_PATH:-scgi_temp}
    </code>
    10 Perl 模块如果指定了--with-perl_modules_path参数,则NGX_PERL_MODULES变量即被设定。如果指定的值为一个绝对路径或未指定(空),则当做相对路径来处理,设定为$NGX_PREFIX/$NGX_PERL_MODULES
    <code>case ".$NGX_PERL_MODULES" in
        ./*)
        ;;
    
        .)
        ;;
    
        *)
            NGX_PERL_MODULES=$NGX_PREFIX/$NGX_PERL_MODULES
        ;;
    esac
    </code>
    11 小结通过运行auto/options脚本,所有的配置项已经被正确解析并加载到相应的配置变量中了。-转载请注明来自“柳大的CSDN博客”:blog.csdn.net/Poechant-

    The above has introduced the anatomy of Nginx automatic script (1) parsing the configuration options script auto/options, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

    Statement
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

    PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

    PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

    PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

    Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

    PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

    PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

    PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

    PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

    PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

    How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

    PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

    How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

    In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

    PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

    PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    1 months agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    1 months agoBy尊渡假赌尊渡假赌尊渡假赌
    Will R.E.P.O. Have Crossplay?
    1 months agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    Powerful PHP integrated development environment

    VSCode Windows 64-bit Download

    VSCode Windows 64-bit Download

    A free and powerful IDE editor launched by Microsoft

    MantisBT

    MantisBT

    Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

    SecLists

    SecLists

    SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.