apt-getでDebian 8 + lua nginx moduleの環境構築(nginx-extras)
nginxにluaを組み込むlua-nginx-module。以前はソースからビルドしたのですが、
Debian 7 + nginx 1.6.2 + lua-nginx-moduleの環境構築
nginx-extrasをインストールすれば使用できるようです。
https://wiki.debian.org/Nginx
Debian 8にインストールして触ってみます。
nginx-extras
apt-getでインストール。
# apt-get install nginx-extras
nginxに加え、libluajit-5.1もインスールされました。
動作確認用に、nginxの設定ファイルへluaプログラムを記載してみます。
/etc/nginx/sites-available/default
location部分を変更。
location / {
default_type 'text/plain';
content_by_lua "ngx.say('Hello,lua!')";
}
default_type 'text/plain';がミソ。
この指定がないとファイルのダウンロードになります。
設定ファイル再読み込み。
# service nginx reload
ブラウザでhttp://[サーバーIP]を表示するとちゃんとメッセージが表示されました。

設定ファイルを変更した場合、service nginx reloadを実行しないと反映されません。
こんな感じで、表示するメッセージを変更した場合は、reloadしてやります。
location / {
default_type 'text/plain';
content_by_lua "ngx.say('Hello,symfo!')";
}

ファイルへ処理を記載
luaプログラムをファイルに記載する場合は、nginxの設定ファイルに
access_by_lua_file [ファイルのパス];
と記載します。
過去の記事が参考になりました。
nginx 1.6.2 + lua-nginx-moduleで簡易ファイルアップローダー
nginx 1.6.2 + lua-nginx-moduleでapache2 mod_access_tokenを実装
最初の例をluaファイルに記載してみます。
・/var/dev/lua/sample.lua
- ngx.say('Hello,lua!')
nginxの設定ファイルを変更します。
location / {
default_type 'text/plain';
content_by_lua_file /var/dev/lua/sample.lua;
}
設定ファイル再読み込み。
# service nginx reload
これで最初と同様の内容が表示されます。
デバック
ファイルを変更するたびに設定ファイルの再読み込みは面倒と思い調べてみると、
lua_code_cache
lua_code_cacheをoffにすると、毎回luaファイルを処理してくれるようです。
※もちろん、可動環境ではonにしておく必要あり。
nginxの設定ファイルに設定を追記します。
location / {
lua_code_cache off;
default_type 'text/plain';
content_by_lua_file /var/dev/lua/sample.lua;
}
設定を反映
# service nginx reload
これでluaファイルの内容を変更後、ブラウザをリロードするとすぐに反映されるようになりました。
ログの出力は、こちらが参考になりました。
How to debug lua code inside nginx config?
作成した/var/dev/lua/sample.luaを修正。
- ngx.say('Hello,symfo!')
- ngx.log(ngx.STDERR, 'your message here')
ブラウザをリロードすると、nginxのエラーログ(/var/log/nginx/error.log)にメッセージが出力されます。
# tail -f /var/log/nginx/error.log
2017/04/09 15:14:10 [alert] 1290#0: aborting
2017/04/09 15:14:10 [info] 1301#0: Using 32768KiB of shared memory for push module in /etc/nginx/nginx.conf:63
2017/04/09 15:15:29 [notice] 1315#0: signal process started
2017/04/09 15:21:22 [notice] 1331#0: signal process started
2017/04/09 15:34:56 [notice] 1346#0: signal process started
2017/04/09 15:35:32 [notice] 1358#0: signal process started
2017/04/09 15:38:37 [alert] 1369#0: lua_code_cache is off; this will hurt performance in /etc/nginx/sites-enabled/default:38
2017/04/09 15:38:37 [notice] 1369#0: signal process started
2017/04/09 15:38:37 [alert] 1305#0: lua_code_cache is off; this will hurt performance in /etc/nginx/sites-enabled/default:38
2017/04/09 15:40:28 [] 1371#0: *15 [lua] sample.lua:2: your message here, client: 192.168.1.4, server: _, request: "GET / HTTP/1.1", host: "192.168.1.104"
- 関連記事
-
- lua nginx moduleからMariaDB(MySQL)に接続する(lua-sql-mysql)
- lua nginx moduleでjson形式のレスポンスを返す(cjson)
- apt-getでDebian 8 + lua nginx moduleの環境構築(nginx-extras)
- lftp 隠しファイル(ドットから始まるファイル)もmirrorする
- nginx + luaで簡易ファイルストレージ
コメント