二、OpenResty的应用编写hello lua和关闭缓存

一、编写hello lua

1、停止nginx进程

打开/usr/local/openresty/nginx/conf/nginx.conf

在其中加入

二、OpenResty的应用编写hello lua和关闭缓存

2、执行

nginx -s reload

 该命令用于配置文件被修改后时使用

这时候会报错

nginx: [error] open() "/usr/local/openresty/nginx/logs/nginx.pid" failed (2: No such file or directory)

输入下面代码解决:

/usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf

此时nginx会自动启动,不用再start了

二、OpenResty的应用编写hello lua和关闭缓存

 

该方法是在Nginx的配置文件的嵌入Lua代码

当代码逻辑复杂时就不适合在配置文件中写了,因此可以引用第三方脚本

二、引入lua代码文件编写hello lua

1、在nginx.conf中编写content_by_lua_file

二、OpenResty的应用编写hello lua和关闭缓存

2、创建lua文件夹,编写hello.lua

mkdir lua

二、OpenResty的应用编写hello lua和关闭缓存

vi hello.lua

二、OpenResty的应用编写hello lua和关闭缓存

3、查看结果

curl 127.0.0.1/hello

二、OpenResty的应用编写hello lua和关闭缓存

三、lua_code_cache

在上面的脚本中,修改了内容,则再次访问时还是原来的结果,这是因为其中有缓存,因此我们要将lua_code_cache关闭(注意生产环境不能关闭,因为会影响性能,开发环境可以关闭进行调试)

二、OpenResty的应用编写hello lua和关闭缓存

 再将hello.lua内容修改为hello moon

nginx -s reload

 二、OpenResty的应用编写hello lua和关闭缓存

内容改为hello sun

二、OpenResty的应用编写hello lua和关闭缓存

证明现在缓存已经关闭,至此修改代码后不用重启nginx了