Nginx源码分析之--auto/sources脚本

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

回顾

上一篇文章我们详细的讲解了auto/init文件,该文件主要是初始化一些文件目录,便于后面的编译过程。
configure执行auto/init之后就会执行auto/sources文件,所以本文分析一下auto/sources文件,这个文件虽然内容很多,但是结构非常简单,全部是初始化操作,为后面的Makefile文件生成各种依赖。比如编译Core模块用到的依赖,编译pcre模块用到的依赖等等。

auto/sources

该文件主要初始化了一下几部分内容:
auto-source一览
下面我们分析一下各个模块的内容。

1、核心模块CORE
模块名

首先定义了一个CORE_MODULES变量,这个变量表示Nginx的核心模块,从定义中可以看出来,它包括了ngx_core_module, ngx_errlog_module,以及ngx_conf_module这三个模块。

CORE_MODULES="ngx_core_module ngx_errlog_module ngx_conf_module"
头文件

定义了CORE_INCS表示核心模块的头文件所在目录。

CORE_INCS="src/core"
依赖文件

定义了一个CORE_DEPS(depandencies:依赖)变量表示核心模块的依赖文件。编译核心模块的时候要使用到这些依赖文件。

CORE_DEPS="src/core/nginx.h \
           src/core/ngx_config.h \
           src/core/ngx_core.h \
           src/core/ngx_log.h \
           src/core/ngx_palloc.h \
           src/core/ngx_array.h \
           src/core/ngx_list.h \
           src/core/ngx_hash.h \
           src/core/ngx_buf.h \
           src/core/ngx_queue.h \
           src/core/ngx_string.h \
           src/core/ngx_parse.h \
           src/core/ngx_inet.h \
           src/core/ngx_file.h \
           src/core/ngx_crc.h \
           src/core/ngx_crc32.h \
           src/core/ngx_murmurhash.h \
           src/core/ngx_md5.h \
           src/core/ngx_sha1.h \
           src/core/ngx_rbtree.h \
           src/core/ngx_radix_tree.h \
           src/core/ngx_slab.h \
           src/core/ngx_times.h \
           src/core/ngx_shmtx.h \
           src/core/ngx_connection.h \
           src/core/ngx_cycle.h \
           src/core/ngx_conf_file.h \
           src/core/ngx_resolver.h \
           src/core/ngx_open_file_cache.h \
           src/core/ngx_crypt.h"
核心模块的源文件

定义了CORE_SRCS变量表示核心模块的源文件路径,这些源文件就是实现核心模块的代码。

CORE_SRCS="src/core/nginx.c \
           src/core/ngx_log.c \
           src/core/ngx_palloc.c \
           src/core/ngx_array.c \
           src/core/ngx_list.c \
           src/core/ngx_hash.c \
           src/core/ngx_buf.c \
           src/core/ngx_queue.c \
           src/core/ngx_output_chain.c \
           src/core/ngx_string.c \
           src/core/ngx_parse.c \
           src/core/ngx_inet.c \
           src/core/ngx_file.c \
           src/core/ngx_crc32.c \
           src/core/ngx_murmurhash.c \
           src/core/ngx_md5.c \
           src/core/ngx_rbtree.c \
           src/core/ngx_radix_tree.c \
           src/core/ngx_slab.c \
           src/core/ngx_times.c \
           src/core/ngx_shmtx.c \
           src/core/ngx_connection.c \
           src/core/ngx_cycle.c \
           src/core/ngx_spinlock.c \
           src/core/ngx_cpuinfo.c \
           src/core/ngx_conf_file.c \
           src/core/ngx_resolver.c \
           src/core/ngx_open_file_cache.c \
           src/core/ngx_crypt.c"
2、正则表达式模块

正则表达式(PCRE)模块是Nginx实现rewrite模块的关键,在auto/sources中定义了依赖文件和源文件两个变量来编译正则表达式。

REGEX_DEPS=src/core/ngx_regex.h
REGEX_SRCS=src/core/ngx_regex.c
3、OpenSSL模块

这个模块是ssl加密模块,用来实现HTTPS加密。定义了三个变量来编译该模块,语法同CORE模块。

OPENSSL_MODULE=ngx_openssl_module
OPENSSL_DEPS=src/event/ngx_event_openssl.h
OPENSSL_SRCS=src/event/ngx_event_openssl.c
4、事件模块

事件模块是nginx非常重要的模块,我们都知道nginx的高并发就是因为使用了优秀的I/O模块。nginx为了可移植性,定义了一个事件框架的模型,在不同的平台上面可以使用不同的事件处理机制,通常在Linux环境下,我们都是用epoll事件机制。
同样的,auto/sources定义了几个变量分别表示事件模块的模块名,头文件,依赖文件和源码文件,可以参考CORE_MODULE的分析。

EVENT_MODULES="ngx_events_module ngx_event_core_module"

EVENT_INCS="src/event src/event/modules"

EVENT_DEPS="src/event/ngx_event.h \
            src/event/ngx_event_timer.h \
            src/event/ngx_event_posted.h \
            src/event/ngx_event_busy_lock.h \
            src/event/ngx_event_connect.h \
            src/event/ngx_event_pipe.h"

EVENT_SRCS="src/event/ngx_event.c \
            src/event/ngx_event_timer.c \
            src/event/ngx_event_posted.c \
            src/event/ngx_event_busy_lock.c \
            src/event/ngx_event_accept.c \
            src/event/ngx_event_connect.c \
            src/event/ngx_event_pipe.c"
5、事件驱动模块

在上面的第4步中,我们提到了为了在多个平台上运行,Nginx实现了一套事件机制,这样用户就可以根据当前的平台选择不同的事件处理方法了。比如在Mac上面可以使用kqueue,在linux上面可以使用select,epoll等。Nginx自带了很多种事件处理模块的实现,我们可以根据自己的需求选用不用的模块。
每种模块的定义方式都是相同的。

#select 模块
SELECT_MODULE=ngx_select_module
SELECT_SRCS=src/event/modules/ngx_select_module.c
WIN32_SELECT_SRCS=src/event/modules/ngx_win32_select_module.c

### poll模块
POLL_MODULE=ngx_poll_module
POLL_SRCS=src/event/modules/ngx_poll_module.c

# kqueue模块
KQUEUE_MODULE=ngx_kqueue_module
KQUEUE_SRCS=src/event/modules/ngx_kqueue_module.c

### devpoll模块
DEVPOLL_MODULE=ngx_devpoll_module
DEVPOLL_SRCS=src/event/modules/ngx_devpoll_module.c

EVENTPORT_MODULE=ngx_eventport_module
EVENTPORT_SRCS=src/event/modules/ngx_eventport_module.c

EPOLL_MODULE=ngx_epoll_module
EPOLL_SRCS=src/event/modules/ngx_epoll_module.c

RTSIG_MODULE=ngx_rtsig_module
RTSIG_SRCS=src/event/modules/ngx_rtsig_module.c

# windows平台使用iocp模块
IOCP_MODULE=ngx_iocp_module
IOCP_SRCS=src/event/modules/ngx_iocp_module.c

### aio模块,这是linux平台的一种文件异步处理机制
AIO_MODULE=ngx_aio_module
AIO_SRCS="src/event/modules/ngx_aio_module.c \
          src/os/unix/ngx_aio_read.c \
          src/os/unix/ngx_aio_write.c \
          src/os/unix/ngx_aio_read_chain.c \
          src/os/unix/ngx_aio_write_chain.c"

FILE_AIO_SRCS="src/os/unix/ngx_file_aio_read.c"
LINUX_AIO_SRCS="src/os/unix/ngx_linux_aio_read.c"
6、操作系统相关模块

对于不同的平台,nginx都实现了一种平台特定代码,用于最底层的数据传输等。我们只看unix/linux相关的部分,其余的平台也是一样的。其实也没啥要说的哦,就是定义了一些依赖文件,头文件,源文件的目录。

UNIX_INCS="$CORE_INCS $EVENT_INCS src/os/unix"

UNIX_DEPS="$CORE_DEPS $EVENT_DEPS \
            src/os/unix/ngx_time.h \
            src/os/unix/ngx_errno.h \
            src/os/unix/ngx_alloc.h \
            src/os/unix/ngx_files.h \
            src/os/unix/ngx_channel.h \
            src/os/unix/ngx_shmem.h \
            src/os/unix/ngx_process.h \
            src/os/unix/ngx_setproctitle.h \
            src/os/unix/ngx_atomic.h \
            src/os/unix/ngx_gcc_atomic_x86.h \
            src/os/unix/ngx_thread.h \
            src/os/unix/ngx_socket.h \
            src/os/unix/ngx_os.h \
            src/os/unix/ngx_user.h \
            src/os/unix/ngx_process_cycle.h"

# add to UNIX_DEPS
#            src/os/unix/ngx_gcc_atomic_amd64.h \
#            src/os/unix/ngx_gcc_atomic_sparc64.h \
#            src/os/unix/ngx_gcc_atomic_ppc.h \
#            src/os/unix/ngx_sunpro_atomic_sparc64.h \
#            src/os/unix/ngx_sunpro_x86.il \
#            src/os/unix/ngx_sunpro_amd64.il \
#            src/os/unix/ngx_sunpro_sparc64.il \


UNIX_SRCS="$CORE_SRCS $EVENT_SRCS \
            src/os/unix/ngx_time.c \
            src/os/unix/ngx_errno.c \
            src/os/unix/ngx_alloc.c \
            src/os/unix/ngx_files.c \
            src/os/unix/ngx_socket.c \
            src/os/unix/ngx_recv.c \
            src/os/unix/ngx_readv_chain.c \
            src/os/unix/ngx_udp_recv.c \
            src/os/unix/ngx_send.c \
            src/os/unix/ngx_writev_chain.c \
            src/os/unix/ngx_channel.c \
            src/os/unix/ngx_shmem.c \
            src/os/unix/ngx_process.c \
            src/os/unix/ngx_daemon.c \
            src/os/unix/ngx_setproctitle.c \
            src/os/unix/ngx_posix_init.c \
            src/os/unix/ngx_user.c \
            src/os/unix/ngx_process_cycle.c"

linux相关的内容如下:

LINUX_DEPS="src/os/unix/ngx_linux_config.h src/os/unix/ngx_linux.h"
LINUX_SRCS=src/os/unix/ngx_linux_init.c
LINUX_SENDFILE_SRCS=src/os/unix/ngx_linux_sendfile_chain.c

7、HTTP模块

下面的重头戏来了,开始定义HTTP相关的内容了。

基础的模块

定义了一些基础的模块,是必须要编译到nginx中的内容,包括ngx_http_modulengx_http_core_modulengx_http_log_modulengx_http_upstream_module,如下:

# the http modules that have their logging formats
# must be after ngx_http_log_module

HTTP_MODULES="ngx_http_module \
              ngx_http_core_module \
              ngx_http_log_module \
              ngx_http_upstream_module"

以及HTTP模块的头文件,依赖文件和源代码文件路径,如下:

HTTP_INCS="src/http src/http/modules"

HTTP_DEPS="src/http/ngx_http.h \
           src/http/ngx_http_request.h \
           src/http/ngx_http_config.h \
           src/http/ngx_http_core_module.h \
           src/http/ngx_http_cache.h \
           src/http/ngx_http_variables.h \
           src/http/ngx_http_script.h \
           src/http/ngx_http_upstream.h \
           src/http/ngx_http_upstream_round_robin.h \
           src/http/ngx_http_busy_lock.h"

HTTP_SRCS="src/http/ngx_http.c \
           src/http/ngx_http_core_module.c \
           src/http/ngx_http_special_response.c \
           src/http/ngx_http_request.c \
           src/http/ngx_http_parse.c \
           src/http/ngx_http_header_filter_module.c \
           src/http/ngx_http_write_filter_module.c \
           src/http/ngx_http_copy_filter_module.c \
           src/http/modules/ngx_http_log_module.c \
           src/http/ngx_http_request_body.c \
           src/http/ngx_http_variables.c \
           src/http/ngx_http_script.c \
           src/http/ngx_http_upstream.c \
           src/http/ngx_http_upstream_round_robin.c \
           src/http/ngx_http_parse_time.c \
           src/http/modules/ngx_http_static_module.c \
           src/http/modules/ngx_http_index_module.c \
           src/http/modules/ngx_http_chunked_filter_module.c \
           src/http/modules/ngx_http_range_filter_module.c \
           src/http/modules/ngx_http_headers_filter_module.c \
           src/http/modules/ngx_http_not_modified_filter_module.c"
自定义模块

剩下的是一些自定义模块,我们在configure的时候可以选择是否编译,如果不编译,那么在生成的Makefile中就不会生成相应的代码,我们随便找一个分析一下:

HTTP_REALIP_MODULE=ngx_http_realip_module
HTTP_REALIP_SRCS=src/http/modules/ngx_http_realip_module.c

上面的模块是获取客户端真实ip的模块,如果我们在configure的时候选择编译该模块,那么生成Makefile的时候就会将相应的源码和依赖包含进去。

8、邮件模块

这部分忽略,因为我从来没用过nginx当邮件服务器。
但是它的格式和其他模块一模一样,可以参考上面的东东自行分析该部分。

9、Google Perftool

Google Perftool它是由google开发的用来分析C/C++程序性能的一套工具,这里的性能分析主要包括内存和CPU两个方面,内存分析使用google-perftool所提供的tcmallocCPU分析使用它所提供的profiler
这里简单了解一下就行了,和nginx的实现无关。

NGX_GOOGLE_PERFTOOLS_MODULE=ngx_google_perftools_module
NGX_GOOGLE_PERFTOOLS_SRCS=src/misc/ngx_google_perftools_module.c
10、C++测试模块

这个就是一个测试模块,测试是否支持C++内嵌C语法,没什么作用(至少我没发现有啥作用)。

下篇内容

一大篇文章,详细的分析了auto/sources模块的功能,希望对你有所帮助。请继续关注后续文章。

<think>好的,我现在需要帮助用户分析他们运行`sudo find / -name "nginx.conf" 2>/dev/null`后得到的多个nginx.conf文件路径。用户之前的问题是关于如何确定正确的Nginx配置文件位置,现在他们提供了查找结果,里面有几个可能的路径,需要我解释这些结果并指导他们确定正确的配置文件。 首先,我要理解用户的需求。他们可能在配置Nginx时遇到问题,或者需要确认哪个配置文件是当前正在使用的。用户之前尝试过用`nginx -V`来查找配置路径,但没有成功,现在通过find命令找到了多个路径,需要帮助筛选。 接下来,我需要分析用户提供的路径。给出的路径有: 1. `/root/nginx/nginx-rtmp-module-1.2.2/test/nginx.conf` 2. `/root/nginx/nginx-1.25.5/conf/nginx.conf` 3. `/usr/local/nginx/conf/nginx.conf` 4. `/www/server/nginx/nginx-rtmp-module/test/nginx.conf` 5. `/www/server/nginx/conf/nginx.conf` 6. `/www/backup/file_history/www/server/nginx/conf/nginx.conf` 首先,我需要排除测试或备份文件。比如路径中包含`test`或`backup`的可能是测试用例或备份文件,不太可能是当前使用的配置。例如,路径1和4中的`test`目录下的nginx.conf,通常是模块的测试配置文件,而不是实际使用的。路径6看起来像是备份文件,因为路径中有`backup/file_history`。 剩下的可能性较大的路径是: - `/root/nginx/nginx-1.25.5/conf/nginx.conf`:这可能是在用户自己编译安装Nginx时生成的配置文件,尤其是如果用户手动下载并编译了Nginx 1.25.5版本,这个路径可能对应源码安装的位置。 - `/usr/local/nginx/conf/nginx.conf`:这也是常见的源码安装的默认路径,通常当用户通过源码编译安装且没有指定其他路径时,配置文件会在这里。 - `/www/server/nginx/conf/nginx.conf`:这个路径看起来像是通过某些第三方脚本或面板(如宝塔面板)安装的Nginx的配置路径,很多控制面板会自定义安装目录到`/www/server`下。 接下来,用户需要确定哪个是当前正在使用的配置文件。我需要建议用户使用之前提到的`nginx -t`命令来测试配置,这会显示实际加载的配置文件路径。如果用户运行`nginx -t`,输出中会显示正在使用的配置文件,例如“configuration file /etc/nginx/nginx.conf syntax is ok”,这样就能明确知道正确的路径。 如果用户没有权限运行`nginx -t`,可能需要使用`sudo`。此外,检查Nginx进程的启动参数也是一个方法,通过`ps aux | grep nginx`查看主进程是否带有`-c`参数指定配置文件,例如`-c /www/server/nginx/conf/nginx.conf`,这样可以直接确认。 另外,用户可能安装了多个Nginx实例,比如通过不同方式安装的,这时候需要确认当前运行的Nginx二进制文件的位置,使用`which nginx`或者`ps aux | grep nginx`找到二进制路径,再根据该路径推断配置文件的默认位置。 最后,需要提醒用户,如果存在多个配置文件,修改错误的文件不会生效,因此确认正确的路径非常重要。建议用户优先检查`/www/server/nginx/conf/nginx.conf`,因为第三方面板常用此路径,同时结合`nginx -t`的输出来验证。</think>根据你提供的 `find` 命令结果,系统中存在 **多个 `nginx.conf` 文件**,这表明可能存在以下情况: --- ### **路径分析** 1. **测试/备份文件(可忽略)** - `/root/nginx/nginx-rtmp-module-1.2.2/test/nginx.conf` - `/www/server/nginx/nginx-rtmp-module/test/nginx.conf` - `/www/backup/file_history/www/server/nginx/conf/nginx.conf` **说明**:这些是 **Nginx RTMP 模块的测试配置文件** 或 **历史备份文件**,与当前运行中的 Nginx 无关。 2. **源码编译安装路径** - `/root/nginx/nginx-1.25.5/conf/nginx.conf` - `/usr/local/nginx/conf/nginx.conf` **说明**:可能是你通过源码编译安装 Nginx 时生成的默认配置文件,但若未实际运行此版本,则非当前生效配置。 3. **重点怀疑路径** - `/www/server/nginx/conf/nginx.conf` **说明**:此路径常见于通过 **第三方脚本(如宝塔面板、LNMP一键包)** 安装的 Nginx,极有可能是当前正在使用的配置文件。 --- ### **如何确认实际生效的配置文件?** #### **方法一:通过 `nginx -t` 验证配置** 运行命令查看 Nginx 实际加载的配置文件路径: ```bash sudo nginx -t ``` 输出示例: ``` nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied) nginx: the configuration file **/www/server/nginx/conf/nginx.conf** syntax is ok nginx: configuration file /www/server/nginx/conf/nginx.conf test is successful ``` **第一行**会显示当前配置文件路径(即使权限错误也会输出路径)。 #### **方法二:查看 Nginx 进程参数** 运行命令检查进程启动时指定的配置文件: ```bash ps aux | grep nginx | grep master ``` 输出示例: ``` root 1234 0.0 0.1 12345 6789 ? Ss 12:34 0:00 nginx: master process **/www/server/nginx/sbin/nginx -c /www/server/nginx/conf/nginx.conf** ``` 若存在 `-c` 参数,则直接显示配置文件路径。 --- ### **最终结论** 根据路径特征和常见安装方式,**当前生效的配置文件应为 `/www/server/nginx/conf/nginx.conf`**。建议按以下步骤操作: 1. **验证配置语法**: ```bash sudo nginx -t -c /www/server/nginx/conf/nginx.conf ``` 2. **查看配置关联文件**: 配置文件通常包含 `include` 指令加载其他配置(如虚拟主机),例如: ```nginx include /www/server/nginx/conf/vhost/*.conf; ``` 3. **若需修改配置**: 直接编辑 `/www/server/nginx/conf/nginx.conf`,修改后执行 `sudo nginx -s reload` 重载配置。 --- ### **其他注意事项** - **多版本共存**:若系统中存在多个 Nginx 实例,需通过 `which nginx` 确认当前运行的二进制文件路径,再结合 `ps aux` 进一步分析。 - **权限问题**:操作配置文件时可能需要 `sudo` 提权。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值