因为用nginx+lua去开发,所以会选择用最流行的开源方案,就是用OpenResty

nginx+lua打包在一起,而且提供了包括redis客户端,mysql客户端,http客户端在内的大量的组件

1、部署第一个nginx,作为应用层nginx

(1)部署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

  

(2)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;;"; /usr/servers/nginx/conf下,创建一个lua.conf server {
listen 80;
server_name _;
} 在nginx.conf的http部分添加: include lua.conf; 验证配置是否正确: /usr/servers/nginx/sbin/nginx -t 在lua.conf的server部分添加: 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.31.187/lua vi /usr/servers/nginx/conf/lua/test.lua ngx.say("hello world"); 修改lua.conf location /lua {
default_type 'text/html';
content_by_lua_file conf/lua/test.lua;
}

  

查看异常日志

tail -f /usr/servers/nginx/logs/error.log

(3)工程化的nginx+lua项目结构

项目工程结构

hello
hello.conf
lua
hello.lua
lualib
*.lua
*.so 放在/usr/hello目录下 /usr/servers/nginx/conf/nginx.conf worker_processes 2; error_log logs/error.log; events {
worker_connections 1024;
} http {
include mime.types;
default_type text/html; lua_package_path "/usr/hello/lualib/?.lua;;";
lua_package_cpath "/usr/hello/lualib/?.so;;";
include /usr/hello/hello.conf;
} /usr/hello/hello.conf server {
listen 80;
server_name _; location /lua {
default_type 'text/html';
lua_code_cache off;
content_by_lua_file /usr/example/lua/test.lua;
}
}

  用eshop-cache01和eshop-cache02作为应用层nginx服务器,用eshop-cache03作为分发层nginx

在eshop-cache03,也就是分发层nginx中,编写lua脚本,完成基于商品id的流量分发策略

步骤:

1、获取请求参数,比如productId
2、对productId进行hash
3、hash值对应用服务器数量取模,获取到一个应用服务器
4、利用http发送请求到应用层nginx
5、获取响应后返回

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

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

  代码:

 

local uri_args = ngx.req.get_uri_args()
local productId = uri_args["productId"] local hosts = {"192.168.31.187", "192.168.31.19"}
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()

  

分发层nginx,lua应用,会将商品id,商品店铺id,都转发到后端的应用nginx
位置:/usr/servers/nginx/sbin/nginx -s reload

 

1、应用nginx的lua脚本接收到请求
2、获取请求参数中的商品id,以及商品店铺id
3、根据商品id和商品店铺id,在nginx本地缓存中尝试获取数据
4、如果在nginx本地缓存中没有获取到数据,那么就到redis分布式缓存中获取数据,如果获取到了数据,还要设置到nginx本地缓存中
但是这里有个问题,建议不要用nginx+lua直接去获取redis数据
因为openresty没有太好的redis cluster的支持包,所以建议是发送http请求到缓存数据生产服务,由该服务提供一个http接口
缓存数生产服务可以基于redis cluster api从redis中直接获取数据,并返回给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

  

5、如果缓存数据生产服务没有在redis分布式缓存中没有获取到数据,那么就在自己本地ehcache中获取数据,返回数据给nginx,也要设置到nginx本地缓存中
6、如果ehcache本地缓存都没有数据,那么就需要去原始的服务中拉去数据,该服务会从mysql中查询,拉去到数据之后,返回给nginx,并重新设置到ehcache和redis中
7、nginx最终利用获取到的数据,动态渲染网页模板
cd /usr/hello/lualib/resty/
wget https://raw.githubusercontent.com/bungle/lua-resty-template/master/lib/resty/template.lua
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中配置模板位置

set $template_location "/templates";
set $template_root "/usr/hello/templates"; mkdir /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/>

  

8、将渲染后的网页模板作为http响应,返回给分发层nginx
hello.conf中:

lua_shared_dict my_cache 128m;

  lua脚本中:

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.31.179: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.31.179: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)

  

第一次访问的时候,其实在nginx本地缓存中是取不到的,所以会发送http请求到后端的缓存服务里去获取,会从redis中获取

拿到数据以后,会放到nginx本地缓存里面去,过期时间是10分钟

然后将所有数据渲染到模板中,返回模板

以后再来访问的时候,就会直接从nginx本地缓存区获取数据了

缓存数据生产 -> 有数据变更 -> 主动更新两级缓存(ehcache+redis)-> 缓存维度化拆分

分发层nginx + 应用层nginx -> 自定义流量分发策略提高缓存命中率

nginx shared dict缓存 -> 缓存服务 -> redis -> ehcache -> 渲染html模板 -> 返回页面

如果你的数据在nginx -> redis -> ehcache三级缓存都不在了,可能就是被LRU清理掉了

这个时候缓存服务会重新拉去数据,去更新到ehcache和redis中

OpenResty部署nginx及nginx+lua的更多相关文章

  1. CentOS6.4 安装OpenResty和Redis 并在Nginx中利用lua简单读取Redis数据

    1.下载OpenResty和Redis OpenResty下载地址:wget http://openresty.org/download/ngx_openresty-1.4.3.6.tar.gz Re ...

  2. OpenResty / Nginx模块,Lua库和相关资源的列表

    OpenResty / Nginx模块,Lua库和相关资源的列表 什么是OpenResty OpenResty是一个成熟的网络平台,它集成了标准的Nginx核心,LuaJIT,许多精心编写的Lua库, ...

  3. openresty HTTP status constants nginx api for lua

    https://github.com/openresty/lua-nginx-module context: init_by_lua, set_by_lua, rewrite_by_lua, acce ...

  4. Nginx基础 - Nginx+Lua实现灰度发布与WAF

    1.Nginx加载Lua环境默认情况下Nginx不支持Lua模块, 需要安装LuaJIT解释器, 并且需要重新编译Nginx, 建议使用openrestry 1)环境准备 [root@localhos ...

  5. nginx添加编译lua模块

    一 .安装LuaJit 1.下载LuaJit # wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz 2.编译安装 # tar xzvf LuaJI ...

  6. Nginx 内嵌lua脚本,结合Redis使用

    0x00 Nginx 内嵌Lua脚本有下面特点: 20k个并发连接 Lua脚本能够在Nignx 11个层次的不同层次发挥作用,扩展Ngnix功能 Lua速度极快(寄存器指令) 0x01 应用场景 在w ...

  7. openresty如何完美替换nginx

    下载openresty wget https://openresty.org/download/openresty-1.15.8.1.tar.gz tar zxvf openresty-1.15.8. ...

  8. 安装nginx环境(含lua)时遇到报错ngx_http_lua_common.h:20:20: error: luajit.h: No such file or directory的解决

    下面是安装nginx+lua环境时使用的相关模块及版本,ngx_devel_kit和lua-nginx-module模块用的都是github上最新的模块.并进行了LuaJIT的安装. #Install ...

  9. HappyAA服务器部署笔记1(nginx+tomcat的安装与配置)

    这是本人的服务器部署笔记.文章名称叫"部署笔记1"的原因是之后我对这个进行了改进之后,会有"部署笔记2","部署笔记3"...循序渐进,估计 ...

随机推荐

  1. 【NOI2002】荒岛野人(信息学奥赛一本通 1637)(洛谷 2421)

    题目描述 克里特岛以野人群居而著称.岛上有排列成环行的M个山洞.这些山洞顺时针编号为1,2,…,M.岛上住着N个野人,一开始依次住在山洞C1,C2,…,CN中,以后每年,第i个野人会沿顺时针向前走Pi ...

  2. GCC编译UDF和gdb调试UDF

    Fluent版本:19.0 前面我们介绍过使用VS来编译和调试UDF,其实我们也可以用GCC来编译UDF.gdb调试UDF.本次介绍的方法更具有通用性,也适用于Linux下Fluent的UDF编译和调 ...

  3. 经典算法(四) 数组相关 & 螺旋矩阵 & 数字大小写转换 & 字符串相关

    一.求所有子数组的和的最大值 public static void main(String[] args) { int[] a = { 1, -2, 3, 10, -4, 7, 2, -5 }; Fi ...

  4. Xshell远程登录

    1.xshell由一台服务器a登录另一台服务器b sftp -oPort = root@ip 2.下载git /上传pull git b服务器想下载的目标文件目录 a服务器上的文件下载目录

  5. Gamma阶段第二次scrum meeting

    每日任务内容 队员 昨日完成任务 明日要完成的任务 张圆宁 #91 用户体验与优化https://github.com/rRetr0Git/rateMyCourse/issues/91(持续完成) # ...

  6. 【Beta】测试报告

    测试计划 一.对新增加的用户注册.登录及访问控制的测试 注册信息的填写 用户名包含纯大小写字母.数字.中文.特殊字符及几种情况的混合 密码包含大小写字母.数字和特殊字符 用户名长度不大于150个字节 ...

  7. Java的string类为什么是不可变的

    最流行的Java面试题之一就是:什么是不可变对象(immutable object),不可变对象有什么好处,在什么情况下应该用,或者更具体一些,Java的String类为什么要设成immutable类 ...

  8. FTO Obesity Variant Circuitry and Adipocyte Browning in Humans

    好文献非常难得,提供了核心的研究思路. FTO Obesity Variant Circuitry and Adipocyte Browning in Humans - 这篇文章需要好好的解析 为深入 ...

  9. 福昕PDF高级企业版编辑器9.5 Foxit PhantomPDF Business安装破解教程

    title: "福昕PDF高级企业版编辑器9.5 Foxit PhantomPDF Business安装破解教程" categories: soft tags: soft auth ...

  10. 阿里云mysql数据库恢复到本地

    本地环境为win10,mysql引擎为InnoDB 第一步:服务里面停掉mysql 第二步:把my.ini 的 innodb_force_recovery  设置为0 第三步:把.frm和.idb文件 ...