ngx_add_inherited_sockets函数分析

ngx_add_inherited_sockets开始通过getenv获取"NGINX"环境变量,如果是NULL,直接return,通过增加log发现,正常这个值确实是NULL。

NGINX_VAR环境变量的设置是在平滑升级的时候才设置,nginx所采用的平滑升级能够保证在不中止服务的状况下更新nginx版本,具体需要看下ngx_exec_new_binary函数的实现。

所以ngx_add_inherited_sockets一般情况下用处不大,除非是需要升级nginx。

static ngx_int_t
ngx_add_inherited_sockets(ngx_cycle_t *cycle) {
    u_char *p, *v, *inherited;
    ngx_int_t s;
    ngx_listening_t *ls;

    inherited = (u_char *)getenv(NGINX_VAR);

    if (inherited == NULL) {
        return NGX_OK;
    }

    ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
                  "using inherited sockets from \"%s\"", inherited);
    if (ngx_array_init(&cycle->listening, cycle->pool, 10,
                       sizeof(ngx_listening_t)) != NGX_OK) {
        return NGX_ERROR;
    }

    for (p = inherited, v = p; *p; p++) {
        if (*p == ':' || *p == ';') {
            s = ngx_atoi(v, p - v);
            if (s == NGX_ERROR) {
                ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
                              "invalid socket number \"%s\" in " NGINX_VAR
                              " environment variable, ignoring the rest"
                              " of the variable",
                              v);
                break;
            }

            v = p + 1;

            ls = ngx_array_push(&cycle->listening);
            if (ls == NULL) {
                return NGX_ERROR;
            }

            ngx_memzero(ls, sizeof(ngx_listening_t));

            ls->fd = (ngx_socket_t)s;
        }
    }

    if (v != p) {
        ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
                      "invalid socket number \"%s\" in " NGINX_VAR
                      " environment variable, ignoring",
                      v);
    }

    ngx_inherited = 1;

    return ngx_set_inherited_sockets(cycle);
}

ps找到nginx的进程,然后看nginx的environ也没有什么信息

$ ps -ef | grep nginx
root      4993  2764  0 12:01 ?        00:00:00 nginx: master process ./sbin/nginx
hui       5007  4993  0 12:01 ?        00:00:00 nginx: worker process

$ sudo cat /proc/4993/environ 
 process ./sbin/nginx

$ cat /proc/5007/environ 
 process

附:nginx中输出log的办法

在nginx的main函数中,每个函数调用前增加log后,在logs/error.log文件中,只能输出到ngx_log_redirect_stderr,因为在这之后的log都被redirect了。

$ sudo cat logs/error.log 
2021/09/28 11:23:27 [notice] 24632#0: ungx_save_argv
2021/09/28 11:23:27 [notice] 24632#0: ngx_process_options
2021/09/28 11:23:27 [notice] 24632#0: ngx_os_init
2021/09/28 11:23:27 [notice] 24632#0: ngx_crc32_table_init
2021/09/28 11:23:27 [notice] 24632#0: ngx_slab_sizes_init
2021/09/28 11:23:27 [notice] 24632#0: ngx_add_inherited_sockets
2021/09/28 11:23:27 [notice] 24632#0: ngx_preinit_modules
2021/09/28 11:23:27 [notice] 24632#0: ngx_init_cycle
2021/09/28 11:23:27 [notice] 24633#0: ngx_log_redirect_stderr

所以要输出全部log,需要在nginx.conf文件中配置log输出路径到/home/hui/disk4t/media-test/samples/log.txt,后面的debug是对应的log level。

worker_processes  1;
error_log /home/hui/disk4t/media-test/samples/log.txt debug;

nginx中的log level有8级

#define NGX_LOG_STDERR            0
#define NGX_LOG_EMERG             1
#define NGX_LOG_ALERT             2
#define NGX_LOG_CRIT              3
#define NGX_LOG_ERR               4
#define NGX_LOG_WARN              5
#define NGX_LOG_NOTICE            6
#define NGX_LOG_INFO              7
#define NGX_LOG_DEBUG             8

如果没有在configure的时候带上debug选项,输出的log是很少的,比如启动nginx,就只有下面这几行log

2021/09/28 11:28:32 [notice] 29022#0: using the "epoll" event method
2021/09/28 11:28:32 [notice] 29022#0: nginx/1.18.0
2021/09/28 11:28:32 [notice] 29022#0: built by gcc 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)
2021/09/28 11:28:32 [notice] 29022#0: OS: Linux 5.4.0-81-generic
2021/09/28 11:28:32 [notice] 29022#0: getrlimit(RLIMIT_NOFILE): 1024:4096
2021/09/28 11:28:32 [notice] 29023#0: start worker processes
2021/09/28 11:28:32 [notice] 29023#0: start worker process 29024

加上--with-debug选项后,输出log就很多了:

./configure --prefix=/usr/local/nginx  --with-debug --with-http_ssl_module  --add-module=../nginx-rtmp-module  --with-http_flv_module --with-http_mp4_module
$ tail /home/hui/disk4t/media-test/samples/log.txt
2021/09/28 12:38:53 [debug] 5007#0: *4 free: 000056092AC69560, unused: 0
2021/09/28 12:38:53 [debug] 5007#0: *4 free: 000056092ACADD60, unused: 2772
2021/09/28 12:38:53 [debug] 5007#0: *4 free: 000056092AC3B320
2021/09/28 12:38:53 [debug] 5007#0: *4 hc free: 0000000000000000
2021/09/28 12:38:53 [debug] 5007#0: *4 hc busy: 0000000000000000 0
2021/09/28 12:38:53 [debug] 5007#0: *4 reusable connection: 1
2021/09/28 12:38:53 [debug] 5007#0: *4 event timer add: 3: 65000:962096260
2021/09/28 12:38:53 [debug] 5007#0: timer delta: 15016
2021/09/28 12:38:53 [debug] 5007#0: worker cycle
2021/09/28 12:38:53 [debug] 5007#0: epoll timer: 65000

参考

-Nginx启动流程之ngx_add_inherited_sockets

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值