从Hello World初识OpenResty

1
2
$ resty -e "ngx.say('hello world')"
hello world

简介

OpenResty是一个兼具开发效率和性能的服务端开发平台,它的核心是基于Nginx的一个C模块(lua-nginx-module),该模块将LuaJIT嵌入到Nginx服务器中,并对外提供一套完整的Lua API,透明地支持非阻塞I/O,提供了轻量级线程、定时器等高级抽象。同时,围绕这个模块,OpenResty构建了一套完备的测试框架、调试技术以及由Lua实现的周边功能库。

安装

跟着官方文档走就行,说的很清楚,我使用的系统版本是Ubuntu 18.04。

如果已经开启运行了Nginx,需要先关闭停止Nginx服务

1
2
sudo systemctl disable nginx
sudo systemctl stop nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
# import our GPG key:
wget -qO - https://openresty.org/package/pubkey.gpg | sudo apt-key add -

# for installing the add-apt-repository command
# (you can remove this package and its dependencies later):
sudo apt-get -y install software-properties-common

# add the our official APT repository:
sudo add-apt-repository -y "deb http://openresty.org/package/ubuntu $(lsb_release -sc) main"

# to update the APT index:
sudo apt-get update
sudo apt-get install openresty

安装完毕后,可以通过which查看OpenResty的CLI: resty的位置:

1
2
$ which resty
/usr/bin/resty

Hello World

像简介上面的写法,便是resty最简单输出Hello World的代码。

内嵌代码

首先创建工作目录

1
2
3
$ mkdir restywork
$ cd restywork/
$ mkdir logs/ conf/

在conf目录下新建一个简化的nginx.conf,在其中添加OpenRestry的content_by_lua指令,嵌入ngx.say代码,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ cat conf/nginx.conf
events {
worker_connections 1024;
}

http {
server {
listen 8080;
location / {
content_by_lua '
ngx.say("hello, restry")
';
}
}
}

此时启动openresty服务:

1
$ openresty -p `pwd` -c conf/nginx.conf

没有报错就代表启动成功。

使用curl或者浏览器访问即可:

1
2
3
4
5
6
7
8
9
$ curl -i 127.0.0.1:8080
HTTP/1.1 200 OK
Server: openresty/1.15.8.1
Date: Thu, 20 Jun 2019 14:06:49 GMT
Content-Type: text/plain
Transfer-Encoding: chunked
Connection: keep-alive

hello, restry

引入Lua脚本

在restywork中创建一个目录单独存放Lua代码:

1
2
3
$ mkdir lua_script
$ cat lua_script/hello.lua
ngx.say("hello, lua world")

现在可以去修改nginx.conf的配置,将content_by_lua_block修改为content_by_lua_file:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ cat conf/nginx.conf 
events {
worker_connections 1024;
}

http {
server {
listen 8080;
location / {
content_by_lua_file lua_script/hello.lua;
}
}
}

然后通过reload更新:

1
openresty -p `pwd` -c conf/nginx.conf -s reload

再次使用curl或者浏览器访问:

1
2
3
4
5
6
7
8
9
$ curl -i 127.0.0.1:8080
HTTP/1.1 200 OK
Server: openresty/1.15.8.1
Date: Thu, 20 Jun 2019 14:32:38 GMT
Content-Type: text/plain
Transfer-Encoding: chunked
Connection: keep-alive

hello, lua world

关于缓存

Lua代码的变更,需要reload才能生效,所以在调试的时候,最好关闭 lua_code_cache这个选项。

1
lua_code_cache off;

关闭lua_code_cache之后,OpenResty会给每个请求创建新的Lua VM。由于没有Lua module的缓存,新的VM会去加载刚最新的Lua文件。这样,你修改完代码后,不用reload Nginx就可以生效了。在生产环境下记得打开这个选项。

当然,由于每个请求运行在独立的Lua VM里,lua_code_cache off会带来以下几个问题:

  • 每个请求都会有独立的module,独立的lrucache,独立的timer,独立的线程池。
  • 跟请求无关的模块,由于不会被新的请求加载,并不会主动更新。比如init_by_lua_file引用的文件就不会被更新。
  • *_by_lua_block里面的代码,由于不在Lua文件里面,设置lua_code_cache对其没有意义。

如果调试的目标涉及以上内容,仍需设置lua_code_cache on,通过reload来更新代码。

在nginx.conf中添加lua_code_cache off:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ cat conf/nginx.conf 
events {
worker_connections 1024;
}

http {
server {
listen 8080;
lua_code_cache off;
location / {
content_by_lua_file lua_script/hello.lua;
}
}
}

通过reload更新

1
2
$ openresty -p `pwd` -c conf/nginx.conf -s reload
nginx: [alert] lua_code_cache is off; this will hurt performance in /home/top/restywork/conf/nginx.conf:8

接下来修改hello.lua中的内容,并使用curl进行访问:

1
2
3
4
5
6
7
8
9
10
11
12
$ cat lua_script/hello.lua 
ngx.say("hello, openresty world")

$ curl -i 127.0.0.1:8080
HTTP/1.1 200 OK
Server: openresty/1.15.8.1
Date: Thu, 20 Jun 2019 14:49:24 GMT
Content-Type: text/plain
Transfer-Encoding: chunked
Connection: keep-alive

hello, openresty world

关闭服务

以上就是使用OpenResty完成Hello World的过程,现在可以关闭服务了

1
openresty -s quit -p `pwd` -c conf/nginx.conf

参考

1、极客时间专栏《OpenResty从入门到实战》
2、OpenResty Best Practices

0%