openresty安装

openresty官网:http://openresty.org/cn/

openresty官方安装文档:http://openresty.org/cn/installation.html

github地址:https://github.com/openresty

安装前准备,必须安装perl、libpcre、libssl库。

可以用如下命令查看安装情况

cpp 复制代码
sudo ldconfig -v
 sudo apt install libpcre3-dev libssl-dev perl make build-essential curl libreadline-dev libncurses5-dev

到官网下载源码openresty-1.25.3.1后:

cpp 复制代码
 ./configure
sudo make && make install
cd /usr/local/openresty

默认会被安装到/usr/local/openresty目录下

启动Nginx

cpp 复制代码
sudo /usr/local/openresty/nginx/sbin/nginx
ps -ef | grep nginx

查看版本:

cpp 复制代码
/usr/local/openresty/nginx/sbin/nginx -v

编写第一个hellow

OpenResty 是一个基于 NGINX 的全功能 Web 平台,集成了大量的第三方模块和库,其中最重要的是 ngx_lua 模块,它允许在 NGINX 配置中嵌入 Lua 脚本,实现高级的请求处理逻辑、动态内容生成、访问控制等功能。下面是一个简单的OpenResty 示例:

cpp 复制代码
sudo vim /usr/local/openresty/nginx/conf/nginx.conf
cpp 复制代码
server {
        listen       80;
        server_name  localhost;
        location /test {
                default_type 'text/plain'; #加这一句使得可以显示在网页上,如果只有下面代码,那么浏览器访问只会下载文件爱包含这句话。
                content_by_lua_block {
                                        ngx.say("Hello sherlock!, LuaJIT!")
                                     }
                        }
        #charset koi8-r;
        #...伪代码
}
cpp 复制代码
sudo /usr/local/openresty/nginx/sbin/nginx -t //检查以上配置文件的语法
sudo /usr/local/openresty/nginx/sbin/nginx -s reload //加载配置文件, 这里不是重启

访问:

cpp 复制代码
curl -v http://127.0.0.1/test

返回:

Hello sherlock!, LuaJIT!

网页访问:http://127.0.0.1:80/test

显示:

Hello sherlock!, LuaJIT!

以上配置了一个监听 80 端口的 server 块,处理 example.com 的请求。

当访问路径 /test 时,通过 content_by_lua_block 指令执行 Lua 代码,输出 "Hello sherlock!, LuaJIT!"。

这只是一个最简单的演示,OpenResty 的强大之处在于它允许在 NGINX 配置中使用 Lua 脚本,从而实现更复杂的逻辑。以下是一个稍复杂的示例,演示了如何通过 OpenResty 实现简单的 API 访问控制:

cpp 复制代码
http {
    lua_shared_dict my_limit 10m;

    server {
        listen 80;
        server_name api.example.com;

        location /api {
            access_by_lua_block {
                local limit = ngx.shared.my_limit
                local key = ngx.var.remote_addr
                local reqs, err = limit:get(key)
                if reqs then
                    if reqs > 10 then
                        ngx.exit(ngx.HTTP_TOO_MANY_REQUESTS)
                    else
                        limit:incr(key, 1)
                    end
                else
                    limit:set(key, 1, 60)
                end
            }

            default_type 'application/json';
            content_by_lua_block {
                ngx.say('{"message": "API response"}')
            }
        }
    }
}

在这个示例中:

配置了一个共享内存字典 my_limit 用于存储请求计数。

当访问路径 /api 时,通过 access_by_lua_block 指令执行 Lua 代码,实现了一个简单的请求频率限制,每个 IP 地址在 60 秒内最多允许 10 次请求。

如果超过请求限制,将返回 HTTP 429 (TOO MANY REQUESTS) 状态码;否则,继续执行后续 Lua 代码返回 JSON 响应。

这只是 OpenResty 的一小部分功能展示,实际使用中可以结合更多的模块和功能,如 ngx_http_lua_upstream、ngx_http_headers_more、ngx_stream_lua 等,以实现更复杂的 Web 应用和服务。

更多示例,参考:OpenResty 介绍与实战讲解(nginx&lua)

相关推荐
dgiij3 天前
对免认证服务提供apikey验证
nginx·http·lua·llama·openresty
蕴微轩7 天前
用openresty和lua实现壁纸投票功能
lua·openresty
xyc12117 天前
OpenResty
openresty
蕴微轩15 天前
通过openresty和lua实现随机壁纸
开发语言·lua·openresty
fly-iot16 天前
【openresty服务器】:源码编译openresty支持ssl,增加service系统服务,开机启动,自己本地签名证书,配置https访问
服务器·ssl·openresty
core51217 天前
“可通过HTTP获取远端WWW服务信息”漏洞修复
nginx·漏洞·openresty·可通过http·获取远端www服务信息
Narutolxy1 个月前
使用 OpenResty 构建高效的动态图片水印代理服务20250127
junit·openresty
龙哥·三年风水1 个月前
openresty(nginx)+lua+kafka实现日志搜集系统
kafka·lua·openresty
請叫我菜鳥1 个月前
什么是 OpenResty
nginx·lua·openresty
Hello.Reader2 个月前
深入浅出 OpenResty
junit·openresty