关注小众语言、AI技术,记录、分享技术点滴!

0%

centos下原生nginx支持openresty功能

nginx想支持openresty的功能,需要安装以下模块及依赖luajit2lua-resty-corelua-resty-lrucachelua-nginx-modulengx_devel_kit以下5个依赖包必须先下载,另外如果需要同时支持SSL的话,还需要下载openssl依赖包(ssl只需要下载nginx编译时使用)。

一、安装openresty依赖包
1、安装luajit2(下载最新版本)

1
2
3
4
5
6
7
8
9
wget https://github.com/openresty/luajit2/archive/v2.1-20201229.tar.gz
tar -zxvf luajit2-2.1-20201229.tar.gz
cd luajit2-2.1-20201229
make
make install

#导入环境变量(编译nginx时需要)
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.1

2、安装lua-resty-core

1
2
3
4
wget https://github.com/openresty/lua-resty-core/archive/v0.1.21.tar.gz
tar -zxvf lua-resty-core-0.1.21.tar.gz
cd lua-resty-core-0.1.21
make install

3、安装lua-resty-lrucache

1
2
3
4
wget https://github.com/openresty/lua-resty-lrucache/archive/v0.10.tar.gz
tar -zxvf lua-resty-lrucache-0.10.tar.gz
cd lua-resty-lrucache-0.10
make install

4、解压lua-nginx-module

1
2
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.19.tar.gz
tar -zxvf lua-nginx-module-0.10.19.tar.gz

5、解压ngx_devel_kit

1
2
wget https://github.com/vision5/ngx_devel_kit/archive/v0.3.1.tar.gz
tar -zxvf ngx_devel_kit-0.3.1.tar.gz

6、解压openssl

1
2
wget https://www.openssl.org/source/openssl-1.1.1k.tar.gz
tar -zxvf openssl-1.1.1k.tar.gz

二、编译及安装nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -zxvf nginx-1.18.0.tar.gz

./configure --prefix=/usr/local/nginx \
--with-openssl=/usr/local/src/openssl-1.1.1k \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-http_gzip_static_module \
--with-threads \
--with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-http_slice_module \
--with-compat \
--with-file-aio \
--with-http_v2_module \
--with-ld-opt="-Wl,-rpath,/usr/local/lib" \
--add-module=/usr/local/src/ngx_devel_kit-0.3.1 \
--add-module=/usr/local/src/lua-nginx-module-0.10.19

make && make install

三、nginx配置

1
2
3
4
5
6
#设置openresty模块目录
lua_package_path "/usr/local/lib/lua/?.lua;/usr/local/nginx/conf/lua/?.lua;;";

#指定dns解析服务器,实现动态upstream
resolver 114.114.114.114 223.5.5.5 1.1.1.1 8.8.8.8 valid=30;
resolver_timeout 5;

四、hello openresty(/usr/local/nginx/conf/lua/hello.lua)

1
ngx.print("hello openresty!")