zoukankan      html  css  js  c++  java
  • Nginx源码完全注释(9)nginx.c: ngx_get_options

    本文分析 ngxin.c 中的 ngx_get_options 函数,其影响:

    nginx.c 中的:

    
    static ngx_uint_t   ngx_show_help;
    static ngx_uint_t   ngx_show_version;
    static ngx_uint_t   ngx_show_configure;
    static u_char      *ngx_prefix;
    static u_char      *ngx_conf_file;  
    static u_char      *ngx_conf_params;    
    static char        *ngx_signal;
    

    ngx_cycle.c 中的:

    
    ngx_uint_t             ngx_test_config;
    ngx_uint_t             ngx_quiet_mode;
    

    ngx_process_cycle.c(src/os/win32 或 src/os/unix)中的:

    
    ngx_uint_t    ngx_process;
    

    这些变量的作用域由 static 限制为 nginx.c 文件。ngx_get_options 函数如下:

    
    // 传入的是 main 函数的两个参数 argc 和 argv
    static ngx_int_t
    ngx_get_options(int argc, char *const *argv)
    {
        u_char     *p;
        ngx_int_t   i;
    
        // 对于每一个 argv(注意是从 1 开始,因为 0 是 "nginx")
        for (i = 1; i < argc; i++) {
    
            // p 为第 i 个参数的地址
            p = (u_char *) argv[i];
    
            // 
            if (*p++ != '-') {
                ngx_log_stderr(0, "invalid option: "%s"", argv[i]);
                return NGX_ERROR;
            }
    
            // 之所以 while 循环是因为一个减号可以带过个参数,比如 -hV
            while (*p) {
    
                // 注意 p 被加 1
                switch (*p++) {
    
                // 问号和 h 都是显示帮助信息和版本信息
                case '?':
                case 'h':
                    ngx_show_version = 1;
                    ngx_show_help = 1;
                    break;
    
                // 小 v 显示版本信息
                case 'v':
                    ngx_show_version = 1;
                    break;
    
                // 大 v 显示版本信息和配置信息
                case 'V':
                    ngx_show_version = 1;
                    ngx_show_configure = 1;
                    break;
    
                // t 用于测试配置文件
                case 't':
                    ngx_test_config = 1;
                    break;
    
                // q 表示安静模式
                case 'q':
                    ngx_quiet_mode = 1;
                    break;
    
                // p 为指定 prefix path
                case 'p':
                    if (*p) {
                        ngx_prefix = p;
                        goto next;
                    }
    
                    if (argv[++i]) {
                        ngx_prefix = (u_char *) argv[i];
                        goto next;
                    }
    
                    ngx_log_stderr(0, "option "-p" requires directory name");
                    return NGX_ERROR;
    
                // 使用指定的配置文件
                case 'c':
                    if (*p) {
                        ngx_conf_file = p;
                        goto next;
                    }
    
                    if (argv[++i]) {
                        ngx_conf_file = (u_char *) argv[i];
                        goto next;
                    }
    
                    ngx_log_stderr(0, "option "-c" requires file name");
                    return NGX_ERROR;
    
                // 在配置文件之外设置全局指令
                case 'g':
                    if (*p) {
                        ngx_conf_params = p;
                        goto next;
                    }
    
                    if (argv[++i]) {
                        ngx_conf_params = (u_char *) argv[i];
                        goto next;
                    }
    
                    ngx_log_stderr(0, "option "-g" requires parameter");
                    return NGX_ERROR;
    
                // s 为 signal,即给 Nginx 发送信号
                case 's':
                    if (*p) { // 下一个参数紧跟在 -s 后,比如 -sstop
                        ngx_signal = (char *) p;
    
                    } else if (argv[++i]) { // 下一个参数
                        ngx_signal = argv[i];
    
                    } else { // -s 没有带参数时
                        ngx_log_stderr(0, "option "-s" requires parameter");
                        return NGX_ERROR;
                    }
    
                    // 四个信号分别对应:停止、退出、重新打开文件(日志文件等)、重新加载配置文件
                    if (ngx_strcmp(ngx_signal, "stop") == 0
                        || ngx_strcmp(ngx_signal, "quit") == 0
                        || ngx_strcmp(ngx_signal, "reopen") == 0
                        || ngx_strcmp(ngx_signal, "reload") == 0)
                    {
                        ngx_process = NGX_PROCESS_SIGNALLER;
                        goto next;
                    }
    
                    ngx_log_stderr(0, "invalid option: "-s %s"", ngx_signal);
                    return NGX_ERROR;
    
                default:
                    ngx_log_stderr(0, "invalid option: "%c"", *(p - 1));
                    return NGX_ERROR;
                }
            }
    
        next:
    
            continue;
        }
    
        return NGX_OK;
    }
    

    帮助信息如下:

    Options:
      -?,-h         : this help
      -v            : show version and exit
      -V            : show version and configure options then exit
      -t            : test configuration and exit
      -q            : suppress non-error messages during configuration testing
      -s signal     : send signal to a master process: stop, quit, reopen, reload
      -p prefix     : set prefix path (default: /usr/local/nginx/)
      -c filename   : set configuration file (default: conf/nginx.conf)
      -g directives : set global directives out of configuration file
    

    v 版本信息形式如下:

    nginx version: nginx/1.3.5
    

    V 版本信息如下:

    nginx version: nginx/1.3.5
    built by gcc 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) 
    configure arguments: --with-pcre=/home/michael/packages.d/pcre-8.20 --with-zlib=/home/michael/packages.d/zlib-1.2.7
    
  • 相关阅读:
    Dockerfile构建基础镜像为alpine的jdk1.8镜像
    app测试01_app测试漫谈
    ES基本操作命令
    springboot 前后端日期json格式化
    降维处理PCA
    分析鸢尾花数据集
    Python--matplotlib绘图可视化知识点整理
    模拟股票预期收益案例源代码
    np.random.normal()正态分布
    pandas
  • 原文地址:https://www.cnblogs.com/breg/p/4043610.html
Copyright © 2011-2022 走看看