reference https://medium.com/@VarshaChahal/using-lua-nginx-module-for-dynamic-routing-based-on-redis-values-1740a10f9905
Dalam penginstall nginx harus di build dari source code nginx
Install beberapa package yang diperlukan
# apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev -y
install redis server
# apt install redis-server
Download resty luajit2 package
# wget -O Luajit.tar.gz https://github.com/openresty/luajit2/archive/refs/tags/v2.1-20231117.tar.gz
Extract Luajit
# tar -zxvf Luajit.tar.gz
Masuk ke direktori Luajit yg di extract tadi
# cd luajit2-2.1-20231117
Install package luajit
~/luajit2-2.1-20231117# make install
==== Successfully installed LuaJIT 2.1.1700206165 to /usr/local ====
Luajit berhasil terinstall keluar dari direktori luajit
Selanjutnya download beberapa package berikut
Nginx Package
# wget https://nginx.org/download/nginx-1.24.0.tar.gz
Nginx development kit
# wget -O ngnx_dev_kit.tar.gz https://github.com/vision5/ngx_devel_kit/archive/refs/tags/v0.3.3.tar.gz
Lua nginx module
# wget -O lua_nginx_module.tar.gz https://github.com/openresty/lua-nginx-module/archive/refs/tags/v0.10.26.tar.gz
Stream lua nginx module
# wget -O stream_lua_nginx.tar.gz https://github.com/openresty/stream-lua-nginx-module/archive/refs/tags/v0.0.14.tar.gz
Extract semua download package
# tar -zxvf nginx-1.24.0.tar.gz
# tar -zxvf ngnx_dev_kit.tar.gz
# tar -zxvf lua_nginx_module.tar.gz
# tar -zxvf stream_lua_nginx.tar.gz
Masuk direktori Nginx
# cd nginx-1.24.0/
Configure Nginx build package
~/nginx-1.24.0# LUAJIT_LIB=/usr/local/lib LUAJIT_INC=/usr/local/include/luajit-2.1 ./configure --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-pcre --pid-path=/var/run/nginx.pid --with-http_ssl_module --add-module=../ngx_devel_kit-0.3.3 --add-module=../lua-nginx-module-0.10.26 --add-module=../stream-lua-nginx-module-0.0.14 --with-stream --with-stream_ssl_module --with-ld-opt="-Wl,-rpath,/usr/local/lib"
Compile package and install
~/nginx-1.24.0# make install
Keluar direktori nginx dan pastikan sudah terinstall
# nginx -V
nginx version: nginx/1.24.0
built by gcc 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04)
built with OpenSSL 3.0.2 15 Mar 2022
TLS SNI support enabled
configure arguments: --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-pcre --pid-path=/var/run/nginx.pid --with-http_ssl_module --add-module=../ngx_devel_kit-0.3.3 --add-module=../lua-nginx-module-0.10.26 --add-module=../stream-lua-nginx-module-0.0.14 --with-stream --with-stream_ssl_module --with-ld-opt=-Wl,-rpath,/usr/local/lib
Download beberapa package lua dan lua-redis
# wget -O lua-resty-core.tar.gz https://github.com/openresty/lua-resty-core/archive/refs/tags/v0.1.28.tar.gz
#wget -O lua-resty-lrucache.tar.gz https://github.com/openresty/lua-resty-lrucache/archive/refs/tags/v0.13.tar.gz
# wget -O lua-resty-redis.tar.gz https://github.com/openresty/lua-resty-redis/archive/refs/tags/v0.29.tar.gz
Extract masuk di rektori lalu install modul
Lua resty core
# tar -zxvf lua-resty-core.tar.gz
# cd lua-resty-core-0.1.28/
~/lua-resty-core-0.1.28# make install LUA_LIB_DIR=/usr/local/share/lua/5.1
Lua resti lrucache
# tar -zxvf lua-resty-lrucache.tar.gz
# cd lua-resty-lrucache-0.13/
~/lua-resty-lrucache-0.13# make install LUA_LIB_DIR=/usr/local/share/lua/5.1
Lua resty redis
# tar -zxvf lua-resty-redis.tar.gz
# cd lua-resty-redis-0.29/
~/lua-resty-redis-0.29# make install LUA_LIB_DIR=/usr/local/share/lua/5.1
tambahkan package path di nginx.conf pada bagian dalam http
# nano /etc/nginx/nginx.conf
http {
lua_package_path "/usr/local/share/lua/5.1/?.lua;;";
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
Save and exit
Create systemd nginx
# nano /lib/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
Reload systemctl daemon
# systemctl daemon-reload
# systemctl start nginx
sekarang anda dapat melakukan enable start stop status disable dengan systemctl
Testing
https://openresty.org/en/dynamic-routing-based-on-redis.html
pada pengetesan tersebut adalah pengetesan redis dynamic router berdasarkan user-agent
Tambahkan pada location / {} site enabled nginx anda
resolver 8.8.4.4;
set $target '';
access_by_lua '
local key = ngx.var.http_user_agent
if not key then
ngx.log(ngx.ERR, "no user-agent found")
return ngx.exit(400)
end
local redis = require "resty.redis"
local red = redis:new()
red:set_timeout(1000) -- 1 second
local ok, err = red:connect("127.0.0.1", 6379)
if not ok then
ngx.log(ngx.ERR, "failed to connect to redis: ", err)
return ngx.exit(500)
end
local host, err = red:get(key)
if not host then
ngx.log(ngx.ERR, "failed to get redis key: ", err)
return ngx.exit(500)
end
if host == ngx.null then
ngx.log(ngx.ERR, "no host found for key ", key)
return ngx.exit(400)
end
ngx.var.target = host
';
proxy_pass http://$target;
#
service nginx restart
Masukkan data contoh dengan rediscli
# redis-cli
127.0.0.1:6379> set foo apache.org
OK
127.0.0.1:6379> set bar nginx.org
OK
selanjutnya saya test menggunakan postman web client untuk mengtest user-agent dengan foo dan bar
No comments:
Post a Comment