nginx启动 -- 错误日志相关

微信公众号:Nginx源码分析
关注可了解更多的Nginx知识。任何问题或建议,请公众号留言;
关注公众号,有趣有内涵的文章第一时间送达!

本节内容

从本节开始,我们开始真正的分析nginx源码。我并没有一个准确的先后顺序,想到哪就写到哪里。
当然,有很多地方我也不懂代码的含义,这些地方我会表明,然后先略过去。

启动

main函数在src/core/nginx.c文件中。
我们首先看一下nginx启动的部分,启动部分有很多函数,我们逐个的分析一下每个函数的作用。

ngx_debug_init

这个函数是设置debug相关功能的,在linux环境下,只是一个空的宏定义,可以略过。

ngx_strerror_init

这部分代码是在src/os/unix/ngx_errno.c文件中的,主要用于错误信息相关的输出。
这个文件包含了两个函数ngx_strerror_initngx_strerror,我们先看一下ngx_strerror_init函数,顾名思义,这是一个初始化函数。

代码
ngx_int_t
ngx_strerror_init(void)
{
    char       *msg;
    u_char     *p;
    size_t      len;
    ngx_err_t   err;
    // int i 是测试代码
    int i ;

    /*
     * ngx_strerror() is not ready to work at this stage, therefore,
     * malloc() is used and possible errors are logged using strerror().
     */

    len = NGX_SYS_NERR * sizeof(ngx_str_t);

    ngx_sys_errlist = malloc(len);
    if (ngx_sys_errlist == NULL) {
        goto failed;
    }

    for (err = 0; err < NGX_SYS_NERR; err++) {
        msg = strerror(err);
        len = ngx_strlen(msg);

        p = malloc(len);
        if (p == NULL) {
            goto failed;
        }

        ngx_memcpy(p, msg, len);
        ngx_sys_errlist[err].len = len;
        ngx_sys_errlist[err].data = p;
    }
// 下面的for循环是我们添加的测试代码    
    for(i = 0; i < NGX_SYS_NERR; i++){
       printf("errno is %d, msg is %s\n", i, ngx_sys_errlist[i].data);
    }

    return NGX_OK;

failed:

    err = errno;
    ngx_log_stderr(0, "malloc(%uz) failed (%d: %s)", len, err, strerror(err));

    return NGX_ERROR;
}

这里面用到了一个变量NGX_SYS_NERR,这是一个宏定义,是在auto/unix脚本中设置的,如下:

ngx_feature="sys_nerr"
ngx_feature_name="NGX_SYS_NERR"
ngx_feature_run=value
ngx_feature_incs='#include <errno.h>
                  #include <stdio.h>'
ngx_feature_path=
ngx_feature_libs=
ngx_feature_test='printf("%d", sys_nerr);'
. auto/feature

这段脚本通过获取sys_nerr参数的值,然后赋值给NGX_SYS_NERR,这个参数表示的是当前操作系统提供的最大的错误码数值。

我们在ngx_strerror_init中添加了一段测试代码,编译之后,启动nginx可以看到ngx_sys_errlist数组元素的值。

errno is 0, msg is Success
errno is 1, msg is Operation not permitted
errno is 2, msg is No such file or directory
errno is 3, msg is No such process
errno is 4, msg is Interrupted system call
errno is 5, msg is Input/output error
errno is 6, msg is No such device or address
errno is 7, msg is Argument list too long
errno is 8, msg is Exec format error
errno is 9, msg is Bad file descriptor
errno is 10, msg is No child processes
errno is 11, msg is Resource temporarily unavailable
errno is 12, msg is Cannot allocate memory
errno is 13, msg is Permission denied
errno is 14, msg is Bad address
errno is 15, msg is Block device required
errno is 16, msg is Device or resource busy
errno is 17, msg is File exists
errno is 18, msg is Invalid cross-device link
errno is 19, msg is No such device
errno is 20, msg is Not a directory
errno is 21, msg is Is a directory
errno is 22, msg is Invalid argument
errno is 23, msg is Too many open files in system
errno is 24, msg is Too many open files
errno is 25, msg is Inappropriate ioctl for device
errno is 26, msg is Text file busy
errno is 27, msg is File too large
errno is 28, msg is No space left on device
errno is 29, msg is Illegal seek
errno is 30, msg is Read-only file system
errno is 31, msg is Too many links
errno is 32, msg is Broken pipe
errno is 33, msg is Numerical argument out of domain
errno is 34, msg is Numerical result out of range
errno is 35, msg is Resource deadlock avoided
errno is 36, msg is File name too long
errno is 37, msg is No locks available
errno is 38, msg is Function not implemented!
errno is 39, msg is Directory not empty
errno is 40, msg is Too many levels of symbolic links
errno is 41, msg is Unknown error 41
errno is 42, msg is No message of desired type
errno is 43, msg is Identifier removed
errno is 44, msg is Channel number out of range
errno is 45, msg is Level 2 not synchronized!
errno is 46, msg is Level 3 halted
errno is 47, msg is Level 3 reset
errno is 48, msg is Link number out of range1
errno is 49, msg is Protocol driver not attached
errno is 50, msg is No CSI structure available
errno is 51, msg is Level 2 halted
errno is 52, msg is Invalid exchange
errno is 53, msg is Invalid request descriptor
errno is 54, msg is Exchange full
errno is 55, msg is No anode
errno is 56, msg is Invalid request code
errno is 57, msg is Invalid slot
errno is 58, msg is Unknown error 58
errno is 59, msg is Bad font file format
errno is 60, msg is Device not a stream
errno is 61, msg is No data available
errno is 62, msg is Timer expired
errno is 63, msg is Out of streams resources1
errno is 64, msg is Machine is not on the network
errno is 65, msg is Package not installed
errno is 66, msg is Object is remote
errno is 67, msg is Link has been severed
errno is 68, msg is Advertise error
errno is 69, msg is Srmount error
errno is 70, msg is Communication error on send
errno is 71, msg is Protocol error
errno is 72, msg is Multihop attempted
errno is 73, msg is RFS specific error
errno is 74, msg is Bad message
errno is 75, msg is Value too large for defined data type
errno is 76, msg is Name not unique on network
errno is 77, msg is File descriptor in bad state
errno is 78, msg is Remote address changed
errno is 79, msg is Can not access a needed shared library
errno is 80, msg is Accessing a corrupted shared library
errno is 81, msg is .lib section in a.out corrupted
errno is 82, msg is Attempting to link in too many shared libraries
errno is 83, msg is Cannot exec a shared library directly
errno is 84, msg is Invalid or incomplete multibyte or wide character
errno is 85, msg is Interrupted system call should be restarted
errno is 86, msg is Streams pipe error
errno is 87, msg is Too many users
errno is 88, msg is Socket operation on non-socket
errno is 89, msg is Destination address required
errno is 90, msg is Message too long
errno is 91, msg is Protocol wrong type for socket
errno is 92, msg is Protocol not available
errno is 93, msg is Protocol not supported
errno is 94, msg is Socket type not supported
errno is 95, msg is Operation not supported
errno is 96, msg is Protocol family not supported
errno is 97, msg is Address family not supported by protocol!
errno is 98, msg is Address already in use
errno is 99, msg is Cannot assign requested address
errno is 100, msg is Network is down
errno is 101, msg is Network is unreachable
errno is 102, msg is Network dropped connection on reset
errno is 103, msg is Software caused connection abort
errno is 104, msg is Connection reset by peer1
errno is 105, msg is No buffer space available
errno is 106, msg is Transport endpoint is already connected
errno is 107, msg is Transport endpoint is not connected
errno is 108, msg is Cannot send after transport endpoint shutdown
errno is 109, msg is Too many references: cannot splice
errno is 110, msg is Connection timed out
errno is 111, msg is Connection refused
errno is 112, msg is Host is down
errno is 113, msg is No route to host
errno is 114, msg is Operation already in progress
errno is 115, msg is Operation now in progress
errno is 116, msg is Stale file handle
errno is 117, msg is Structure needs cleaning1
errno is 118, msg is Not a XENIX named type file
errno is 119, msg is No XENIX semaphores available
errno is 120, msg is Is a named type file
errno is 121, msg is Remote I/O error
errno is 122, msg is Disk quota exceeded
errno is 123, msg is No medium found
errno is 124, msg is Wrong medium type
errno is 125, msg is Operation canceled
errno is 126, msg is Required key not available
errno is 127, msg is Key has expired
errno is 128, msg is Key has been revoked
errno is 129, msg is Key was rejected by service
errno is 130, msg is Owner died
errno is 131, msg is State not recoverable
errno is 132, msg is Operation not possible due to RF-kill
errno is 133, msg is Memory page has hardware error
errno is 134, msg is Unknown error 134

ngx_strerror

这个函数是获取上面的ngx_sys_errlist数组的内容,函数非常简单

u_char *
ngx_strerror(ngx_err_t err, u_char *errstr, size_t size)
{
    ngx_str_t  *msg;

    msg = ((ngx_uint_t) err < NGX_SYS_NERR) ? &ngx_sys_errlist[err]:
                                              &ngx_unknown_error;
    size = ngx_min(size, msg->len);

    return ngx_cpymem(errstr, msg->data, size);
}

三个参数的含义如下:
err : 错误的数值no
errstr : 保存错误信息的字符串指针
size : 保存错误信息的内存空间长度

总结

本文就先简单的分析一下启动过程中的错误信息的处理方法。如果喜欢本文,可以关注公众号Nginx源码分析.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Windows 7上启动Nginx-1.17.9,您可以按照以下步骤进行操作: 第一步,下载并安装Nginx-1.17.9的Windows版本。您可以从Nginx官方网站(http://nginx.org)或其他可靠的第三方网站上下载该软件。 第二步,解压缩下载的文件。将压缩包解压到您计划安装Nginx的目录中。您可以选择将其解压缩到C:\目录或其他您指定的目录中。 第三步,配置Nginx。在解压缩后的Nginx文件夹中,找到nginx.conf文件,并使用文本编辑器打开它。在该文件中,您可以配置Nginx以满足您的特定需求,例如监听端口,设置虚拟主机等。如果您对Nginx的配置不熟悉,您可以保留默认配置,并稍后再进行更改。 第四步,启动Nginx。在解压缩后的Nginx文件夹中,找到nginx.exe文件,并双击运行它。这将启动Nginx服务器,并开始监听配置文件中设置的端口。 第五步,验证Nginx是否成功启动。打开您的Web浏览器,并在地址栏中输入http://localhost:80(或您在配置文件中设置的监听端口)。如果您看到Nginx的欢迎页面,说明Nginx已经成功启动,并可以通过该端口提供Web服务。 请注意,如果您遇到启动Nginx时的问题,您可以在Nginx所在目录的logs文件夹中查找错误日志。该日志文件将记录任何启动过程中的错误,并帮助您进行故障排除。 希望以上步骤对您有所帮助,祝您成功启动Windows 7上的Nginx-1.17.9!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值