nginx+lua+redis實踐
概述
nginx、lua訪問redis的三種方式:
1。?HttpRedis模塊。
指令少,功能單一 ,適合簡單的緩存。只支持get 、select命令。
2。?HttpRedis2Module模塊。
功能強大,比較靈活。
3。?lua-resty-redis庫
OpenResty。api。適合復雜業務,節省內存。
OpenResty:基于nginx開源版本的一個擴展版本。集成了大量的精良的lua庫。
OpenResty安裝
安裝wget
[root@localhost?yum.repos.d]# yum?install?wget
下載資源庫
[root@localhost?yum.repos.d]# wget?https://openresty.org/package/centos/openresty.repo
得到文件: openresty.repo?安裝OpenResty
[root@localhost?yum.repos.d]# yum?install?openresty
啟動OpenResty
[root@localhost?yum.repos.d]# /usr/local/openresty/nginx/sbin/nginx?-p /usr/local/openresty/nginx/
[root@localhost?yum.repos.d]#
測試:
我們用nat,注意端口映射。

初試測試lua
修改conf
server?{
listen?8080;?location?/ {
default_type?text/html;
content_by_lua?'ngx.say("hello?my?openresty")';?}
}
redis安裝
安裝epel:第三方的源(軟件安裝包)。
[root@localhost?/]# yum?install?epel-release
安裝redis
[root@localhost?/]# yum?install?redis
啟動redis
[root@localhost?/]# /usr/bin/redis-cli
Could?not?connect?to?Redis?at?127.0.0.1:6379: Connection?refused
Could?not?connect?to?Redis?at?127.0.0.1:6379: Connection?refused not?connected>
[root@localhost?/]# systemctl?start?redis
測試redis
redis服務端和redis客戶端?啟動redis客戶端
[root@localhost?/]# cd?/usr/bin/?????[root@localhost?/]# which?redis-cli /usr/bin/redis-cli
[root@localhost?bin]# ./redis-cli
127.0.0.1:6379> set?akey?avalue OK
127.0.0.1:6379> get?akey?"avalue"
127.0.0.1:6379> quit
[root@localhost?bin]#?pwd?/usr/bin
[root@localhost?bin]#
HttpRedis
修改配置文件
mValue[root@localhost?conf]# cat?nginx-httpredis.conf?worker_processes??1;
events?{
worker_connections??1024;
}
http?{
include???????mime.types;
default_type??application/octet-stream;
sendfile????????on;
keepalive_timeout??65;
server?{
listen???????80;
server_name??www.cpf.com;
root?html;
index?index.html;
location?/ {
default_type?text/plain;
set?$redis_key?"m";
redis_pass?127.0.0.1:6379;
error_page 404?=?@fetch;?}
location?@fetch?{
root?html;?}
}
}
[root@localhost?conf]#
以這個配置文件啟動
[root@localhost?conf]# /usr/local/openresty/nginx/sbin/nginx?-p /usr/local/openresty/nginx/ -c /usr/local/o?測試一下:
1。?redis中沒有?key為m的 鍵值對。
[root@localhost?conf]# curl?http://localhost/1.html
I?am?1?html
2。我們通過redis,設置key為m的value是:"mValue"。(m=mValue)
[root@localhost?conf]# curl?http://localhost/1.html?mValue
擴展:
用于降級。
HttpRedis2Module
[root@localhost?conf]# cat?nginx-httpRedis2Module.conf?worker_processes??1;
events?{
worker_connections??1024;
}
http?{
include???????mime.types;
default_type??application/octet-stream;
sendfile????????on;
keepalive_timeout??65;
server?{
listen???????80;
server_name??www.cpf.com;
root?html;
index?index.html;
location?/get?{
set_unescape_uri?$key?'n';
redis2_query?get?$key;
redis2_pass?127.0.0.1:6379; }
location?/set?{
set_unescape_uri?$key?'n';
redis2_query set?$key?'nValue';
redis2_pass?127.0.0.1:6379; }
}
}
[root@localhost?conf]#
重啟
[root@localhost?conf]# /usr/local/openresty/nginx/sbin/nginx?-p /usr/local/openresty/nginx/ -c /usr/local/o?測試:
[root@localhost?conf]# curl?localhost/get $-1
[root@localhost?conf]# curl?localhost/set +OK
[root@localhost?conf]# curl?localhost/get $7
n1Value
[root@localhost?conf]#