前面我们已经使用了nginx,它拥有丰富的模块供我们使用,由于nginx是由c语言编写而成的,所以以前编写模块就必须使用c/c++,后来,有人将lua解释器继承到了nginx中,内建了ngx_lua模块,至此,nginx支持了lua
一、OpenResty
OpenResty是基于nginx开源版本的一个扩展版本,集成了大量的lua库
1. 添加repo
- cd /etc/yum.repos.d/
- wget https://openresty.org/package/centos/openresty.repo
2. 安装openresty
yum install openresty
3. 启动openresty
openresty默认安装路径为/usr/local/openresty/
,其中有个nginx目录
,关于openresty的启动和配置都和以前nginx相同
cd /usr/local/openresty/nginx/conf
修改openresty的http模块
配置,新增server模块
,使用content_by_lua
执行一段lua代码:
- server{
- listen 8080;
- location /{
- default_type text/html;
- content_by_lua 'ngx.say("hello openresty")';
- }
- }
启动openresty:
- cd /usr/local/openresty/nginx/sbin/
- ./nginx -p /usr/local/openresty/nginx/
浏览器访问8080
端口:

二、http访问Redis
上面完成了nginx执行lua语句,接下来来看nginx如何访问Redis
Redis环境搭建可以看之前的文章:分布式--Redis的安装与数据类型的使用
1. 获取Redis数据
下面是通过nginx获取Redis中key
对应的value
1.1 启动Redis
启动一个默认6379
端口的Redis即可,下面还有我之前搭建的Redis集群:

1.2 修改nginx配置
- 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 localhost;
-
- location / {
- default_type text/plain;
- # 设置获取redis中key为m的值
- set $redis_key "m";
- redis_pass 127.0.0.1:6379;
-
- # 如果发生404,就交由@fetch处理
- error_page 404 = @fetch;
- }
-
- location @fetch {
- root html;
- }
-
- }
- }
nginx重新加载配置:
./nginx -p /usr/local/openresty/nginx/ -c /usr/local/openresty/nginx/conf/nginx-http.conf -s reload
1.3 通过http访问
curl http://localhost/1.html
结果:

redis客户端设置m
的键值对:

再次访问:

2. 设置Redis数据
上面方式1,只是获取了Redis的数据,那么如何设置Redis键值对呢?如果nginx支持redis的指令就好了,实际上nginx是支持的
2.1 修改nginx配置
可以使用redis2_query
后面跟上redis的指令,来设置值,同样也能使用redis指令获取值:
- 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 localhost;
-
- location /get {
- # 设置个变量
- set_unescape_uri $key "n";
- redis2_query get $key;
- redis_pass 127.0.0.1:6379;
-
- # 如果发生404,就交由@fetch处理
- error_page 404 = @fetch;
- }
-
- location /set {
- # 设置个变量
- set_unescape_uri $key "n";
- redis2_query set $key "hello2";
- redis2_pass 127.0.0.1:6379;
- }
-
- location @fetch {
- root html;
- }
-
- }
- }
nginx重启:
- ./nginx -s stop
- ./nginx -p /usr/local/openresty/nginx/ -c /usr/local/openresty/nginx/conf/nginx-http2.conf
2.2 访问测试
- curl http://localhost/get
- curl http://localhost/set
结果:

三、lua结合Nginx、Redis
官方介绍:https://github.com/openresty/lua-resty-redis
首先,OpenResty集成lua后,有三种调用lua的方式:
方式语法 | 描述 |
---|---|
xxx_by_lua | 最开始我们已经使用过了,它支持执行一条lua代码 |
xxx_by_lua_file | 该语句支持执行一个lua脚本文件,也是用的最多的 |
xxx_by_lua_block | 该语句支持执行一个lua代码块,针对一些简单场景使用 |
下面来使用lua与Redis进行交互
1. lua操作Redis数据
lua操作Redis,步骤是引入Redis模块,连接Redis,然后再操作
1.1 编写lua脚本
创建个目录存放lua脚本:
- mkdir lua
- vi control_redis.lua
内容为:
- -- 引入redis模块
- local redis = require("resty.redis")
- -- 创建个redis对象
- local red = redis:new()
-
- -- 1. 连接redis
- -- 多参数返回
- local ok,err = red:connect("127.0.0.1",6379)
- if not ok then
- ngx.say("connect failed:",err)
- return
- end
-
- -- 2. 设置redis的键值对
- ok,err = red:set("luaKey","luaValue")
- if not ok then
- ngx.say("set faild:",err)
- return
- end
-
- -- 3. 读取redis的键值对
- ret = red:get("luaKey")
- ngx.say("read luaKey value:",ret)
-
- return
1.2 修改nginx配置
使用content_by_lua_file
指定lua脚本的绝对路径:
- worker_processes 1;
-
- events {
- worker_connections 1024;
- }
-
-
- http {
- include mime.types;
- default_type application/octet-stream;
- sendfile on;
- keepalive_timeout 65;
-
- server {
- listen 8090;
- server_name localhost;
-
- location / {
- default_type text/html;
- content_by_lua_file /usr/local/openresty/nginx/lua/control_redis.lua;
- }
-
- }
- }
重启nginx:
- ./nginx -s stop
- ./nginx -p /usr/local/openresty/nginx/ -c /usr/local/openresty/nginx/conf/nginx-lua.conf
1.3 测试

2. lua获取get请求参数
2.1 编写lua脚本
vi http_get.lua
使用ngx.req.get_uri_args()
获取,内容为:
- -- 返回的是一个table类型
- local args = ngx.req.get_uri_args()
-
-
- for k,v in pairs(args) do
- ngx.say("key:"..k.."value:"..v)
- end
2.2 修改nginx配置
新增端口监听:
- server {
- listen 8091;
- server_name localhost;
-
- location /get {
- default_type text/html;
- content_by_lua_file /usr/local/openresty/nginx/lua/http_get.lua;
- }
- }
重新加载配置文件:
./nginx -p /usr/local/openresty/nginx/ -c /usr/local/openresty/nginx/conf/nginx-lua.conf -s reload
2.3 测试
http://192.168.42.4:8091/get?name=张三&age=19

3. lua获取post请求参数
post请求有两种:body键值对和body请求体。后者对应现在流行的json格式
3.1 编写lua脚本
post请求参数的获取都需要先调用ngx.req.read_body()
方法
键值对:
vi http_post_kv.lua
使用ngx.req.get_post_args()
获取,内容为:
- -- 先读取下
- ngx.req.read_body()
- -- 再获取
- local params = ngx.req.get_post_args()
-
- for k,v in pairs(params) do
- ngx.say("key:"..k.." value:"..v)
- end
请求体:
vi http_post_body.lua
使用ngx.req.get_body_data()
获取,内容为:
- -- 先读取下
- ngx.req.read_body()
- -- 再获取请求体
- local body = ngx.req.get_body_data();
-
- ngx.say(body)
3.2 修改nginx配置
- server {
- listen 8092;
- server_name localhost;
-
- location /post_kv {
- default_type text/html;
- content_by_lua_file /usr/local/openresty/nginx/lua/http_post_kv.lua;
- }
-
- location /post_body {
- default_type text/html;
- content_by_lua_file /usr/local/openresty/nginx/lua/http_post_body.lua;
- }
- }
重新加载nginx配置:
./nginx -p /usr/local/openresty/nginx/ -c /usr/local/openresty/nginx/conf/nginx-lua.conf -s reload
3.3 测试
键值对:

请求体:

4. lua获取请求头
4.1 编写lua脚本
vi http_headers.lua
请求头通过ngx.req.get_headers()
获取,内容为:
- local headers = ngx.req.get_headers()
-
- for k,v in pairs(headers) do
- ngx.say("key:"..k.." value:"..v)
- end
4.2 修改nginx配置
- server {
- listen 8093;
- server_name localhost;
-
- location /headers {
- default_type text/html;
- content_by_lua_file /usr/local/openresty/nginx/lua/http_headers.lua;
- }
- }
重新加载配置文件:
./nginx -p /usr/local/openresty/nginx/ -c /usr/local/openresty/nginx/conf/nginx-lua.conf -s reload
4.3 测试

以上就是openresty+lua+redis的基本使用