日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Nginx >内容正文

Nginx

nginx 获取body参数_分布式实战:Nginx缓存之流量分发层

發布時間:2024/2/28 Nginx 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 nginx 获取body参数_分布式实战:Nginx缓存之流量分发层 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文首發于Ressmix個人站點:https://www.tpvlog.com

本章,我將進行Nginx流量分發層的lua代碼編寫。流量分發層的Nginx服務器,會基于商品id執行流量分發策略:

  • 獲取請求參數productId;

  • 對productId進行hash運算,將hash值與應用層的Nginx服務器數量取模,獲取到一個應用層服務器;

  • 利用http包發送請求到應用層nginx;

  • 獲取響應后返回。

  • 一、OpenResty配置

    我們需要對OpenResty做一些額外配置,引入依賴的工具包。我們目前的項目目錄結構如下:

    1/www2??/conf3????nginx.conf4??/logs5??/lua6????main.lua7??/lualib

    首先修改nginx.conf配置如下:

    1worker_processes??1; 2error_log?logs/error.log; 3events?{ 4????worker_connections?1024; 5} 6http?{ 7????lua_package_path?"$prefix/lualib/?.lua;;";?? 8????lua_package_cpath?"$prefix/lualib/?.so;;";? 9????server?{10????????listen?80;11????????location?/product?{12????????????default_type?text/html;13????????????content_by_lua_file?lua/main.lua;14????????}15????}16}

    上面的$prefix是指我們啟動openresty時代入的-p參數。然后,我們需要將一些官方或第三方的lua庫放到lualib目錄下,比如我們可以直接將openresty默認安裝包下的lualib直接復制到我們自己的項目的lualib中。

    然后,在我們項目中的lualib/resty目錄下,執行以下命令,安裝http庫:

    1cd?/usr/local/www/lualib/resty/??2wget?https://github.com/ledgetech/lua-resty-http/blob/master/lib/resty/http.lua??3wget?https://github.com/ledgetech/lua-resty-http/blob/master/lib/resty/http_headers.lua

    二、功能實現

    接著,我們就要在ressmix-dsf01上的main.lua中實現流量分發邏輯了,我們直接根據請求URI中的productId進行哈希:

    1--獲取http請求中的uri參數 2local?uri_args?=?ngx.req.get_uri_args() 3local?productId?=?uri_args["productId"] 4local?shopId?=?uri_args["shopId"] 5 6--確定路由到那臺應用層Nginx服務器 7local?host?=?{"192.168.0.109",?"192.168.0.110"} 8local?hash?=?ngx.crc32_long(productId) 910hash?=?(hash?%?2)?+?111backend?=?"http://"..host[hash]1213local?requestBody?=?"/product?productId="..productId.."&shopId="..shopId1415local?http?=?require("resty.http")16local?httpc?=?http.new()1718--發起請求19local?resp,?err?=?httpc:request_uri(backend,?{20????method?=?"GET",21????path?=?requestBody22})2324if?not?resp?then25????ngx.say("request?error?:",?err)26????return27end2829--響應30ngx.say(resp.body)3132httpc:close()

    最后,分別啟動ressmix-dsf01、ressmix-dsf02、ressmix-dsf03上的openresty:

    1openresty?-p?/usr/local/www?-c?/usr/local/www/conf/nginx.conf

    然后在ressmix-dsf01上發起兩筆請求測試下:

    1[root@ressmix-dsf01?lua]#?curl?-i?http://192.168.0.107/product?productId=117&shopId=123 2HTTP/1.1?200?OK 3Server:?openresty/1.15.8.3 4Date:?Wed,?03?Jun?2020?19:25:55?GMT 5Content-Type:?text/html 6Transfer-Encoding:?chunked 7Connection:?keep-alive 8 9http://192.168.0.107/product?productId=117&shopId=12310

    hello,?world,ressmix-dsf02

    1[root@ressmix-dsf01?lua]#?curl?-i?http://192.168.0.107/product?productId=112&shopId=123 2HTTP/1.1?200?OK 3Server:?openresty/1.15.8.3 4Date:?Wed,?03?Jun?2020?19:26:26?GMT 5Content-Type:?text/html 6Transfer-Encoding:?chunked 7Connection:?keep-alive 8 9http://192.168.0.107/product?productId=112&shopId=12310

    hello,?world,ressmix-dsf03


    可以看到,ressmix-dsf01上的OpenResty流量分發層已經能夠根據productId將請求分發到應用層了。

    三、總結

    本章,我通過lua腳本腳本實現了Nginx流量分發層的分發邏輯,核心是利用了OpenResty的http包,讀者在使用OpenResty時需要多參考官方的文檔,因為OpenResty有很多API使用上的坑,也沒有一個比較好的調試工具,所以出現錯誤時有時會很難排查。

    總結

    以上是生活随笔為你收集整理的nginx 获取body参数_分布式实战:Nginx缓存之流量分发层的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。