博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基于OpenRestry部署nginx+lua实现流量定向分发
阅读量:4291 次
发布时间:2019-05-27

本文共 6408 字,大约阅读时间需要 21 分钟。

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 gccwget 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/luajitcd bundle  wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz  tar -xvf 2.3.tar.gz  cd bundlewget 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.confserver {      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.confserver {      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.lualocal 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="..productIdlocal 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 endngx.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/htmlcd /usr/hello/lualib/resty/htmlwget https://raw.githubusercontent.com/bungle/lua-resty-template/master/lib/resty/template/html.lua//在hello.conf的server中配置模板的位置:vi /usr/hello/hello.confset $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/templatescd /usr/hello/templatesvi product.htmlproduct id: {* productId *}
product name: {* productName *}
product picture list: {* productPictureList *}
product specification: {* productSpecification *}
product service: {* productService *}
product color: {* productColor *}
product size: {* productSize *}
shop id: {* shopId *}
shop name: {* shopName *}
shop level: {* shopLevel *}
shop good cooment rate: {* shopGoodCommentRate *}
//添加业务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_cachelocal productCacheKey = "product_info_"..productIdlocal shopCacheKey = "shop_info_"..shopIdlocal 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)endif 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)endlocal 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)

转载地址:http://whrgi.baihongyu.com/

你可能感兴趣的文章
react之setState解析
查看>>
elasticsearch7.3版本已经不需要额外安装中文分词插件了
查看>>
【重大好消息】elasticsearch 7.3版本已经可以免费使用x-pack就可以设置账号和密码了,让你的数据不再裸奔
查看>>
解决使用logstash中jdbc导入mysql中的数据到elasticsearch中tinyint类型被转成布尔型的问题的方法
查看>>
elasticsearch7.3版本环境搭建(一)elasticsearch安装和配置
查看>>
SEO基本功:站内优化的一些基本手段
查看>>
centos6系列和7系列如何对外开放80,3306端口号或者其他端口号
查看>>
为什么您宁愿吃生活的苦,也不愿吃学习的苦?为什么你不愿意去学习呢
查看>>
解决elasticsearch7.3版本安装过程中遇到的包括内存不够、线程不够等问题
查看>>
日常项目测试用例检查点(来自一线测试人员的吐血总结)
查看>>
网站建设之域名注册和域名备案
查看>>
解决bootstrap时间输入框总被浏览器记住的记录遮挡住的问题
查看>>
git将一个分支完全覆盖另外一个分支如:dev分支代码完全覆盖某一个开发分支
查看>>
elasticsearch7.3版本环境搭建(二)可视化管理后台kibana的安装和配置
查看>>
elasticsearch7.3版本环境搭建(三)可视化管理后台kibana的汉化(设置中文界面)
查看>>
记录一次DDos攻击实战
查看>>
分享一首小诗--《致程序员》
查看>>
为什么百度只抓取了首页而不抓取我的网站的内页的原因分析
查看>>
年薪170万的阿里P8级员工征婚有感--话说阿里真有钱,这员工要求的条件真多
查看>>
又是一年桂花飘香时,祝我们伟大的祖国70年华诞更加繁荣昌盛,祝大家国庆节快乐
查看>>