解剖Nginx·模組開發篇(6)配置檔案config入門

鍾超發表於2012-06-13

解剖Nginx·模組開發篇(6)配置檔案config入門

  • 作者:柳大·Poechant(鍾超)
  • 郵箱:zhongchao.ustc#gmail.com(# -> @)
  • 部落格:Blog.CSDN.net/Poechant
  • 日期:June 13th, 2012

本文介紹 config 檔案的初級寫法,作為入門。首先你要知道 config 其實就是一個 shell 指令碼,是在./configure的時候呼叫的。所以你可以用寫 shell 指令碼的方式來寫 config。一般的形式如下:

ngx_addon_name=ngx_http_XXXXXXXXXXXXX_module
HTTP_MODULES="$HTTP_MODULES ngx_http_XXXXXXXXXXXXX_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_XXXXXXXXXXXXX_module.c"

這三句話的含義是:

  1. 模組名稱
  2. 模組型別(表徵了其作用域)
  3. 模組原始檔

所以不同功能的模組的配置檔案都大同小異:

Handler 模組的配置檔案

如果模組名稱為 ngx_http_poechant_handler_module,那麼配置檔案內容如下:

ngx_addon_name=ngx_http_poechant_handler_module
HTTP_MODULES="$HTTP_MODULES ngx_http_poechant_handler_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_poechant_handler_module.c"

Filter 模組的配置檔案

如果模組名稱為 ngx_http_poechant_filter_module,那麼配置檔案內容如下:

ngx_addon_name=ngx_http_poechant_filter_module
HTTP_AUX_FILTER_MODULES="$HTTP_AUX_FILTER_MODULES ngx_http_poechant_filter_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_poechant_filter_module.c"

以 nginx-http-footer-filter 模組為例

ngx_addon_name=ngx_http_footer_filter_module
HTTP_FILTER_MODULES="$HTTP_FILTER_MODULES ngx_http_footer_filter_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_footer_filter_module.c"

Load balancer 模組的配置檔案

以 ngx_http_php_memcache_standard_balancer 模組為例:

ngx_addon_name=ngx_http_php_memcache_standard_balancer
HTTP_MODULES="$HTTP_MODULES ngx_http_php_memcache_standard_balancer_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_php_memcache_standard_balancer.c"

複雜一些的配置檔案

複雜一些的配置檔案,不過是進行一起預備行為,用 shell 指令碼的方式做些事情,比如 drizzle-nginx-module 這個第三方模組的配置檔案如下:

ngx_feature_name=
ngx_feature_run=no
ngx_feature_incs="#include <libdrizzle/drizzle_client.h>"
ngx_feature_test="drizzle_version();"

if [ -n "$LIBDRIZZLE_INC" -o -n "$LIBDRIZZLE_LIB" ]; then
    # explicit set libdrizzle lib path
    ngx_feature="libdrizzle library in directories specified by LIBDRIZZLE_INC ($LIBDRIZZLE_INC) and LIBDRIZZLE_LIB ($LIBDRIZZLE_LIB)"
    ngx_feature_path="$LIBDRIZZLE_INC"
    if [ $NGX_RPATH = YES ]; then
        ngx_feature_libs="-R$LIBDRIZZLE_LIB -L$LIBDRIZZLE_LIB -ldrizzle"
    else
        ngx_feature_libs="-L$LIBDRIZZLE_LIB -ldrizzle"
    fi
    . auto/feature
else
    # auto-discovery
    ngx_feature="libdrizzle library"
    ngx_feature_path=
    ngx_feature_libs="-ldrizzle"
    . auto/feature

    if [ $ngx_found = no ]; then
        # FreeBSD, OpenBSD
        ngx_feature="libdrizzle library in /usr/local/"
        ngx_feature_path="/usr/local/include/libdrizzle-1.0"
        if [ $NGX_RPATH = YES ]; then
            ngx_feature_libs="-R/usr/local/lib -L/usr/local/lib -ldrizzle"
        else
            ngx_feature_libs="-L/usr/local/lib -ldrizzle"
        fi
        . auto/feature
    fi

    if [ $ngx_found = no ]; then
        # NetBSD
        ngx_feature="libdrizzle library in /usr/pkg/"
        ngx_feature_path="/usr/pkg/include/libdrizzle-1.0"
        if [ $NGX_RPATH = YES ]; then
            ngx_feature_libs="-R/usr/pkg/lib -L/usr/pkg/lib -ldrizzle"
        else
            ngx_feature_libs="-L/usr/pkg/lib -ldrizzle"
        fi
        . auto/feature
    fi

    if [ $ngx_found = no ]; then
        # MacPorts
        ngx_feature="libdrizzle library in /opt/local/"
        ngx_feature_path="/opt/local/include/libdrizzle-1.0"
        if [ $NGX_RPATH = YES ]; then
            ngx_feature_libs="-R/opt/local/lib -L/opt/local/lib -ldrizzle"
        else
            ngx_feature_libs="-L/opt/local/lib -ldrizzle"
        fi
        . auto/feature
    fi
fi

if [ $ngx_found = yes ]; then
    CORE_INCS="$CORE_INCS $ngx_feature_path"
    CORE_LIBS="$CORE_LIBS $ngx_feature_libs"
else
 cat << END
 $0: error: the ngx_drizzle addon requires the libdrizzle library.
END
 exit 1
fi

ngx_addon_name=ngx_http_drizzle_module
HTTP_MODULES="$HTTP_MODULES ngx_http_drizzle_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/src/ngx_http_drizzle_module.c $ngx_addon_dir/src/ngx_http_drizzle_handler.c $ngx_addon_dir/src/ngx_http_drizzle_processor.c $ngx_addon_dir/src/ngx_http_drizzle_upstream.c $ngx_addon_dir/src/ngx_http_drizzle_util.c $ngx_addon_dir/src/ngx_http_drizzle_output.c $ngx_addon_dir/src/ngx_http_drizzle_keepalive.c $ngx_addon_dir/src/ngx_http_drizzle_quoting.c $ngx_addon_dir/src/ngx_http_drizzle_checker.c"
NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ngx_addon_dir/src/ddebug.h $ngx_addon_dir/src/ngx_http_drizzle_module.h $ngx_addon_dir/src/ngx_http_drizzle_handler.h $ngx_addon_dir/src/ngx_http_drizzle_processor.h $ngx_addon_dir/src/ngx_http_drizzle_upstream.h $ngx_addon_dir/src/ngx_http_drizzle_util.h $ngx_addon_dir/src/ngx_http_drizzle_output.h $ngx_addon_dir/src/resty_dbd_stream.h $ngx_addon_dir/src/ngx_http_drizzle_keepalive.h $ngx_addon_dir/src/ngx_http_drizzle_quoting.h $ngx_addon_dir/src/ngx_http_drizzle_checker.h"

have=NGX_DRIZZLE_MODULE . auto/have

上文中我提到配置檔案 config 是在./configure的時候呼叫的,根據我前面寫的系列文章《解剖Nginx·自動指令碼篇》中的介紹,一些工具類指令碼就可以用上了,比如auto/feature,就如模組 drizzle-nginx-module 的配置檔案中所使用的一樣。還記得auto/feature的功能嗎?可以回顧下《解剖Nginx·自動指令碼篇(4)工具型指令碼系列》

  • 輸出檢查提示
  • 初始化ngx_found
  • 初始化ngx_have_feature
  • 初始化ngx_feature_inc_path
  • 生成自動測試程式
  • 編譯自動測試程式
  • 根據ngx_feature_run處理
  • 刪除NGX_AUTOSET測試程式可執行檔案

-

轉載請註明來自柳大的CSDN部落格:Blog.CSDN.net/Poechant

-

相關文章