编写Lua脚本

路径在/lua/hello.lua
编写 hello.lua 内容:

  1. ngx.say("hello, world")
  1. [root@zjj101 lua]# pwd
  2. /lua
  3. [root@zjj101 lua]# cat hello.lua
  4. ngx.say("hello, world")
  5. [root@zjj101 lua]#

配置OpenResty的nginx.conf配置文件

nginx.conf文件修改内容

http模块儿里面添加server模块儿

  1. server {
  2. listen 8081;
  3. location / {
  4. content_by_lua_file '/lua/hello.lua';
  5. }
  6. }

刷新配置文件

需要配置环境变量,如果不会的话,查看
https://www.yuque.com/docs/share/68e00d5d-4a3f-4c1a-8ee0-a32ac8114190?# 《OpenResty配置环境变量》

刷新配置文件命令:

  1. root@zjj101 lua]# openresty -s reload

执行查看结果

  1. [root@zjj101 lua]# curl -i 127.0.0.1:8081
  2. HTTP/1.1 200 OK
  3. Server: openresty/1.15.8.1
  4. Date: Fri, 11 Jun 2021 10:05:11 GMT
  5. Content-Type: application/octet-stream
  6. Transfer-Encoding: chunked
  7. Connection: keep-alive
  8. hello, world
  9. [root@zjj101 lua]#

总结

后面 Lua 代码的变更,你就可以直接修改 hello.lua 这个文件,而不是 nginx.conf 了。