티스토리 뷰

Server/WebServer

How to use nginx lua module

Jacob_baek 2022. 2. 21. 21:47

먼저 가정은 별도의 lua module loading 과정은 사전에 이루어져 있다 보고 기술하였다.

기본 workflow를 알아둘 필요가 있다.

https://cloud.githubusercontent.com/assets/2137369/15272097/77d1c09e-1a37-11e6-97ef-d9767035fc3e.png

위 flow에 기반하여 lua 모듈이 동작되어질 위치를 확인하고 다음과 같은 module을 통해 호출을 하면된다.
(주로 사용해본 모듈은 rewrite_by_lua_xxx, access_by_lua_xxx, content_by_lua_xxx, set_by_lua_xxx 등이 있다.)

content_by_lua

먼저 간단히 content_by_lua 모듈을 이용하여 lua code를 호출하는 방법을 알아보자.

content_by_lua_block

아래는 nginx.conf내에 직접 lua code를 작성한 것이다.

server {
    ...
    location /lua_block {
        content_by_lua_block {
            ngx.say("hi, iam lua_block")
        }
    }
}

ngx.say를 통해 작성된 메세지("hi, iam lua_block")가 response body로 전달되어 web browser 상에 출력된다.

content_by_lua_file

nginx.conf내에 존재하지 않는 lua file을 직접 호출하는 방식이다.
여기서 중요한것은 파일 경로인데 절대경로(absolute path)를 써야 한다.

server {
    ...
    location /lua_file {
        content_by_lua_file /etc/nginx/lua/test.lua;
    }
}

또한 파일내용은 아래와 같이 ngx.exit(ngx.OK)와 같은 return 값을 제공해야 한다.

#ngx.say("hello world")
ngx.exit(ngx.OK)

위와 같은 lua 파일을 lua_package_path가 선언된 경로(/etc/nginx/lua)에 생성해두면 lua를 불러오고 "hello world"를 출력하게 된다.
만약 선언되어 있지 않다면 https://github.com/openresty/lua-nginx-module#lua_package_path 를 참고하여 선언을 해야한다.

참고로 필자의 경우 nginx-ingress를 통해 구현하였었기에 기본적으로 /etc/nginx/lua 라는 경로에 모듈을 사용할수 있게 되어 있었다.

예제를 기반한 lua module 사용

geoip를 기반으로 허용되지 않은 나라에 대한 차단을 수행하는 예제이다.
아래와 같이 403 return을 주도록 할수 있다.

먼저 content_by_lua_block을 이용한 예제이다.

         set $block_countries "KR";

         if ($geoip_country_code) {
             content_by_lua_block {
               if string.find(ngx.var.block_countries, ngx.var.geoip_country_code) then
                 ngx.exit(403)
               end
             }
         }

set_by_lua를 이용하여 변수화를 한뒤 다음과 같이 403 return이 되도록 할수 있다.

         set $block_countries "KR";
         set_by_lua $checkgeoip "
           local ret = 0
           if string.find(ngx.var.block_countries, ngx.var.geoip_country_code) then
             ret = 1
           end
           return ret
         ";

         if ($checkgeoip) {
            return 403;
         }

마지막으로 lua file을 통한 403 return 방식이다.

### nginx.conf의 location block 내
         set $block_countries "JP,KR,CN";
         if ($geoip_country_code) {
             access_by_lua_file  "/etc/nginx/lua/test/test.lua";
         }

### /etc/nginx/test/test.lua
if string.find(ngx.var.block_countries, ngx.var.geoip_country_code) then
  ngx.exit(403)
end

참고로 data sharing 코드는 다음예제를 참고하면 좋다.

lua file 생성

lua file은 다음과 같은 형식을 가지는것을 권장한다.

# hello_world
local ngx = ngx

local _M = {}

function _M.rewrite()
  local ua = ngx.var.http_user_agent

  if ua == "hello" then
    ngx.req.set_header("x-hello-world", "1")
  end
end

return _M

위와 같이 생성된 경우 호출시도 require 을 통해 파일 명을 로드하고 이를 함수(_M.rewrite())로
다시 호출하여 동작되게 한다.

        location / {
            content_by_lua_block {
                local hello = require "hello"
                hello.rewrite("a Lua module")
            }

주요 예제

참고사이트

'Server > WebServer' 카테고리의 다른 글

response custom error page from proxy server  (0) 2022.03.03
serve single file on NGINX  (0) 2021.03.23
print local ip address and hostname using php on httpd  (0) 2017.04.17
Apache Traffic Server (ATS)  (0) 2014.04.29
apache httpd module  (0) 2012.04.03
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함