簡單版nginx lua 完成定向流量分發策略


本文鏈接:https://www.cnblogs.com/zhenghongxin/p/9131362.html

公司業務前端是使用 “分發層+應用層” 雙層nginx架構,目的是為了提高緩存的命中率。最前端有個nginx分發層,底下是負載均衡集群。

為了提高緩存的命中率,需要nginx進行定向流量分發,簡略代碼如下:

local uri_args = ngx.req.get_uri_args() local productId = uri_args["productId"] local hosts = {"192.168.31.187", "192.168.31.19"} local hash = ngx.crc32_long(productId) local index = (hash % 2) + 1 backend = "http://"..hosts[index] 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()

 進行hash取模定向轉發


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM