解剖Nginx·模块开发篇(6)配置文件config入门

解剖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

-

你可能感兴趣的:(nginx,脚本,filter,Module,Path,library)