OpenResty-1.快速安装和使用

一、相关资源

1.openresty官网

2.openresty视频教程

3.ngx_lua文档

4.openresty最佳实践

5.相关类库说明

6.【开源访谈】OpenResty 作者章亦春访谈实录

附图一张:

二、快速安装

1. 下载

https://openresty.org/cn/download.html下载最新的版本,例如·

1
2
3
4
cd /opt/soft/
wget https://openresty.org/download/openresty-1.9.15.1.tar.gz '--no-check-certificate'
tar -xvf openresty-1.9.15.1.tar.gz
ln -s openresty-1.9.15.1 openresty

2. 准备

(1) yum相关的依赖

1
yum install readline-devel pcre-devel openssl-devel gcc

(2) 编译安装

1
2
3
4
5
6
7
8
9
10
cd /opt/soft/openresty
./configure \
--prefix=/opt/openresty
--without-http_redis2_module \
--with-cc-opt="-I/usr/local/opt/openssl/include/ -I/usr/local/opt/pcre/include/" \
--with-ld-opt="-L/usr/local/opt/openssl/lib/ -L/usr/local/opt/pcre/lib/" \
-j8
--with-luajit
make
make install

3.启动

启动方法和nginx是一样的:

1
/opt/soft/openresty/nginx/sbin/nginx -c /opt/soft/openresty/nginx/conf/nginx.conf

4.验证

1
curl 127.0.0.1

结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>

三、快速使用

1.在nginx.conf中添加ngx_lua代码

1
2
3
4
5
location /hello {
content_by_lua '
ngx.say("<p>hello openresty!!</p>")
';
}

2.验证并重启nginx

1
2
/opt/soft/openresty/nginx/sbin/nginx -t /opt/soft/openresty/nginx/conf/nginx.conf
/opt/soft/openresty/nginx/sbin/nginx -s reload

3.访问验证

1
2
[@bx-14-182 conf]# curl 127.0.0.1/hello
<p>hello openresty!!</p>

提示,有关ngx_lua的语法可以到如下网站搜索

1
https://www.nginx.com/resources/wiki/modules/lua/?highlight=luas

4.lua脚本独立

(1) lua独立

1
2
3
location /hello {
content_by_lua_file /usr/local/openresty/script/hello.lua;
}

其中content_by_lua_file是通过上面的网站查到的,这个网址应该经常用到。

(2) 脚本

1
2
[@bx-14-182 script]# cat /usr/local/openresty/script/hello.lua
ngx.say("<p>hello openresty content_by_lua_file.</p>")

(3) 缓存:
先修改脚本内容发现,返回内容不变,需要添加缓存选项(如果是生产可能会影响性能)
默认开启, 有多个使用范围

1
2
3
syntax: lua_code_cache on | off
default: lua_code_cache on
context: http, server, location, location if

例如:

1
2
[@bx-14-182 script]# ../nginx/sbin/nginx -t
nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/openresty/nginx/conf/nginx.conf:48