nginx的configure流程


configure配置

nginx的編譯過程,第一步是configure。我們使用 --help可以看到configure的很多配置。

configure的過程做的事情其實就是檢測環境,然后根據環境生成Makefile,包含各種參數的c頭文件等(ngx_auto_config.h/ ngx_auto_headers.h)。這個c頭文件包含了所有根據當前環境配置的參數。

configure命令 之后多了些什么?

多了一個Makefile文件

這里的Makefile文件指的是根目錄下的Makefile, 這個是后面make命令的入口

多了一個objs文件夾

objs文件夾里面有這幾個文件:

autoconf.err

configure過程中不僅僅是使用簡單的shell命令來檢測環境,還會生成一些簡單的c程序,並且用編譯器編譯執行,獲取輸出來獲取環境參數。只是這些c程序和編譯后的文件在獲取參數完成之后會刪除,所以我們實際看不到這個文件的存在。

這些shell命令,c程序的一些輸出和錯誤信息都記錄在這個autoconf.err中了,實際上這個文件沒有什么用。

Makefile

這個obj/Makefile才是本質的構建程序和模塊的過程。所有需要加載的模塊和一些設置都在這邊了。

我們可以看一下,它使用的編譯器是cc, cc編譯器在linux上就是gcc。 gcc是一個各種不同語言的編譯器。gcc代表 the GNU Compiler Collection。 比如你的代碼后綴是.c, 它會調用c的編譯器還有linker去鏈接c的庫。

nginx_auto_config.h

這個就是configure出來的一個重要成果,本機環境的一些配置,比如int多少位之類的。

nginx_auto_headers.h

這個是configure出來的,判斷一些header是否存在。

nginx_modules.c

這個文件就告訴我們了我們這次編譯nginx到底有多少個模塊。

其中的一些模塊是很重要的,但是我們也可以自主選擇。

configure的語法

have + auto/define 語法

have=NGX_SBIN_PATH value="\"$NGX_SBIN_PATH\"" . auto/define

auto/define的定義:

# Copyright (C) Igor Sysoev
# Copyright (C) Nginx, Inc.


cat << END >> $NGX_AUTO_CONFIG_H

#ifndef $have
#define $have  $value
#endif

END

結合起來的意思就是,如果我在參數里面設置了NGX_SBIN_PATH(有參數可以設置),那么我就在ngx_auto_config.h中增加這個宏定義:

#ifndef NGX_SBIN_PATH
#define NGX_SBIN_PATH  "sbin/nginx"
#endif

have + auto/have 語法

cat << END >> $NGX_AUTO_CONFIG_H

#ifndef $have
#define $have  1
#endif

END

意思其實就是auto/define的翻版,只是如果這里的value是1

比如

have=NGX_HAVE_EPOLL . auto/have

等同

#ifndef NGX_HAVE_EPOLL
#define NGX_HAVE_EPOLL  1
#endif

auto/feature

ngx_feature="GeoIP IPv6 support"
ngx_feature_name="NGX_HAVE_GEOIP_V6"
ngx_feature_run=no
ngx_feature_incs="#include <stdio.h>
                  #include <GeoIP.h>"
#ngx_feature_path=
#ngx_feature_libs=
ngx_feature_test="printf(\"%d\", GEOIP_CITY_EDITION_REV0_V6);"
. auto/feature

上面這個是個feature的模版,它的目的是為了檢查當前的環境是否包含這個feature, 比如上面的例子就是判斷這個機器環境是否支持GEO IPV6。

還可以看看下面這個例子:

ngx_feature="GeoIP library in /opt/local/"
ngx_feature_path="/opt/local/include"

if [ $NGX_RPATH = YES ]; then
    ngx_feature_libs="-R/opt/local/lib -L/opt/local/lib -lGeoIP"
else
    ngx_feature_libs="-L/opt/local/lib -lGeoIP"
fi

. auto/feature

feature的代碼就不貼出來了, 具體做了下面幾個事情:

  • 控制台先輸出checking for
  • 在auto_config.err中輸出checking for
  • 檢查變量$ngx_feature_name是否有設置
  • 如果設置了ngx_feature_path,就設置ngx_feature_inc_path
  • 根據$ngx_feature_test生成測試c文件
  • 編譯生成的測試c文件
  • 執行測試c文件並捕獲輸出到auto_config.err
  • 如果設置了ngx_feature_run, 就把輸出設置為對應的$ngx_feature_name的值
  • 沒有找到的情況,在auto_config.err中體現,並且在控制台顯示 not found
  • 刪除測試c文件及編譯出來的東西

auto/include

ngx_include="inttypes.h";    . auto/include

生成一個簡單的c文件:

cat << END > $NGX_AUTOTEST.c

$NGX_INCLUDE_SYS_PARAM_H
#include <$ngx_include>

int main(void) {
    return 0;
}

END

判斷檢測環境是否有這個頭文件。

auto/module

ngx_module_type=HTTP

if :; then
    ngx_module_name="ngx_http_module \
                     ngx_http_core_module \
                     ngx_http_log_module \
                     ngx_http_upstream_module"
    ngx_module_incs="src/http src/http/modules"
    ngx_module_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"
    ngx_module_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/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"
    ngx_module_libs=
    ngx_module_link=YES

    . auto/module

這個auto/module會對每一個模塊設置一些模塊需要的變量,比如模塊的源碼地址,模塊依賴的lib庫等。

控制台輸出:

checking for OS
 + Linux 3.10.0-514.16.1.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for gcc builtin 64 bit byteswap ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
checking for sys/filio.h ... not found
checking for sys/param.h ... found
checking for sys/mount.h ... found
checking for sys/statvfs.h ... found
checking for crypt.h ... found
checking for Linux specific features
checking for epoll ... found
checking for EPOLLRDHUP ... found
checking for EPOLLEXCLUSIVE ... not found
checking for O_PATH ... not found
checking for sendfile() ... found
checking for sendfile64() ... found
checking for sys/prctl.h ... found
checking for prctl(PR_SET_DUMPABLE) ... found
checking for prctl(PR_SET_KEEPCAPS) ... found
checking for capabilities ... found
checking for crypt_r() ... found
checking for sys/vfs.h ... found
checking for nobody group ... found
checking for poll() ... found
checking for /dev/poll ... not found
checking for kqueue ... not found
checking for crypt() ... not found
checking for crypt() in libcrypt ... found
checking for F_READAHEAD ... not found
checking for posix_fadvise() ... found
checking for O_DIRECT ... found
checking for F_NOCACHE ... not found
checking for directio() ... not found
checking for statfs() ... found
checking for statvfs() ... found
checking for dlopen() ... not found
checking for dlopen() in libdl ... found
checking for sched_yield() ... found
checking for sched_setaffinity() ... found
checking for SO_SETFIB ... not found
checking for SO_REUSEPORT ... found
checking for SO_ACCEPTFILTER ... not found
checking for SO_BINDANY ... not found
checking for IP_TRANSPARENT ... found
checking for IP_BINDANY ... not found
checking for IP_BIND_ADDRESS_NO_PORT ... not found
checking for IP_RECVDSTADDR ... not found
checking for IP_SENDSRCADDR ... not found
checking for IP_PKTINFO ... found
checking for IPV6_RECVPKTINFO ... found
checking for TCP_DEFER_ACCEPT ... found
checking for TCP_KEEPIDLE ... found
checking for TCP_FASTOPEN ... not found
checking for TCP_INFO ... found
checking for accept4() ... found
checking for eventfd() ... found
checking for int size ... 4 bytes
checking for long size ... 8 bytes
checking for long long size ... 8 bytes
checking for void * size ... 8 bytes
checking for uint32_t ... found
checking for uint64_t ... found
checking for sig_atomic_t ... found
checking for sig_atomic_t size ... 4 bytes
checking for socklen_t ... found
checking for in_addr_t ... found
checking for in_port_t ... found
checking for rlim_t ... found
checking for uintptr_t ... uintptr_t found
checking for system byte ordering ... little endian
checking for size_t size ... 8 bytes
checking for off_t size ... 8 bytes
checking for time_t size ... 8 bytes
checking for AF_INET6 ... found
checking for setproctitle() ... not found
checking for pread() ... found
checking for pwrite() ... found
checking for pwritev() ... found
checking for sys_nerr ... found
checking for localtime_r() ... found
checking for clock_gettime(CLOCK_MONOTONIC) ... not found
checking for clock_gettime(CLOCK_MONOTONIC) in librt ... found
checking for posix_memalign() ... found
checking for memalign() ... found
checking for mmap(MAP_ANON|MAP_SHARED) ... found
checking for mmap("/dev/zero", MAP_SHARED) ... found
checking for System V shared memory ... found
checking for POSIX semaphores ... not found
checking for POSIX semaphores in libpthread ... found
checking for struct msghdr.msg_control ... found
checking for ioctl(FIONBIO) ... found
checking for struct tm.tm_gmtoff ... found
checking for struct dirent.d_namlen ... not found
checking for struct dirent.d_type ... found
checking for sysconf(_SC_NPROCESSORS_ONLN) ... found
checking for sysconf(_SC_LEVEL1_DCACHE_LINESIZE) ... found
checking for openat(), fstatat() ... found
checking for getaddrinfo() ... found
checking for zlib library ... found
creating objs/Makefile

Configuration summary
  + PCRE library is not used
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp

我們其實可以對着 xmind 的流程和 configure 這個程序來看就很清晰了

https://github.com/its-tech/nginx-1.14.0-research/blob/master/configure

再加上前面幾個configure語法就能看懂了。

參考文章

https://blog.csdn.net/fzy0201/article/details/17683883


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM