静的モジュールの動的モジュールへの変換

NGINX 1.9.11以降は、モジュールの動的ロードの新しい方法が導入されました。このことは、設定ファイルに基づいて実行時にNGINXに選択したモジュールをロードできることを意味します。それらは設定ファイルを編集してNGINXをリロードすることで、アンロードすることもできます。

元々のの静的モジュールと動的モジュールの両方についてモジュールAPIは同じですが、config ファイルとそれらをどうやってコンパイルするかがわずかに異なります。これらの変更がこのドキュメント内で説明されます。

前提条件

全てのモジュールが同的モジュールに変換できるわけではありません。以下は気を付けなければならない事の幾つかです:

  • NGINXソースコードをパッチするモジュールは変換することをお勧めできません。They definitely won’t work when out-of-source compilation is supported.
  • モジュールのために使うように明示的に設計されたNGINX APIの部分だけを使うようにしてください。NGINXの内部に触れる場合には、動的モジュールと連携しないでしょう。

更に、以前に特定の順番でインストールする必要があったすべての動的モジュールは、今は必要な順番を ngx_module_order 変数を使って設定します。

思いがけない問題に遭遇した場合はNGINX 開発者メーリングリストに連絡を取ってください

動的モジュールのコンパイル

動的モジュールとしてモジュールを追加するために、新しい設定オプションが作られました。--add-moduleを使う代わりに、--add-dynamic-moduleを使ってください。例えば:

$ ./configure --add-dynamic-module=/opt/source/ngx_my_module/

makeコマンドを実行することで、モジュールはNGINXと一緒にコンパイルされます。別のやり方として、以下を行うことでNGINXにモジュールだけをビルドするように依頼することができます:

$ make -f objs/Makefile modules

With NGINX 1.9.13 the following is another way of doing this:

$ make modules

It is possible to run configure with the same parameters as before with an additional module, compiling as above and using the resulting module binary with the NGINX that has already been built with the source. 他の設定オプションあるいはNGINXソースを変更する場合は、今のところAPI/ABI互換性が無いため、全てを再コンパイルする必要があるでしょう。

モジュールをコンパイルする間、バイナリは.soファイルとしてobjs ディレクトリの中に生成されるでしょう。この.so ファイルはその後NGINXのインストレーション パスのmodules サブディレクトリにインストールされます。

動的モジュールのローディング

モジュールは新しいload_moduleディレクティブを使ってNGINXにロードすることができます。例えば:

load_module modules/ngx_my_module.so;

注意

NGINXのソースのNGX_MAX_DYNAMIC_MODULESで定義されているように、一度にロードできる動的モジュールには128のハードリミットがあります。このハードリミットはこの定数を編集することで増加させることができます。

設定ファイルの変換

以下はサードパーティのngx_http_response_moduleの古いスタイルのconfigのファイルの例です:

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

新しい方法は新しいスタイルの設定が動的および静的なモジュールを使うことができるように多くの事をセットアップする auto/module と呼ばれるビルドスクリプトを使います。これは config ファイル内のオプションを処理するでしょう。ngx_http_response_moduleのための新しいスタイルのバージョンは以下のようになるでしょう:

ngx_addon_name=ngx_http_response_module

if test -n "$ngx_module_link"; then
    ngx_module_type=HTTP
    ngx_module_name=ngx_http_response_module
    ngx_module_srcs="$ngx_addon_dir/ngx_http_response_module.c"

    . auto/module
else
    HTTP_MODULES="$HTTP_MODULES ngx_http_response_module"
    NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_response_module.c"
fi

これは、古いバージョンのNGINXがモジュールと互換性があるように古いスタイルのconfig ファイルも組み込みます。これらのオプションの詳細な説明は新しい設定シェルファイルで見ることができます

複雑な例

幾つかのモジュールは1つのパッケージ内に実際には複数のモジュールタイプがあります。これらは変換するには少しだけ複雑になるかも知れません。それらは静的モジュールとしてコンパイルする場合に個々のモジュールに分割する必要がありますが、動的モジュールについては1つの .so ファイルかも知れません。以下の例では、内部にCOREとHTTPモジュールを含む ngx_rtmp_moduleをしらべるつもりです。

最終的な変換は以下のようになります:

ngx_addon_name="ngx_rtmp_module"
RTMP_CORE_MODULES="                                         \
                ngx_rtmp_module                             \
                ngx_rtmp_core_module                        \
                ngx_rtmp_cmd_module                         \
                ngx_rtmp_codec_module                       \
                ngx_rtmp_access_module                      \
                ngx_rtmp_record_module                      \
                ngx_rtmp_live_module                        \
                ngx_rtmp_play_module                        \
                ngx_rtmp_flv_module                         \
                ngx_rtmp_mp4_module                         \
                ngx_rtmp_netcall_module                     \
                ngx_rtmp_relay_module                       \
                ngx_rtmp_exec_module                        \
                ngx_rtmp_auto_push_module                   \
                ngx_rtmp_notify_module                      \
                ngx_rtmp_log_module                         \
                ngx_rtmp_limit_module                       \
                ngx_rtmp_hls_module                         \
                ngx_rtmp_dash_module                        \
                "
RTMP_HTTP_MODULES="                                         \
                ngx_rtmp_stat_module                        \
                ngx_rtmp_control_module                     \
                "
RTMP_DEPS="                                                 \
                $ngx_addon_dir/ngx_rtmp_amf.h               \
                $ngx_addon_dir/ngx_rtmp_bandwidth.h         \
                $ngx_addon_dir/ngx_rtmp_cmd_module.h        \
                $ngx_addon_dir/ngx_rtmp_codec_module.h      \
                $ngx_addon_dir/ngx_rtmp_eval.h              \
                $ngx_addon_dir/ngx_rtmp.h                   \
                $ngx_addon_dir/ngx_rtmp_version.h           \
                $ngx_addon_dir/ngx_rtmp_live_module.h       \
                $ngx_addon_dir/ngx_rtmp_netcall_module.h    \
                $ngx_addon_dir/ngx_rtmp_play_module.h       \
                $ngx_addon_dir/ngx_rtmp_record_module.h     \
                $ngx_addon_dir/ngx_rtmp_relay_module.h      \
                $ngx_addon_dir/ngx_rtmp_streams.h           \
                $ngx_addon_dir/ngx_rtmp_bitop.h             \
                $ngx_addon_dir/ngx_rtmp_proxy_protocol.h    \
                $ngx_addon_dir/hls/ngx_rtmp_mpegts.h        \
                $ngx_addon_dir/dash/ngx_rtmp_mp4.h          \
                "
RTMP_CORE_SRCS="                                            \
                $ngx_addon_dir/ngx_rtmp.c                   \
                $ngx_addon_dir/ngx_rtmp_init.c              \
                $ngx_addon_dir/ngx_rtmp_handshake.c         \
                $ngx_addon_dir/ngx_rtmp_handler.c           \
                $ngx_addon_dir/ngx_rtmp_amf.c               \
                $ngx_addon_dir/ngx_rtmp_send.c              \
                $ngx_addon_dir/ngx_rtmp_shared.c            \
                $ngx_addon_dir/ngx_rtmp_eval.c              \
                $ngx_addon_dir/ngx_rtmp_receive.c           \
                $ngx_addon_dir/ngx_rtmp_core_module.c       \
                $ngx_addon_dir/ngx_rtmp_cmd_module.c        \
                $ngx_addon_dir/ngx_rtmp_codec_module.c      \
                $ngx_addon_dir/ngx_rtmp_access_module.c     \
                $ngx_addon_dir/ngx_rtmp_record_module.c     \
                $ngx_addon_dir/ngx_rtmp_live_module.c       \
                $ngx_addon_dir/ngx_rtmp_play_module.c       \
                $ngx_addon_dir/ngx_rtmp_flv_module.c        \
                $ngx_addon_dir/ngx_rtmp_mp4_module.c        \
                $ngx_addon_dir/ngx_rtmp_netcall_module.c    \
                $ngx_addon_dir/ngx_rtmp_relay_module.c      \
                $ngx_addon_dir/ngx_rtmp_bandwidth.c         \
                $ngx_addon_dir/ngx_rtmp_exec_module.c       \
                $ngx_addon_dir/ngx_rtmp_auto_push_module.c  \
                $ngx_addon_dir/ngx_rtmp_notify_module.c     \
                $ngx_addon_dir/ngx_rtmp_log_module.c        \
                $ngx_addon_dir/ngx_rtmp_limit_module.c      \
                $ngx_addon_dir/ngx_rtmp_bitop.c             \
                $ngx_addon_dir/ngx_rtmp_proxy_protocol.c    \
                $ngx_addon_dir/hls/ngx_rtmp_hls_module.c    \
                $ngx_addon_dir/dash/ngx_rtmp_dash_module.c  \
                $ngx_addon_dir/hls/ngx_rtmp_mpegts.c        \
                $ngx_addon_dir/dash/ngx_rtmp_mp4.c          \
                "
RTMP_HTTP_SRCS="                                            \
                $ngx_addon_dir/ngx_rtmp_stat_module.c       \
                $ngx_addon_dir/ngx_rtmp_control_module.c    \
                "
ngx_module_incs=$ngx_addon_dir
ngx_module_deps=$RTMP_DEPS

if [ $ngx_module_link = DYNAMIC ] ; then
    ngx_module_name="$RTMP_CORE_MODULES $RTMP_HTTP_MODULES"
    ngx_module_srcs="$RTMP_CORE_SRCS $RTMP_HTTP_SRCS"
    . auto/module
elif [ $ngx_module_link = ADDON ] ; then
    ngx_module_type=CORE
    ngx_module_name=$RTMP_CORE_MODULES
    ngx_module_srcs=$RTMP_CORE_SRCS
    . auto/module
    ngx_module_type=HTTP
    ngx_module_name=$RTMP_HTTP_MODULES
    ngx_module_srcs=$RTMP_HTTP_SRCS
    . auto/module
fi

USE_OPENSSL=YES

モジュールをコンパイルする場合、静的なモジュールとしてモジュールをコンパイルするために$ngx_module_linkADDONに設定され、動的なモジュールとしてコンパイルする場合は DYNAMICに設定されます。静的なコンパイルは auto/module を二回呼びます。1回はCOREモジュールのため、もう1回はHTTPモジュールのためです。それに対し、動的なコンパイルは1つのモジュール内で起こります。

TOP
inserted by FC2 system