1. 程式人生 > >基於OpenRestry部署nginx+lua實現流量定向分發

基於OpenRestry部署nginx+lua實現流量定向分發

OpenResty® is a full-fledged web platform that integrates the standard Nginx core, LuaJIT, many carefully written Lua libraries, lots of high quality 3rd-party Nginx modules, and most of their external dependencies. It is designed to help developers easily build scalable web applications, web services, and dynamic web gateways.

部署openresty

mkdir -p /usr/servers  
cd /usr/servers/

yum install -y readline-devel pcre-devel openssl-devel gcc

wget http://openresty.org/download/ngx_openresty-1.7.7.2.tar.gz  
tar -xzvf ngx_openresty-1.7.7.2.tar.gz  
cd /usr/servers/ngx_openresty-1.7.7.2/

cd bundle/LuaJIT-2.1-20150120/  
make clean &&
make && make install ln -sf luajit-2.1.0-alpha /usr/local/bin/luajit cd bundle wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz tar -xvf 2.3.tar.gz cd bundle wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz tar -xvf v0.3.0.tar.gz cd /usr/servers/ngx_openresty-
1.7.7.2 ./configure --prefix=/usr/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 make && make install cd /usr/servers/ ll /usr/servers/luajit /usr/servers/lualib /usr/servers/nginx /usr/servers/nginx/sbin/nginx -V 啟動nginx: /usr/servers/nginx/sbin/nginx

nginx+lua開發hello world

vi /usr/servers/nginx/conf/nginx.conf
//在http部分新增:

lua_package_path "/usr/servers/lualib/?.lua;;";  
lua_package_cpath "/usr/servers/lualib/?.so;;"; 
include lua.conf;


----------


/usr/servers/nginx/conf下,建立一個lua.conf

server {  
    listen       80;  
    server_name  _;  
    location /lua {  
    default_type 'text/html';  
    content_by_lua 'ngx.say("hello world")';  
} 
}


----------
//驗證配置檔案是否正確
/usr/servers/nginx/sbin/nginx -t

//重新nginx載入配置
/usr/servers/nginx/sbin/nginx -s reload  

//訪問http: 
http://192.168.1.107/lua

//檢視異常日誌
tail -f /usr/servers/nginx/logs/error.log

構建完整的nginx+lua專案結構


hello
    hello.conf     
    lua              
      hello.lua
    lualib            
      *.lua
      *.so
/usr/servers/nginx/conf/nginx.conf
//在http下加入  
lua_package_path "/usr/hello/lualib/?.lua;;";  
lua_package_cpath "/usr/hello/lualib/?.so;;";  
include /usr/hello/hello.conf;

cp -r /usr/servers/lualib/ /usr/hello/

/usr/hello/hello.conf
server {  
    listen       80;  
    server_name  _;  

    location /lua {  
        default_type 'text/html';   
        content_by_lua_file /usr/hello/lua/hello.lua;  
    }  
}

/usr/hello/lua/hello.lua  
ngx.say("hello world!!!"); 

編寫lua指令碼來實現流量定向分發(分發nginx)

cd /usr/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 


----------


vi /usr/hello/lua/hello.lua

local uri_args = ngx.req.get_uri_args()
local productId = uri_args["productId"]

local host = {"192.168.1.107", "192.168.1.104"}
local hash = ngx.crc32_long(productId)
hash = (hash % 2) + 1  
backend = "http://"..host[hash]

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() 

應用nginx+lua實現

//安裝相應的元件:

cd /usr/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

//nginx利用獲取到的資料,動態渲染網頁模板:

mkdir /usr/hello/lualib/resty/html
cd /usr/hello/lualib/resty/html
wget https://raw.githubusercontent.com/bungle/lua-resty-template/master/lib/resty/template/html.lua

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

vi /usr/hello/hello.conf
set $template_location "/templates";  
set $template_root "/usr/hello/templates";

vi /usr/servers/nginx/conf/nginx.conf
//在http下面新增
lua_shared_dict my_cache 128m;   //設定nginx快取容量,儲存滿後會使用LRU清除演算法

mkdir /usr/hello/templates
cd /usr/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/>

//新增業務lua:
//當請求由分發nginx過來之後,首先檢視nginx是否有對應的快取資料,如果有則直接返回,否則發起http請求去獲取(redis叢集)資料,然後設定到nginx快取中並返回

local uri_args = ngx.req.get_uri_args()
local productId = uri_args["productId"]
local shopId = uri_args["shopId"]

local cache_ngx = ngx.shared.my_cache

local productCacheKey = "product_info_"..productId
local shopCacheKey = "shop_info_"..shopId

local productCache = cache_ngx:get(productCacheKey)
local shopCache = cache_ngx:get(shopCacheKey)

if productCache == "" or productCache == nil then
    local http = require("resty.http")
    local httpc = http.new()

    local resp, err = httpc:request_uri("http://192.168.1.101:8080",{
        method = "GET",
        path = "/getProductInfo?productId="..productId
    })

    productCache = resp.body
    cache_ngx:set(productCacheKey, productCache, 10 * 60)
end

if shopCache == "" or shopCache == nil then
    local http = require("resty.http")
    local httpc = http.new()

    local resp, err = httpc:request_uri("http://192.168.1.101:8080",{
        method = "GET",
        path = "/getShopInfo?shopId="..shopId
    })

    shopCache = resp.body
    cache_ngx:set(shopCacheKey, shopCache, 10 * 60)
end

local cjson = require("cjson")
local productCacheJSON = cjson.decode(productCache)
local shopCacheJSON = cjson.decode(shopCache)

local context = {
    productId = productCacheJSON.id,
    productName = productCacheJSON.name,
    productPrice = productCacheJSON.price,
    productPictureList = productCacheJSON.pictureList,
    productSpecification = productCacheJSON.specification,
    productService = productCacheJSON.service,
    productColor = productCacheJSON.color,
    productSize = productCacheJSON.size,
    shopId = shopCacheJSON.id,
    shopName = shopCacheJSON.name,
    shopLevel = shopCacheJSON.level,
    shopGoodCommentRate = shopCacheJSON.goodCommentRate
}

local template = require("resty.template")
template.render("product.html", context)