Skip to content

Instantly share code, notes, and snippets.

@yteraoka
Last active December 24, 2015 11:29
Show Gist options
  • Save yteraoka/6791341 to your computer and use it in GitHub Desktop.
Save yteraoka/6791341 to your computer and use it in GitHub Desktop.

デフォルトで変数のスコープは Global であるため、 perl の my のように local 宣言しましょう。

local var = "hogehoge"

文字列はダブルクオートが必要

        location /test {
            content_by_lua '
                ngx.header.content_type = "text/plain";
                ngx.say("hogehoge");
            ';
        }

Accept-Encoding によっては proxy した結果の body には gzip されたデータが入っている。

ngx.req.set_header("Accept-Encoding", nil)

として Accept-Encoding をクリアしておけば gzip はされない

認証フィルターを書くなら access_by_lua を使う

http://wiki.nginx.org/HttpLuaModule#access_by_lua

Lua に配列型は存在せず、PHPのように連想配列を配列のように使う。

配列の場合は ipairs を次のように使えば i に 1,2,3 と添字が、value に要素が入る。 key が数字ではない場合は pairs() を使う。

local hogehoge_list = { "AAA", "BBB", "CCC" }
table.insert(hogehoge_list, "DDD")
for i , value in ipairs(hogehoge_list) do
  ngx.log(ngx.DEBUG, i, ": ", value)
end

hogehoge_list[2] と直接アクセスすることもできる。添字は1から始まる。0ではない。 入れるの要素数は #hogehoge_list で得られる、ただし、値に nil が入っている場合はその前の要素までの数となる。 ipairs() も同じで、途中で番号が飛ぶとそこまでで loop は終了する。そんな場合は pairs() で。

コメントは SQL のように「--」移行がコメントとして扱われる

while loop

local i = 0
while i < 10 do
    i = i + 1
    ngx.say(i)
end

loop を抜けるのは break

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment