安装openresty

1、安装依赖开发库

yum install pcre-devel openssl-devel gcc curl postgresql-devel

2、下载 + 编译 + 安装

wget https://openresty.org/download/openresty-1.19.3.1.tar.gz
我刚开始先下载的1.19.3.1版本,但是在编译时增加nginx_upstream_check_module模块,一直报错,错误信息如下
./configure: error: the ngx_http_upstream_check_module addon error. 按照https://github.com/yaoweibin/nginx_upstream_check_module这里的步骤执行还是失败,于是就更换了低版本 1.15.8.2
cd ~
wget https://openresty.org/download/openresty-1.15.8.2.tar.gz
tar -zxvf openresty-1.15.8.2.tar.gz
下载nginx_upstream_check_module模块
git clone https://github.com/yaoweibin/nginx_upstream_check_module.git
cp -R nginx_upstream_check_module ~/openresty-1.15.8.2/bundle/
下载ngx_cache_purge模块
git clone https://github.com/FRiCKLE/ngx_cache_purge.git
cp -R ngx_cache_purge ~/openresty-1.15.8.2/bundle/
cd openresty-1.15.8.2
./configure --prefix=/opt/openresty \
--with-pcre \
--with-luajit \
--with-http_realip_module \
--with-http_iconv_module \
--with-http_postgres_module \
--add-module=./bundle/ngx_cache_purge \
--add-module=./bundle/nginx_upstream_check_module
make && make install
--with*** 安装一些内置/集成的模块
--with-http_realip_module 取用户真实 ip 模块
-with-pcre Perl 兼容的达式模块
--with-luajit 集成 luajit 模块
--add-module 添加自定义的第三方模块,ngx_cache_purge用于清除指定url的缓存 nginx_upstream_check_module健康检查

3、配置lua环境

3.1 编辑 nginx.conf

vim /opt/openresty/nginx/conf/nginx.conf
在http部分加入以下代码
lua模块路径,多个之间”;”分隔,其中”;;”表示默认搜索路径,默认到/opt/openresty/下找
lua_package_path "/opt/openresty/lualib/?.lua;;"; #lua 模块
lua_package_cpath "/opt/openresty/lualib/?.so;;"; #c模块
include lua.conf;#引入lua.conf

3.2 编辑lua.conf

vim lua.conf 写入以下代码

server {
    listen       8080;
    server_name  _;

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

   location /filelua {
        default_type 'text/html';
        lua_code_cache off;#关闭缓存,这样调试时每次修改lua代码不需要reload nginx
        content_by_lua_file conf/lua/test.lua;
   }
}

mkdir lua
cd lua
vim test.lua #写入以下代码
ngx.say("hello world,lua file");
然后重启nginx
/opt/openresty/nginx/sbin -t
/opt/openresty/nginx/sbin -c /opt/openresty/nginx/conf/nginx.conf
或者
/opt/openresty/nginx/sbin -s reload

3.3 测试

curl 127.0.0.1:8080/lua 输出 nbsp; hello world
curl 127.0.0.1:8080/filelua 输出 hello world,lua file