Openresty增强版Nginx--整合Lua访问Redis

Openresty

OpenResty是一个基于Nginx与Lua的高性能Web平台,其内部集成了大量精良的Lua库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态Web应用、Web服务和动态网关。

安装

预编译安装

以CentOS举例 其他系统参照:http://openresty.org/cn/linux-packages.html

你可以在你的 CentOS 系统中添加 openresty 仓库,这样就可以便于未来安装或更新我们的软件包(通过 yum update 命令)。运行下面的命令就可以添加我们的仓库:

 yum install yum-utils

 yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo

然后就可以像下面这样安装软件包,比如 openresty:

 yum install openresty

如果你想安装命令行工具 resty,那么可以像下面这样安装 openresty-resty 包:

 sudo yum install openresty-resty

源码编译安装

下载

http://openresty.org/cn/download.html

./configure

然后在进入 openresty-VERSION/目录, 然后输入以下命令配置:

./configure

默认, --prefix=/usr/local/openresty 程序会被安装到/usr/local/openresty目录。

依赖 gcc openssl-devel pcre-devel zlib-devel

安装:yum install gcc openssl-devel pcre-devel zlib-devel postgresql-devel

启动

systemctl start openstry.service

整合Lua脚本

在openresty/nginx/conf的nginx.conf文件中配置

location /lua{
    default_type text/html;
    content_type_lua_file conf/lua/hello.lua;
}

创建hello.lua文件 vim hello.lua

ngx.say("<p>Hello,World!</p>");

整合Redis模块redis2-nginx-module

首先启动redis

然后配置nginx.conf文件

location = /foo {

default_type text/html;

     redis2_query auth 123123; # 设置密码

     set $value 'first';  #定义一个变量

     redis2_query set one $value;  #redis设置一个key

     redis2_pass 192.168.1.107:6379;

 }
location = /get {

default_type text/html;

     redis2_pass 192.168.1.107:6379;

     redis2_query auth 123123;

     set_unescape_uri $key $arg_key;  # this requires ngx_set_misc

     redis2_query get $key;

}

访问keyhttp://192.168.1.107/get?key=one

# GET /set?key=one&val=first%20value

location = /set {

default_type text/html;

redis2_pass 127.0.0.1:6379;

redis2_query auth 123123;
 

     set_unescape_uri $key $arg_key;  # this requires ngx_set_misc

     set_unescape_uri $val $arg_val;  # this requires ngx_set_misc

     redis2_query set $key $val;

 }

pipeline

     set $value 'first';

     redis2_query set one $value;

     redis2_query get one;

     redis2_query set one two;

     redis2_query get one;

redis2_query del key1;

list

    redis2_query lpush key1 C;     redis2_query lpush key1 B;     redis2_query lpush key1 A; redis2_query lrange key1 0 -1;

工具lua-resty-redis

引入lua-resty-redis

#在conf/nginx.conf配置文件中
location = /luaresty {
    default_type text/html;
    content_by_lua_file lua/luaresty.lua;
}

创建luaresty.lua文件vi luaresty.lua

local redis =require "resty.redis"
local red=redis.new();

local ok,err =red:connect("127.0.0.1",6379)
    if not ok then 
        ngx.say("faild to connect: ",err)
        return 
        end
 ok, err = red:set("dog", "an animal")
     if not ok then
         ngx.say("failed to set dog: ",err)
         return 
         end
  ngx.say("set result: ",ok)
      local res, err = red:get("dog")
      if not res then
          ngx.say("failed to get dog: ",err)
          return
          end
          
     if res == ngx.null then
         ngx.say("dog not found.")
         return
        end
      ngx.say("dog: ",res)

keepalive

red:set_keepalive(max_idle_timeout, pool_size)

组件lua-resty-http

下载openresty的lua组件 lua-resty-http

是一个http客户端

wget https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http_headers.lua

wget https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http.lua

放在/usr/local/openresty/lualib/resty目录下

http模块加入dns服务器

resolver 8.8.8.8;
#在nginx.conf文件中配置

在conf/lua/lb.lua

local http = require("resty.http")  

local httpc = http.new()  
  
local resp, err = httpc:request_uri("http://www.sogou.com", {  
    method = "GET",  
    path = "/sogou?query=resty.http",  
    headers = {  
        ["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36"  
    }  
})  
  
if not resp then  
    ngx.say("request error :", err)  
    return  
end  
  
 
ngx.status = resp.status  
  
  
for k, v in pairs(resp.headers) do  
    if k ~= "Transfer-Encoding" and k ~= "Connection" then  
        ngx.header[k] = v  
    end  
end  
  
ngx.say(resp.body)  
  
httpc:close()

在nginx.conf文件中

location = /lb {
        default_type text/html;
        content_by_lua_file lua/lb.lua;
     }

重启openresty

systemctl restart openresty.service 

访问

http://192.168.1.107/lb

lua-resty-http实现一致性hash负载均衡

local http = require("resty.http")  
local httpc = http.new()  

local hosts = {"192.168.150.111","192.168.150.112"}

local item_id= ngx.var.arg_id

local id_hash = ngx.crc32_long(item_id)
local index = (id_hash % 2) +1
  
local resp, err = httpc:request_uri("http://"..hosts[index], {  
    method = "GET",  
    path = "/sogou?query=resty.http",  
    headers = {  
        ["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36"  
    }  
})  
  
if not resp then  
    ngx.say("request error :", err)  
    return  
end  
  
 
ngx.say(resp.body)  
  
httpc:close()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值