CentOS 安装OpenResty(Nginx+Lua)开发环境搭建及Lua从动态渲染网页模板及从Redis获取数据

编写日期:2018-03-05

编写作者:mtsbv110

邮    箱:2532874889@qq.com

 

主机规划:192.168.9.131

ngx_openresty版本

ngx_openresty-1.7.7.2.tar.gz

安装目录

/usr/local/servers  

ngx_cache_purge

2.3.tar.gz

安装目录

/usr/local/servers/ngx_openresty-1.7.7.2/bundle

nginx_upstream_check_module

v0.3.0.tar.gz

安装目录

/usr/local/servers/ngx_openresty-1.7.7.2/bundle

http_headers.lua

http_headers.lua

安装目录

/usr/local/applications/hello/lualib/resty

http.lua

http.lua

安装目录

/usr/local/applications/hello/lualib/resty

template.lua

template.lua

安装目录

/usr/local/applications/hello/lualib/resty

html.lua

html.lua

安装目录

/usr/local/applications/hello/lualib/resty

 

主机规划:192.168.9.132

ngx_openresty版本

ngx_openresty-1.7.7.2.tar.gz

安装目录

/usr/local/servers  

ngx_cache_purge

2.3.tar.gz

安装目录

/usr/local/servers/ngx_openresty-1.7.7.2/bundle

nginx_upstream_check_module

v0.3.0.tar.gz

安装目录

/usr/local/servers/ngx_openresty-1.7.7.2/bundle

http_headers.lua

http_headers.lua

安装目录

/usr/local/applications/hello/lualib/resty

http.lua

http.lua

安装目录

/usr/local/applications/hello/lualib/resty

template.lua

template.lua

安装目录

/usr/local/applications/hello/lualib/resty

html.lua

html.lua

安装目录

/usr/local/applications/hello/lualib/resty

 

主机规划:192.168.9.133

ngx_openresty版本

ngx_openresty-1.7.7.2.tar.gz

安装目录

/usr/local/servers  

ngx_cache_purge

2.3.tar.gz

安装目录

/usr/local/servers/ngx_openresty-1.7.7.2/bundle

nginx_upstream_check_module

v0.3.0.tar.gz

安装目录

/usr/local/servers/ngx_openresty-1.7.7.2/bundle

http_headers.lua

http_headers.lua

安装目录

/usr/local/applications/hello/lualib/resty

http.lua

http.lua

安装目录

/usr/local/applications/hello/lualib/resty

 

 

一、部署应用层openresty(192.168.9.131)

 

yum install -y gcc gcc-c++ make auotmake autoconf libtool readline-devel pcre pcre-devel zlib zlib-devel openssl openssl-devel

 

目录规划

[root@jetsen-zk-01 ~]# mkdir -p /usr/local/servers

上传ngx_openresty-1.7.7.2.tar.gz解压/usr/local/servers

[root@jetsen-zk-01 ngx_openresty-1.7.7.2]# pwd

/usr/local/servers/ngx_openresty-1.7.7.2

[root@jetsen-zk-01 ngx_openresty-1.7.7.2]# cd bundle/LuaJIT-2.1-20150120/

[root@jetsen-zk-01 LuaJIT-2.1-20150120]# make clean && make && make install 

[root@jetsen-zk-01 LuaJIT-2.1-20150120]#

 ln -sf luajit-2.1.0-alpha /usr/local/bin/luajit

 

cd bundle  

[root@jetsen-zk-01 bundle]#

 wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz

[root@jetsen-zk-01 bundle]# tar -zxvf 2.3.tar.gz

 

cd bundle  

[root@jetsen-zk-01 bundle]#

 wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz

[root@jetsen-zk-01 bundle]# tar -zxvf v0.3.0.tar.gz

 

[root@jetsen-zk-01 bundle]# cd /usr/local/servers/ngx_openresty-1.7.7.2  

[root@jetsen-zk-01 ngx_openresty-1.7.7.2]# ./configure --prefix=/usr/local/servers --with-http_realip_module  --with-pcre  --with-luajit --add-module=./bundle/ngx_cache_purge-2.3/ --add-module=./bundle/nginx_upstream_check_module-0.3.0/ -j2

 

[root@jetsen-zk-01 ngx_openresty-1.7.7.2]# make && make install

 

[root@jetsen-zk-01 servers]# cd /usr/local/servers/

[root@jetsen-zk-01 servers]# /usr/local/servers/nginx/sbin/nginx -V

二、nginx+lua开发的hello world

在http部分添加:

 lua_package_path "/usr/local/servers/lualib/?.lua;;";

 lua_package_cpath "/usr/local/servers/lualib/?.so;;";

在/usr/local/servers/nginx/conf下,创建一个lua.conf

[root@jetsen-zk-01 conf]# vi lua.conf

server {

    listen       80;

    server_name  _;

 

    location /lua {

      default_type 'text/html';

      #content_by_lua 'ngx.say("hello world")';

      content_by_lua_file conf/lua/test.lua;

    }

 

}

在nginx.conf的http部分添加:

include lua.conf;

/usr/servers/nginx/sbin/nginx -t  

 

重新nginx加载配置

/usr/servers/nginx/sbin/nginx -s reload

 

创建lua目录 /usr/local/server/nginx/conf/lua

创建test.lua文件

ngx.say("hello world");

 

重新nginx加载配置

 

/usr/servers/nginx/sbin/nginx -s reload  

http://192.168.9.131/lua

 

  1. 工程化的nginx+lua项目结构

hello

    hello.conf     

    lua              

      hello.lua

    lualib            

      *.lua

      *.so

 

放在/usr/local/application/hello目录下

[root@jetsen-zk-01 local]# mkdir -p /usr/local/applications/hello

[root@jetsen-zk-01 local]# mkdir -p /usr/local/applications/hello/lua

分别在对应的目录中创建对应的hello.conf和hello.lua

[root@jetsen-zk-01 hello]# cat hello.conf

server {  

    listen       80;  

    server_name  _;  

  

    location /hello {  

        default_type 'text/html';  

        #lua_code_cache off;  

        content_by_lua_file /usr/local/applications/hello/lua/hello.lua;  

    }  

}

 

 

[root@jetsen-zk-01 hello]# cat lua/hello.lua

ngx.say("hello world");

 

将lualib 文件夹从/usr/local/servers拷贝到对应的应用程序目录中

[root@jetsen-zk-01 hello]# cp -r /usr/local/servers/lualib/ /usr/local/applications/hello/      

修改nginx.conf配置文件

 

http {

    lua_package_path "/usr/local/applications/hello/lualib/?.lua;;";

    lua_package_cpath "/usr/local/applications/hello/lualib/?.so;;";

    include /usr/local/applications/hello/hello.conf;

 

          

在另外一个机器上,也用OpenResty部署一个nginx

三、配置流量分发Nginx(192.168.9.153)

 

[root@jetsen-zk-03 lualib]# cd /usr/local/applications/hello/lualib

一个流量分发的nginx,会发送http请求到后端的应用nginx上面去,所以要先引入lua http lib包

[root@jetsen-zk-03 lualib]# cd resty/

[root@jetsen-zk-03 resty]#

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

 

[root@jetsen-zk-03 resty]#

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

local uri_args = ngx.req.get_uri_args()

local productId = uri_args["productId"]

 

local hosts = {"192.168.9.131", "192.168.9.132"}

local hash = ngx.crc32_long(productId)

local index = (hash % 2) + 1

backend = "http://"..hosts[index]

 

local requestPath = uri_args["requestPath"]

requestPath = "/"..requestPath.."?productId="..productId

 

local http = require("resty.http")

local httpc = http.new()

 

local resp, err = httpc:request_uri(backend,{

  method = "GET",

  path = requestPath

})

 

if not resp then

  ngx.say("request error: ", err)

  return

end

 

ngx.say(resp.body)

 

httpc:close()

 

http://192.168.9.133/hello?requestPath=hello&productId=0(1,2,3,4,5,6……)

 

四、Nginx最终利用LUA动态渲染网页模板

应用Nginx(192.168.9.131,192.168.9.132)添加Http依赖

[root@jetsen-zk-01(02) resty]#

 

 cd /usr/local/applications/hello/lualib/resty

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

 

安装模板依赖组件

wget https://raw.githubusercontent.com/bungle/lua-resty-template/master/lib/resty/template.lua

wget https://raw.githubusercontent.com/bungle/lua-resty-template/master/lib/resty/template/html.lua

 

配置模板的位置

在hello.conf的server中配置模板位置

 

set $template_location "/templates";  

set $template_root "/usr/local/applications/hello/templates

 

vi product.html

product id: {* productId *}<br/>

product name: {* productName *}<br/>

product picture list: {* productPictureList *}<br/>

product specification: {* productSpecification *}<br/>

product service: {* productService *}<br/>

product color: {* productColor *}<br/>

product size: {* productSize *}<br/>

shop id: {* shopId *}<br/>

shop name: {* shopName *}<br/>

shop level: {* shopLevel *}<br/>

shop good cooment rate: {* shopGoodCommentRate *}<br/>

 

 

 

nginx.conf中:

lua_shared_dict my_cache 128m;

http://192.168.9.133/product?requestPath=product&productId=1&shopId=1

192.168.9.140

/usr/local/applications/distribution

/usr/local/applications/distribution/lua

local cjson = require("cjson")

local redis = require("resty.redis")

local http = require("resty.http")

 

local function close_redis(red)

        if not red then

                return

        end

        local pool_max_idle_time = 10000

        local pool_size = 100

        local ok, err = red:set_keepalive(pool_max_idle_time, pool_size)

        if not ok then

                ngx.say("set keepalive error : ", err)

        end

end

 

local cache_ngx = ngx.shared.my_cache

local uri_args = ngx.req.get_uri_args()

local areaCode = uri_args["areaCode"]

 

 

 

local subjectAreaCodeNginxCacheKey = "subject_nginx_cache_areaCode"..areaCode

local subjectAreaCodeNginxCache = cache_ngx:get(subjectAreaCodeCacheKey)

 

if subjectAreaCodeNginxCache == "" or subjectAreaCodeNginxCache == nil then

--if subjectAreaCodeNginxCache ~= "" or subjectAreaCodeNginxCache ~= nil then

  local red = redis:new()

  red:set_timeout(1000)

  local ip = "192.168.9.140"

  local port = 1112

  local ok, err = red:connect(ip, port)

  if not ok then

    ngx.say("connect to redis error : ", err)

    return close_redis(red)

  end

 

  --local redisResp, redisErr = red:get("k1")

  local redisResp, redisErr = red:get("subject_redis_info_areacode_"..areaCode)

 

  if redisResp == ngx.null then

      local httpc = http.new()

      local resp, err = httpc:request_uri("http://39.105.18.180:9026",{

      method = "GET",

      path = "/jeuc/Api/SubjectInfo/getSubjectNameBySubStages?clientId=84&clientSecret=ab49b91ffec3d3a35a693f76cb122fee&subStages=%E9%AB%98%E4%B8%80&areaCode="..areaCode

    })

    --ngx.say("get redis lua  context : ", resp.body)

    subjectAreaCodeNginxCache = resp.body

    redisResp = resp.body

  end

 

  subjectAreaCodeNginxCache = redisResp

 

  math.randomseed(tostring(os.time()):reverse():sub(1, 7))

  local expireTime = math.random(600, 1200)

 

  cache_ngx:set(subjectAreaCodeNginxCacheKey, subjectAreaCodeNginxCache, expireTime)

end

 

ngx.say("get redis lua  context : ", subjectAreaCodeNginxCache)

 

 

 

--[[ngx.say("connect to redis error : ", areaCode)]]--

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值