下載使用
鏈接:https://pan.baidu.com/s/1uQ7yCzQsPWsF6xavFTpbZg
提取碼:htay
–來自百度網(wǎng)盤超級會員V5的分享
在根目錄下執(zhí)行:
# 從 github 上下載文件
git clone https://github.com/ledgetech/lua-resty-http.git
# 將 lua-resty-http/lib/ 下的 resty 文件夾上傳至服務(wù)器一下目錄下(lua-5.4.2安裝目錄下)
/lua-5.4.2/
# 此模塊主要用來發(fā)送 http 請求
# 也可選用 socket.http
ad_load.lua文件文章來源:http://www.zghlxwxcb.cn/news/detail-800380.html
ngx.header.content_type="application/json;charset=utf8"
local cjson = require("cjson")
local https = require 'resty.http'
local json = cjson:new()
--服務(wù)器一,域名拼接request_uri獲得請求的路由和路由上的參數(shù)
-- local server_one_url = 'http://127.0.0.1:9059/index01';
-- local server_one_url = 'http://127.0.0.1:9059'..ngx.var.request_uri;
local server_one_url = ngx.var.url_ip..':'..ngx.var.url_port..ngx.var.request_uri;
local server_one_code = 'ONE_CODE';
-- ngx.say(server_one_url)
-- ngx.exit(ngx.OK)
--http get 請求方法
local function http_get(url)
local http = https:new()
local res, err = http:request_uri(url, {
method = 'GET',
headers = {
['Content-Type'] = 'application/json';
}
})
if res.status == 200 then
ngx.say(res.body)
else
-- ngx.say(res.status)
ngx.say('服務(wù)器錯誤!')
end
end
-- http post 請求方法
local function http_post(url, data)
local http = https:new()
local res, err = http:request_uri(url, {
method = 'POST',
body = data,
headers = {
['Content-Type'] = 'application/json';
}
})
if res.status == 200 then
ngx.say(res.body)
else
ngx.say('服務(wù)器錯誤!')
end
end
-- ngx.say(server_one_url)
-- ngx.say(ngx.var.request_method)
-- ngx.exit(ngx.OK)
if ngx.var.request_method == "GET" then
--解析參數(shù)并做轉(zhuǎn)發(fā)
http_get(server_one_url)
elseif ngx.var.request_method == "POST" then
--獲取請求參數(shù)
ngx.req.read_body()
local data = ngx.req.get_body_data()
--解析參數(shù)并做轉(zhuǎn)發(fā)
http_post(server_one_url, data)
end
location / {
if ($server_port = 9058 ) {
# 定義使用內(nèi)網(wǎng)ip,定義請求端口
set $url_ip 'http://127.0.0.1';
set $url_port '9059';
content_by_lua_file /lua-5.4.2/ad_load.lua;
}
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-800380.html
到了這里,關(guān)于lua使用resty.http做nginx反向代理(https請求,docker容器化部署集群),一個域名多項目轉(zhuǎn)發(fā)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!