29-多级缓存架构-基于OpenResty部署应用层nginx以及nginx+lua开发hello world


我们这里使用nginx,全都会在nginx里去写lua脚本,因为我们需要自定义一些特殊的业务逻辑。

比如说,流量分发,自己用lua去写分发的逻辑,在分发层nginx里去写的

再比如说,要用lua去写多级缓存架构存取的控制逻辑,在应用层nginx里去写的

后面还要做热点数据的自动降级机制,也是用lua脚本去写降级机制的,在分发层nginx里去写的

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

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

我们这一章节是去部署应用层nginx,会采用OpenResty的方式去部署nginx,而且会带着大家写一个nginx+lua开发的一个hello world

1、部署第一个nginx,作为应用层nginx(01那个机器上)

(1)部署openresty

OpenResty(又称:ngx_openresty) 是一个基于 NGINX 的可伸缩的 Web 平台,由中国人章亦春发起,提供了很多高质量的第三方模块。

OpenResty 是一个强大的 Web 应用服务器,Web 开发人员可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,更主要的是在性能方面,OpenResty可以 快速构造出足以胜任 10K 以上并发连接响应的超高性能 Web 应用系统。

//创建文件位置
mkdir -p /opt/openrestydir
cd /opt/openrestydir
#OpenResty 依赖库有: perl 5.6.1+, libreadline, libpcre, libssl。
#所以我们需要先安装好这些依赖库
yum install -y readline-devel pcre-devel openssl-devel gcc
#接下我们可以在官方(https://openresty.org/cn/)下载最新的 OpenResty 源码包并解压编#译安装:

cd /opt/openrestydir
wget https://openresty.org/download/ngx_openresty-1.9.7.1.tar.gz 
tar -xzvf ngx_openresty-1.9.7.1.tar.gz  
cd /opt/openrestydir/ngx_openresty-1.9.7.1.tar.gz
# 装lua得jit

cd /opt/openrestydir/ngx_openresty-1.9.7.1/bundle/LuaJIT-2.1-20151219  
make clean && make && make install  
ln -sf luajit-2.1.0-alpha /usr/local/bin/luajit
# 在bundle下安装一些东西

cd /opt/openrestydir/ngx_openresty-1.9.7.1/bundle
wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz  
tar -xvf 2.3.tar.gz  

cd /opt/openrestydir/ngx_openresty-1.9.7.1/bundle
wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz  
tar -xvf v0.3.0.tar.gz 

 

# 最后安装openresty
cd /opt/openrestydir/ngx_openresty-1.9.7.1  
#配置安装位置,和添加一些模块
./configure --prefix=/opt/openrestydir --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 

查看版本
/opt/openrestydir/nginx/sbin -V 

启动nginx: /usr/servers/nginx/sbin/nginx

(2)nginx+lua开发的hello world

vim /opt/openrestydir/nginx/conf/nginx.conf

在http部分添加:

 

/opt/openrestydir/nginx/conf下,创建一个lua.conf

server {  
    listen       80;  
    server_name  _;  
}  

在nginx.conf的http部分添加:

include lua.conf;

验证配置是否正确:

/opt/openrestydir/nginx/sbin/nginx -t

在lua.conf的server部分添加:

location /lua {  
    default_type 'text/html';  
    content_by_lua 'ngx.say("hello world")';  
} 

/opt/openrestydir/nginx/sbin/nginx -t  

重新nginx加载配置

/opt/openrestydir/nginx/sbin/nginx -s reload  

访问http: http://192.168.1.51/lua

 

vi /opt/openrestydir/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 /opt/openrestydir/nginx/logs/error.log

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

项目工程结构

hello
    hello.conf     
    lua              
      hello.lua
    lualib            
      *.lua
      *.so

放在 /opt/openrestydir/hello目录下

 

/opt/openrestydir/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 /opt/openrestydir/hello/hello.conf;  
}  

/opt/openrestydir/hello/hello.conf

server {  
    listen       80;  
    server_name  _;  
  
    location /lua {  
        default_type 'text/html';  
        lua_code_cache off;  
        content_by_lua_file /opt/openrestydir/hello/lua/test.lua;  
    }  
}  

 

如法炮制,在另外一个机器上,也用OpenResty部署一个nginx

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值