FWQ
openResty中的ngx.location.capture和ngx.location.capture_multi的使用
openResty中的ngx.location.capture和ngx.location.capture_multi的使用 在openResty中,ngx.location.capture_multi是一个非常强大的功能。可以应用于并发多个相互之间没有依赖的请求。在现代的应用架构中经常使用微服务,提供低粒度的接口;但在客户端(例如:app、网页服务)经常需要请求多个微服务接口,才能完整显示页面内容。 例如:打开一个商品详情页,需要请求: banner广告接口; 商品详情; 商品评论等。 那么ngx.location.capture_multi就派上大用场了,当然使用ngx.location.capture_multi不是唯一的办法,呵呵~。下面就来看看这个东东的用法; 先介绍一下下面这几个应用之间的差别; ngx.exec:nginx跳转;跳转到其他的location中执行。但仅限nginx内部的location。 ngx.redirect:和nginx.exec相似,但支持外部跳转。 ngx.location.capture_multi:并发请求;但仅限nginx内部的location。 http包中multi方法:概念上与ngx.location.capture_multi相似,但支持外部接口。 一、ngx.location.capture 语法: res = ngx.location.capture(uri, options?) 作用域: rewrite_by_lua*, access_by_lua*, content_by_lua* 1.1 uri 直接看栗子: location ~ /comment/([0-9]+) {…