基于nginx的OpenResty提供的整合了lua模块,支撑用lua脚本语言快速开发高性能的低开销的应用程序能力,OpenResty也被用于支撑Kong等API接入网关,但市面上比较少有实现的web框架
通过《OpenResty完全开发指南:构建百万级别并发的Web应用》文章学习,搭建了lua web应用程序框架在实际项目中使用,实际运行稳定,高性能,资源占用少,因此开发出来给大家分享。
建议源码编译openresty安装OpenRestry,在IDEA中集成调测通过,源码打包在最后附件中
一、源码编译openresty,当然也可以安装预编译包
1.创建目录
mkdir -p $HOME/nginx/src/package
2.安装c/c++编译器
sudo apt-get install build-essential
3.下载依赖库
cd $HOME/nginx/src/package
wget https://www.openssl.org/source/openssl-1.0.2k.tar.gz
wget https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz
wget http://zlib.net/zlib-1.2.11.tar.gz
wget https://openresty.org/download/openresty-1.19.3.1.tar.gz
#rtmp可选(或者手工下载指定分支如master的zip包)
git clone https://github.com/arut/nginx-rtmp-module.git --depth=1
4.解压缩源码包
cd $HOME/nginx/src
tar -zxvf ./package/openresty-1.19.3.1.tar.gz
tar -zxvf ./package/openssl-1.0.2k.tar.gz
tar -zxvf ./package/pcre-8.42.tar.gz
tar -zxvf ./package/zlib-1.2.11.tar.gz
unzip ./package/nginx-rtmp-module-master.zip
5.配置编译选项
cd $HOME/nginx/src/openresty-1.19.3.1
./configure --prefix=${HOME}/nginx/openresty \
--user=maxl --group=maxl \
--with-debug \
--with-cc-opt="-O2 -g" \
--with-http_stub_status_module \
--with-stream_ssl_module \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_iconv_module \
--with-pcre-jit \
--with-openssl=../openssl-1.0.2k \
--with-pcre=../pcre-8.42 \
--with-zlib=../zlib-1.2.11 \
--with-http_flv_module --with-http_mp4_module --add-module=../nginx-rtmp-module-master
5.编辑且安装
make && make install
6.裁剪nginx编译大小
cd ${HOME}/nginx/openresty/nginx/sbin && cp nginx nginx.bak && strip nginx
7.权限调整,普通用户能够有nginx master权限(可选)
cd ${HOME}/nginx/openresty/nginx/sbin
1)所有用户都可以运行(因为是755权限,文件所有者:root,组所有者:root)
sudo chown root:root nginx
sudo chmod 755 nginx
sudo chmod u+s nginx
2)仅root用户和maxl 用户可以运行(因为是750权限,文件所有者:root,组所有者:maxl)
sudo chown root:maxl nginx
sudo chmod 750 nginx
#就是给某个程序的所有者以suid权限,可以像root用户一样操作
sudo chmod u+s nginx
8.rtmp推流的扩展支持(可选)
ffmpeg推流准备
下载依赖包
wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
wget https://johnvansickle.com/ffmpeg/release-source/ffmpeg-4.1.tar.xz
1).yasm安装
yasm
tar zxvf yasm-1.3.0.tar.gz
cd yasm-1.3.0
./configure
make
sudo make install
2).ffmpeg安装
tar xvJf ffmpeg-4.1.tar.xz
cd ffmpeg-4.1
./configure --enable-shared --prefix=/usr/local/ffmpeg
make
sudo make install
新增文件/usr/local/ffmpeg/lib/ffmepg-libs.conf添加/usr/local/ffmpeg/lib/
ldconfig
然后把/usr/local/openresty/bin加入PATH搜索路径中
二、开发环境idea的nginx插件安装
1.idea 插件安装: OpenResty Lua Support,Lua,Nginx Support (若国内无法下载,请自行手工到idea官方进行下载)
大致思路:配置nginx_server(即openresty路径下nginx) 在idea上编写lua脚本,通过ant将代码及配置文件复制到openresty路径下
然后重启
2.创建idea openresy工程
工程模块中添加conf,src目录,新建ant的xml文件build.xml内容为
<?xml version="1.0" encoding="UTF-8"?>
<!-- 项目路径 -->
<project name="test" default="copy" basedir=".">
<description>
openresty app
</description>
<!-- set global properties for this build -->
<property name="target-home" location="${HOME}/nginx/work"/>
<property name="target-bin" location="${target-home}/bin"/>
<property name="target-conf" location="${target-home}/conf"/>
<property name="target-html" location="${target-home}/html"/>
<property name="target-logs" location="${target-home}/logs"/>
<property name="target-service" location="${target-home}/service"/>
<property name="bin" location="${basedir}/bin"/>
<property name="conf" location="${basedir}/conf"/>
<property name="html" location="${basedir}/html"/>
<property name="logs" location="${basedir}/logs"/>
<property name="service" location="${basedir}/service"/>
<target name="clean" depends="">
<echo>清理openresty目录 ${dist}下的conf,logs,service,bin</echo>
<delete dir="${target-home}"/>
</target>
<target name="init" depends="clean">
<!--<echo>创建安装目录</echo>-->
<mkdir dir="${target-home}"/>
</target>
<target name="copy" depends="init" description="generate the distribution" >
<echo>复制安装文件</echo>
<copy todir="${target-bin}">
<fileset dir="${bin}"></fileset>
</copy>
<copy todir="${target-conf}">
<fileset dir="${conf}"></fileset>
</copy>
<copy todir="${target-html}">
<fileset dir="${html}"></fileset>
</copy>
<copy todir="${target-logs}">
<fileset dir="${logs}"></fileset>
</copy>
<copy todir="${target-service}">
<fileset dir="${service}"></fileset>
</copy>
<chmod file="${target-bin}/service" type="file" perm="ugo+rx"/>
</target>
</project>
3.在idea build窗口让ant配置文件和idea绑定起来
4.Add new Configuration:添加名字如openresty-test
添加 nginx server信息如下:
nginx执行文件:/home/maxl/nginx/openresty/nginx/sbin/nginx
nginx核心配置文件:/home/maxl/nginx/work/conf/nginx.conf
nginix pid文件:/home/maxl/nginx/work/logs/nginx.pid
Log file:
http log file:/home/maxl/nginx/work/logs/access.log
http log file:/home/maxl/nginx/work/logs/access.log
5.将idea ant插件与nginx插件关联起来
在Ant Build窗口加载build.xml
在Edit Configuration窗口中选择“Before launch:Ant Target”
下拉列表中选择 Run Ant target 'copy'
如果是是非root用户直接调试会失败,提示权限不够,所以只能完成拷贝
6.到/home/maxl/nginx/下执行sudo ./bin/service start 运行起来了
-p参数为新的perfix目录,nginx的include指令的相对目录是${prefix}/conf目录
而其它指令是相对于${prefix}
7.去掉nginx二进制文件的调试符,减少其大小,目前可以从21M减少到4.5M
cd ${install_prefix}/nginx/sbin && strip nginx && cd -
8.openrestry开发目录的规划
|--bin:脚本目录,存放各种脚本文件
|--conf:配置目录,存放nginx配置文件
| |--http:存放http服务的配置文件
| |--stream:存放tcp/udp服务的配置文件
| |--nginx.conf:主配置文件
|--logs:日志目录,存放nginx日志文件目录
| |--access.log:访问日志文件
| |--error.log:错误日志文件
|--service:应用程序目录,存放Lua代码
| |--conf:应用程序的配置
| |--http:http服务代码和静态html/css/js文件,web框架的都在这里面,具体细分目录见下面第10点
| |--stream:tcp/udp服务代码
9.demo例子的测试
1) ~/.bashrc添如下配置
NGINX_BASE=${HOME}/nginx
export OPENRESTY_HOME=$NGINX_BASE/openresty
PATH=$OPENRESTY_HOME/bin:$PATH
2) openresty 启动/停止/重新加载配置的方式
sudo ./bin/service start
sudo ./bin/service stop
sudo ./bin/service status
3).udp样例测试
nc -u 127.0.0.1 10054
将进入udp数据输入交互模式,输入数据"xxxx",将返回"收到的数据为:xxxx"
同时,将在logs/error.log中输出错误日志 "接收的数据[字符串]:xxxx"
3).tcp样例测试
nc 127.0.0.1 10053 或者telnet 127.0.0.1 10053
将输出“hello openrestry over TCP”
同时,将在logs/error.log中输出错误日志 “connected to 0.0.0.0:10053”
4).http样例测试
10.http开发框架
参考PHP ECOS源码移植到openresty平台上
1)整个目录结构说明如下
service/http
|--config:运行应用基础配置,数据库,业务响应状态码和状态描述映射
|--controller:restful api接口实现
|--filter:类似于java ee servlet过滤器的实现
|--html:静态html/css/js文件
|--lib:基础能力库代码,包含数据库(mysql/redis),cookie,http请求/响应,mvc,数据模型基类等实现
|--model:数据库业务模型
|--other:杂项目录,包括影响头过滤修改等
|--service:业务服务的实现逻辑
|--test:测试验证
2)关键文件说明
service/http
|--bootstrap.lua:http业务的入口,在openrestry的content_by_lua_file加载,映射到/api目录下
|--env.lua:运行时以来配置参数
|--routes.lua:restful接口和controller,filter之间的映射关系
|--lib
| |--router.lua:类似spring mvc的实现,调度controler接口lua代码方法
| |--response.lua:http响应封装json序列化lua table对象
最后附上源码下载地址:https://download.csdn.net/download/mxn771208/18935815