部署nginx+openresty+lua,作为应用层nginx
安装nginx+openresty
创建安装目录
mkdir -p /usr/servers
cd /usr/servers/
安装依赖
yum install -y readline-devel pcre-devel openssl-devel gcc
wget http://openresty.org/download/openresty-1.11.2.4.tar.gz
tar -zxvf openresty-1.11.2.4.tar.gz
cd /usr/servers/openresty-1.11.2.4
cd bundle/LuaJIT-2.1-20170405/
make clean && make && make install
ln -sf luajit-2.1.0-beta2 /usr/local/bin/luajit
安装bundle
cd /usr/servers/openresty-1.11.2.4/bundle
wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz
tar -xvf 2.3
wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz
tar -xvf v0.3.0
安装openresty
cd /usr/servers/openresty-1.11.2.4
./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
gmake && gmake install
查看安装文件
cd /usr/servers/
ll
total 4304
drwxr-xr-x. 2 root root 4096 Aug 17 17:14 bin
drwxr-xr-x. 6 root root 4096 Aug 17 17:14 luajit
drwxr-xr-x. 6 root root 4096 Aug 17 17:14 lualib
drwxr-xr-x. 11 root root 4096 Aug 17 17:16 nginx
drwxrwxr-x. 6 1000 1000 4096 Aug 17 17:12 openresty-1.11.2.4
-rw-r--r--. 1 root root 4158984 Jul 12 01:44 openresty-1.11.2.4.tar.gz
drwxr-xr-x. 43 root root 4096 Aug 17 17:14 pod
-rw-r--r--. 1 root root 214511 Aug 17 17:14 resty.index
drwxr-xr-x. 5 root root 4096 Aug 17 17:14 site
启动nginx
/usr/servers/nginx/sbin/nginx
nginx+lua开发的hello world
配置nginx.conf
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;
添加lua.conf
在/usr/servers/nginx/conf下,创建一个lua.conf,内容如下:
server {
listen 80;
server_name _;
}
验证配置文件
/usr/servers/nginx/sbin/nginx -t
nginx: the configuration file /usr/servers/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/servers/nginx/conf/nginx.conf test is successful
在lua.conf的server部分添加lua脚本
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://study0/lua
单独编写lua脚本模块
vi /usr/servers/nginx/conf/lua/test.lua
ngx.say("hello lua");
修改lua.conf
location /lua {
default_type 'text/html';
content_by_lua_file conf/lua/test.lua;
}
如果发现异常,查看异常日志
tail -f /usr/servers/nginx/logs/error.log
工程化的nginx+lua项目结构
项目工程结构
hello
hello.conf
lua
hello.lua
lualib
*.lua
*.so
创建hello项目
mkdir /usr/hello
cd /usr/hello
cp -r /usr/servers/lualib/ .
修改nginx.conf
vi /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;
添加hello.conf
vi /usr/hello/hello.conf
server {
listen 80;
server_name _;
location /lua {
default_type 'text/html';
content_by_lua_file /usr/hello/lua/hello.lua;
}
}
添加/usr/hello/lua/hello.lua
mkdir /usr/hello/lua/
vi /usr/hello/lua/hello.lua
ngx.say("hello,lua project")
重启nginx
/usr/servers/nginx/sbin/nginx -s reload