开发者社区> 问答> 正文

[@墨玖tao][¥20]学过一套亿级流量缓存架构的课程,里边一个nginx+lua的定向流量分发,一直调用不了二级nginx

vi /usr/hello/lua/hello.lua

local uri_args = ngx.req.get_uri_args()
local productId = uri_args["productId"]

local host = {"192.168.1.107", "192.168.1.104"}
local hash = ngx.crc32_long(productId)
hash = (hash % 2) + 1
backend = "http://"..host[hash]

local requestPath = uri_args["requestPath"]
requestPath = "/"..requestPath.."?productId="..productId

local http = require("resty.http")
local httpc = http.new()

local resp, err = httpc:request_uri(backend, {

method = "GET",  
path = requestPath

})

if not resp then

ngx.say("request error :", err)  
return  

end

ngx.say(resp.body)

httpc:close()

展开
收起
gaogaoyanjiu888 2018-11-14 01:09:47 2885 0
1 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
高性能Web架构之缓存体系 立即下载
CentOS Nginx PHP JAVA 多语言镜像使用手 立即下载
CentOS Nginx PHP JAVA多语言镜像使用手册 立即下载