前言
OpenResty是一款基于Nginx的高性能负载均衡服务器容器,简单来说是Nginx+Lua。结合了Lua语言来对Nginx进行扩展,使得在Nginx上具有web容器功能。
一、OpenResty运行环境搭建
Centos7安装编译所需要的环境:
yum install readline-devel pcre-devel openssl-devel gc
如果提示缺少perl环境,执行以下安装命令
yum install perl*
OpenResty官网下载地址:http://openresty.org/cn/download.html
复制下载地址:
wget https://openresty.org/download/openresty-1.15.8.1.tar.gz
解压文件
tar -xvzf openresty-1.15.8.1.tar.gz
cd openresty-1.15.8.1
安装前openresty目录结构如下:
安装OpenResty并设置安装目录为/opt/openresty,如果不设置,默认会安装至/usr/local/openresty
./configure --with-cc-opt="-I/usr/local/include" --with-ld-opt="-L/usr/local/lib" --prefix=/opt/openresty
make
make install
编译后的openresty目录结构如下:
OpenResty安装完成,可以尝试启动
/opt/openresty/nginx/sbin/nginx -c /opt/openresty/nginx/conf/nginx.conf -p /opt/openresty/nginx/
启动成功后,查看端口占用情况:
netstat -ntlp
如果不存在netstat命令 执行:
yum install -y net-tools
从上图中可以看到nginx已经在监听80端口。访问该端口
OpenResty已经启动成功
在修改相关配置文件后,需先停止服务,再进行启动:
/opt/openresty/nginx/sbin/nginx -s stop
/opt/openresty/nginx/sbin/nginx -c /opt/openresty/nginx/conf/nginx.conf -p /opt/openresty/nginx/
二、nginx+lua开发环境配置
- 编辑nginx.conf 配置文件
vi /opt/openresty/nginx/conf/nginx.conf
- 再http部分添加如下配置:
#lua模块路径,多个之间”;”分隔,其中”;;”表示默认搜索路径,默认到/usr/servers/nginx下找 lua_package_path "/opt/openresty/lualib/?.lua;;"; #lua 模块 lua_package_cpath "/opt/openresty/lualib/?.so;;"; #c 模块
- 在/opt/openresty/nginx/conf目录下创建一个lua.conf
#lua.conf server { listen 80; server_name _; }
- 在nginx.conf中的http部分添加 include lua.conf 包含此文件片段
include lua.conf
- 测试是否正常
如下图所示,表示配置添加成功/opt/openresty/nginx/sbin/nginx -t
三、Hello world
在lua.conf 中server部分添加如下配置
location /lua {
default_type 'text/html';
content_by_lua 'ngx.say("hello world")';
}
测试配置是否正确
/opt/openresty/nginx/sbin/nginx -t
重启nginx
/opt/openresty/nginx/sbin/nginx -s stop
/opt/openresty/nginx/sbin/nginx -c /opt/openresty/nginx/conf/nginx.conf -p /opt/openresty/nginx/
访问http://xxx.xxx.xxx/lua,看到如下结果:
lua代码文件
我们把lua代码放在nginx配置中会随着lua的代码的增加导致配置文件太长不好维护,因此我们应该把lua代码移到外部文件中存储。
vi /opt/openresty/nginx/conf/lua/test.lua
这里把代码放在nginx/conf/lua中
修改lua.conf,在localtion中增加
content_by_lua_file conf/lua/test.lua; #相对于nginx安装目录
现在lua.conf整体为:
#lua.conf
server {
listen 80;
server_name _;
location /lua {
default_type 'text/html';
content_by_lua_file conf/lua/test.lua; #相对于nginx安装目录
}
}
此处conf/lua/test.lua也可以使用绝对路径/opt/openresty/nginx/conf/lua/test.lua
lua_code_cache
默认情况下 lua_code_cache是开启的,即缓存lua代码,即每次lua代码变更必须reload NGINX才生效,如果在开发阶段可以通过 lua_code_cache off;关闭缓存,这样调试时每次修改lua代码不需要reload NGINX;但是在正式环境要开启缓存。
#lua.conf
server {
listen 80;
server_name _;
location /lua {
default_type 'text/html';
lua_code_cache off;
content_by_lua_file conf/lua/test.lua; #相对于nginx安装目录
}
}
开启后reload NGINX 会看到如下报警
错误日志
如果在运行过程中出现错误,可方便查看错误日志
tail -f -n 100 /opt/openresty/nginx/logs/error.log
基本环境搭建完成。
四、nginx+lua项目构建
随着时间的推移,我们的nginx lua开发文件会越来越多,我们应该把其项目化,已方便开发。项目目录结构如下所示:
其中我们把lualib也放到项目中的好处就是以后部署的时候可以一起部署,防止有的服务器忘记复制依赖而造成缺少依赖的情况。
我们将项目放到到/usr/openResty目录下。
nginx.conf配置文件修改includ的conf文件,修改为我们项目中的conf,同时修改引入lualib的地址
#lua模块路径,多个之间”;”分隔,其中”;;”表示默认搜索路径,默认到/usr/servers/nginx下找
lua_package_path "/usr/openResty/lualib/?.lua;;"; #lua 模块
lua_package_cpath "/usr/openResty/lualib/?.so;;"; #c 模块
include /usr/openResty/openResty.conf;
通过绝对路径包含我们的lua依赖库和nginx项目配置文件。
/usr/openResty/openResty.conf的内容如下:
server {
listen 80;
server_name _;
location /lua {
default_type 'text/html';
lua_code_cache off;
content_by_lua_file /usr/openResty/lua/test.lua;
}
}
lua文件我们使用绝对路径/usr/openResty/lua/test.lua