Skip to content

Instantly share code, notes, and snippets.

@tkuchiki
Last active May 11, 2023 06:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tkuchiki/7025119 to your computer and use it in GitHub Desktop.
Save tkuchiki/7025119 to your computer and use it in GitHub Desktop.
nginx source build (lua-nginx-module). https://gist.github.com/tkuchiki/7025062
# LUAJIT のディレクトリは適宜変更
LUAJIT_LIB=/usr/local/lib
NGX_VERSION=1.4.3
wget "http://nginx.org/download/nginx-$NGX_VERSION.tar.gz"
tar zxvf nginx-$NGX_VERSION.tar.gz
# ./configure: error: the HTTP rewrite module requires the PCRE library.
yum install pcre pcre-devel
yum install openssl openssl-devel zlib zlib-devel readline readline-devel libxml2 libxml2-devel libxslt-devel
NGX_MODULE_DIR=/usr/local/nginx/modules
mkdir -p $NGX_MODULE_DIR
cd !$
git clone https://github.com/simpl/ngx_devel_kit.git
git clone https://github.com/chaoslawful/lua-nginx-module.git
git clone https://github.com/agentzh/lua-resty-memcached.git
git clone https://github.com/agentzh/lua-resty-string.git
git clone https://github.com/agentzh/lua-resty-lock.git
git clone https://github.com/agentzh/lua-resty-redis.git
git clone https://github.com/agentzh/lua-resty-mysql.git
git clone https://github.com/agentzh/redis2-nginx-module.git
git clone https://github.com/agentzh/echo-nginx-module.git
git clone https://github.com/agentzh/srcache-nginx-module.git
git clone https://github.com/agentzh/memc-nginx-module.git
NGX_TMP_DIR=/var/tmp/nginx
mkdir -p $NGX_TMP_DIR/{client,proxy}
cd nginx-$NGX_VERSION
./configure --conf-path=/etc/nginx/nginx.conf \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--http-client-body-temp-path=$NGX_TMP_DIR/client \
--http-proxy-temp-path=$NGX_TMP_DIR/proxy \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--user=nginx --group=nginx \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-mail --with-mail_ssl_module \
--with-file-aio \
--with-ipv6 \
--with-poll_module \
--with-pcre-jit \
--with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' \
--with-ld-opt="-Wl,-rpath,$LUAJIT_LIB" \
--add-module=$NGX_MODULE_DIR/ngx_devel_kit \
--add-module=$NGX_MODULE_DIR/echo-nginx-module \
--add-module=$NGX_MODULE_DIR/lua-nginx-module
make && make install
# set search paths for pure Lua external libraries (';;' is the default path):
lua_package_path "/usr/local/nginx/modules/lua-resty-string/lib/?.lua;/usr/local/nginx/modules/lua-resty-memcached/lib/?.lua;;";
# set search paths for Lua external libraries written in C (can also use ';;'):
#lua_package_cpath '/bar/baz/?.so;/blah/blah/?.so;;';
server {
location /inline_concat {
# MIME type determined by default_type:
default_type 'text/plain';
set $a "hello";
set $b "world";
# inline Lua script
## ngx_devel_kit
set_by_lua $res "return ngx.arg[1]..ngx.arg[2]" $a $b;
## echo-nginx-module
echo $res;
}
location /file_concat {
default_type 'text/plain';
set $a "foo";
set $b "bar";
# script path relative to nginx prefix
# mkdir /etc/nginx/lua
# /etc/nginx/lua/concat.lua contents:
#
# return ngx.arg[1]..ngx.arg[2]
#
# relative path
set_by_lua_file $res lua/concat.lua $a $b;
# absolute path
set_by_lua_file $res /etc/nginx/lua/concat.lua $a $b;
echo $res;
}
location /lua_content {
default_type 'text/plain';
content_by_lua "ngx.print('Hello, lua.')";
}
location /nginx_var {
# MIME type determined by default_type:
default_type 'text/plain';
# try access /nginx_var?a=hello,world
#content_by_lua "ngx.print(ngx.var['arg_a'], '\\n')";
# try access /nginx_var?foo=bar&fuga=piyo
content_by_lua "ngx.print(ngx.var['arg_foo'], ngx.var['arg_fuga'], '\\n')";
}
location /request_body {
default_type 'text/plain';
# force reading request body (default off)
lua_need_request_body on;
client_max_body_size 50k;
client_body_buffer_size 50k;
# curl -d foo=bar http://localhost/request_body
# foo=bar
content_by_lua 'ngx.print(ngx.var.request_body)';
}
location /now {
default_type 'application/json';
content_by_lua '
local now = tostring(os.date("%Y-%m-%d %H:%M:%S", os.time()))
ngx.print(\'{"date": "\' .. now .. \'"}\')
';
}
# transparent non-blocking I/O in Lua via subrequests
location /lua {
# MIME type determined by default_type:
default_type 'application/json';
content_by_lua '
local res = ngx.location.capture("/now")
if res.status == 200 then
ngx.print(res.body)
end';
}
# GET /recur?num=5
# num is: 5
# status=200 body=num is: 4
# status=200 body=num is: 3
# status=200 body=num is: 2
# status=200 body=num is: 1
# status=200 body=num is: 0
# end
location /recur {
# MIME type determined by default_type:
default_type 'text/plain';
content_by_lua '
local num = tonumber(ngx.var.arg_num) or 0
if num > 50 then
ngx.say("num too big")
return
end
ngx.say("num is: ", num)
if num > 0 then
res = ngx.location.capture("/recur?num=" .. tostring(num - 1))
ngx.print("status=", res.status, " ")
ngx.print("body=", res.body)
else
ngx.say("end")
end
';
}
# 外部コマンドの実行結果を返す
location = /execute/command {
content_by_lua '
# retrieve POST
ngx.req.read_body()
local args = ngx.req.get_post_args()
if not args then
ngx.say("failed to get post args: ", err)
return
end
# execute command
local handle = io.popen("echo " .. args["password"])
# read STDIO
local result = handle:read("*a")
handle:close()
ngx.print(result)
';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment