[openresty] hello world

本文介绍如何在OpenResty中使用Lua进行开发,包括通过content_by_lua指令快速输出内容,利用rewrite_by_lua_file执行更复杂的Lua脚本,以及调整lua_code_cache设置以实现代码的即时更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

0.openresty软件部署

不说了,和nginx差不多的。

1.content_by_lua指令

可以用content_by_lua指令将一段lua代码包含到nginx里面去执行,比如下面最简单的调用ngx.say来输出Hello world。

    location / {
            default_type text/html;
            content_by_lua '
                ngx.say("<p>hello, world</p>")
            ';
        }

2.长一些的代码适合用rewrite_by_lua_file

后面就跟一个path路径

    location / {
            content_by_lua_file lua/hello.lua;
        }

[root@test nginx]# cat lua/hello.lua
ngx.say("<p>hello, world!!! hey man</p>")

3.lua_code_cache

默认nginx对lua代码段有一层缓存,提高了效率,但是这也让我们每次修改完lua以后需要reload nginx才能生效,这时候我们可以关掉cache,实时生效,等调试完毕以后再打开。

lua_code_cache off;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值