源碼地址:https://github.com/Tinywan/Lua-Nginx-Redis
一、 目標
使用Redis做分布式緩存;使用lua API來訪問redis緩存;使用nginx向客戶端提供服務,ngx_lua將lua嵌入到nginx,讓nginx執行lua腳本,高並發,非阻塞的處理各種請求。url請求nginx服務器,然后lua查詢redis,返回json數據。
二、准備工作
系統環境:Ubuntu 14.0 (64位)
Redis服務安裝:apt-get install redis-server
安裝Git:apt-get install git
安裝Lua:
# apt-get install lua5.1
# apt-get install liblua5.1-dev # apt-get install liblua5.1-socket2 # apt-get install -y lua5.1 liblua5.1-0 liblua5.1-0-dev
補充:安裝模塊:stream-lua-nginx-module 出現的錯誤信息:
make[1]: *** [objs/addon/src/ngx_stream_lua_socket_tcp.o] Error 1
解決辦法:
apt-get install lua-socket
三、下載庫
1、當前目錄:/home/www 目錄下面
2、下載ngx_devel_kit (NDK(nginx development kit)模塊,是一個拓展nginx服務器核心功能的模塊,第三方模塊開發可以基於它來快速實現。
wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
tar -zxvf v0.3.0.tar.gz
3、lua-nginx-module 下載。可在 Nginx 中嵌入 Lua 語言,讓 Nginx 可以支持 Lua 強大的語法。
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.7.tar.gz
tar -zxvf v0.10.7.tar.gz
4、redis2-nginx-module 下載。是一個支持 Redis 2.0 協議的 Nginx upstream 模塊,它可以讓 Nginx 以非阻塞方式直接防問遠方的 Redis 服務,同時支持 TCP 協議和 Unix Domain Socket 模式,並且可以啟用強大的 Redis 連接池功能。
wget https://github.com/openresty/redis2-nginx-module/archive/v0.13.tar.gz
tar -zxvf v0.13.tar.gz
5、set-misc-nginx-module 下載。是標准的HttpRewriteModule指令的擴展,提供更多的功能,如URI轉義與非轉義、JSON引述,Hexadecimal、MD5、SHA1、Base32、Base64編碼與解碼、隨機數等等
wget https://github.com/openresty/set-misc-nginx-module/archive/v0.31.tar.gz
tar -zxvf v0.31.tar.gz
6、echo-nginx-module 下載,是一個 Nginx 模塊,提供直接在 Nginx 配置使用包括 "echo", "sleep", "time" 等指令。
wget https://github.com/openresty/echo-nginx-module/archive/v0.60.tar.gz
tar -zxvf v0.60.tar.gz
7、Nginx 下載
wget http://nginx.org/download/nginx-1.10.3.tar.gz
tar -zxvf nginx-1.10.3.tar.gz
四、安裝
1、查看所有下載完的包
root@iZ236j3sofdZ:/home/www# ls echo-nginx-module-0.60 lua-nginx-module-0.10.7 nginx-1.10.3 nginx-1.10.3.tar.gz
ngx_devel_kit-0.3.0 redis2-nginx-module-0.13 redis-lua-2.0.4 set-misc-nginx-module-0.31
2、Nginx 配置文件檢測
cd nginx-1.10.3/ ./configure --prefix=/usr/local/nginx --with-debug --with-http_addition_module \
--with-http_perl_module --with-http_realip_module --with-http_secure_link_module \
--with-http_stub_status_module --with-http_ssl_module --with-http_sub_module \
--with-sha1=/usr/include/openssl --with-md5=/usr/include/openssl \
--add-module=../ngx_devel_kit-0.3.0 \
--add-module=../echo-nginx-module-0.60 \
--add-module=../lua-nginx-module-0.10.7 \
--add-module=../redis2-nginx-module-0.13 \
--add-module=../set-misc-nginx-module-0.31
【錯誤信息1】:
./configure: error: ngx_http_lua_module requires the Lua library
解決辦法:
apt-get install lua5.1-0-dev
【錯誤信息3】:
./configure: error: the HTTP rewrite module requires the PCRE library.
解決辦法:
apt-get install libreadline-dev libncurses5-dev libpcre3-dev \
libssl-dev perl make build-essential curl
【錯誤信息2】:
/usr/bin/ld: cannot find -lperl collect2: error: ld returned 1 exit status
說明:/usr/bin/ld: cannot find -lxxx意思是編譯過程找不到對應庫文件。其中,-lxxx表示鏈接庫文件 libxxx.so。了解更多cannot find -lxxx
解決辦法:
apt-get install lperl-dev
五、編譯
make
make install
編譯過程出現這個問題:
make[1]: Leaving directory `/home/www/nginx-1.10.3' 什么意思
解決辦法:
到你的源碼目錄內,先make clean 然后./config ...
3、安裝lua-redis-parser,lua-resty-redis是openresty(1.9.15.1)的一個組件,簡單來說,它提供一個lua語言版的redis API,使用socket(lua sock)和redis通信。
//下載源碼包: git clone https://github.com/openresty/lua-resty-redis.git
移動該源碼包到/usr/local/nginx/lua/ 這里去
mv lua-resty-redis /usr/local/nginx/lua/
4、使用Redis的提示錯誤
[error] 7094#0: *1 lua entry thread aborted: runtime error: /usr/local/nginx/conf/lua/test_redis_basic.lua:11: module 'resty.redis' not found:
no field package.preload['resty.redis']
no file '/home/www/lua-redis-parser-0.12/resty/redis.lua'
no file './resty/redis.lua'
no file '/usr/local/share/lua/5.1/resty/redis.lua'
no file '/usr/local/share/lua/5.1/resty/redis/init.lua'
no file '/usr/local/lib/lua/5.1/resty/redis.lua'
no file '/usr/local/lib/lua/5.1/resty/redis/init.lua'
no file '/usr/share/lua/5.1/resty/redis.lua'
no file '/usr/share/lua/5.1/resty/redis/init.lua'
no file './resty/redis.so'
no file '/usr/local/lib/lua/5.1/resty/redis.so'
no file '/usr/lib/x86_64-linux-gnu/lua/5.1/resty/redis.so'
no file '/usr/lib/lua/5.1/resty/redis.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
no file './resty.so'
no file '/usr/local/lib/lua/5.1/resty.so'
no file '/usr/lib/x86_64-linux-gnu/lua/5.1/resty.so'
no file '/usr/lib/lua/5.1/resty.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
提示以上錯誤的原因是Lua的庫文件沒有加載合適導致的。只需要下載官方的源碼包,按照以下應用即可
lua_package_path "/usr/local/nginx/lua/lua-resty-redis/lib/?.lua;;";
六、測試
1、配置nginx.conf(以下是部分代碼)
# nginx.conf http { ..... lua_package_path "/usr/local/nginx/lua/lua-resty-redis/lib/?.lua;;"; #gzip on;
server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } location /lua { echo "Hello Lua"; } location /lua_test { content_by_lua ' ngx.say("Hello Lua! Tinywan") '; } #content_by_lua_block
location =/content_by_lua_block { default_type 'text/plain'; content_by_lua_block { ngx.say('Hello : content_by_lua_block') } } location /{ default_type 'text/html'; lua_code_cache off; content_by_lua_file /usr/local/nginx/conf/lua/test_redis_basic.lua; } } }
test_redis_basic.lua 添加以下內容:
local function close_redis(redis_instance) if not redis_instance then
return
end
local ok,err = redis_instance:close(); if not ok then ngx.say("close redis error : ",err); end
end
local redis = require("resty.redis"); --local redis = require "redis" -- 創建一個redis對象實例。在失敗,返回nil和描述錯誤的字符串的情況下
local redis_instance = redis:new(); --設置后續操作的超時(以毫秒為單位)保護,包括connect方法
redis_instance:set_timeout(1000) --建立連接
local ip = '127.0.0.1'
local port = 6379
--嘗試連接到redis服務器正在偵聽的遠程主機和端口
local ok,err = redis_instance:connect(ip,port) if not ok then ngx.say("connect redis error : ",err) return close_redis(redis_instance); end
--Redis身份驗證 --local auth,err = redis_instance:auth(""); --if not auth then -- ngx.say("failed to authenticate : ",err) --end
--調用API進行處理
local resp,err = redis_instance:set("msg","hello world") if not resp then ngx.say("set msg error : ",err) return close_redis(redis_instance) end
--調用API獲取數據
local resp, err = redis_instance:get("msg") if not resp then ngx.say("get msg error : ", err) return close_redis(redis_instance) end
--得到的數據為空處理
if resp == ngx.null then resp = 'this is not redis_data' --比如默認值
end ngx.say("msg : ", resp) close_redis(redis_instance)
我們通過curl 在shell腳本中測試以上配置文件
重啟Nginx服務器:
root@iZ236j3sofdZ:/usr/local/nginx/conf# service nginx restart * Stopping Nginx Server... * Starting Nginx Server...
nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/nginx/conf/nginx.conf:69
警告:這個alert是因為objstore.conf中把lua_code_cache為off;若設置為off,nginx不緩存lua腳本,每次改變lua代碼,不必reload nginx即可生效;這便於開發和測試。但禁用緩存對性能有影響,故正式環境下一定記得設置為on;
location /lua
root@iZ236j3sofdZ:/usr/local/nginx/lua# curl "http://localhost/lua" Hello Lua
location /lua_test
root@iZ236j3sofdZ:/usr/local/nginx/lua# curl "http://localhost/lua_test" Hello Lua! Tinywan
location /content_by_lua_block
root@iZ236j3sofdZ:/usr/local/nginx/lua# curl "http://localhost/content_by_lua_block" Hello : content_by_lua_block
location /lua_redis_basic
root@iZ236j3sofdZ:/usr/local/nginx/lua# curl "http://localhost/lua_redis_basic" msg : hello worTinywan
七、一些常見誤區
1、redis2-nginx-module和lua-resty-redis
redis2-nginx-module是一個openresty(1.9.15.1)自帶的模塊。它能夠把請求轉發給upstream(redis2_pass)。注意它和lua-resty-redis不同,lua-resty-redis是一個lua語言版的redis API,使用socket(lua sock)和redis通信。而redis2-nginx-module是把請求轉發給別的upstream。
2、以下錯誤解決辦法
checking for LuaJIT library in /usr/bin/luajit/lib and (specified by the LUAJIT_LIB and LUAJIT_INC env, with -ldl) ... not found checking for LuaJIT library in /usr/bin/luajit/lib and (specified by the LUAJIT_LIB and LUAJIT_INC env) ... not found ./configure: error: ngx_http_lua_module requires the Lua or LuaJIT library and LUAJIT_LIB is defined as
/usr/bin/luajit/lib and LUAJIT_INC (path for lua.h) , but we cannot find LuaJIT there.
下載安裝:LuaJIT-2.0.4
更多版本下載地址:http://luajit.org/download.html
wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz tar -zxvf LuaJIT-2.0.4.tar.gz make && sudo make install
告訴nginx的構建系統在哪里可以找到LuaJIT 2.0:
export LUAJIT_INC=/usr/local/include/luajit-2.0