nginx应用层安装
按照《1.安装nginx+openrestry》在study1和study2节点上安装nginx
配置study1和study2的lua页面
http://study1/lua
1.study1
[root@study1 hello]# cat lua/hello.lua
ngx.say("hello,study1")
2.study2
[root@study2 hello]# cat lua/hello.lua
ngx.say("hello,study2")
配置流量分发层nginx (study0节点)
[root@study0 lua]# cat hello.lua
--ngx.say("hello,lua project")
local uri_args = ngx.req.get_uri_args()
local userId = uri_args["userId"]
local host = {"192.168.137.151", "192.168.137.152"}
local hash = ngx.crc32_long(userId)
hash = (hash % 2) + 1
backend = "http://"..host[hash]
local method = uri_args["method"]
local requestBody = "/"..method.."?userId="..userId
local http = require("resty.http")
local httpc = http.new()
local resp, err = httpc:request_uri(backend, {
method = "GET",
path = requestBody
})
if not resp then
ngx.say("request error :", err)
return
end
ngx.say(resp.body)
httpc:close()
重启study0,study1,study2的节点的nginx
/usr/servers/nginx/sbin/nginx -s reload
验证
浏览器打开:http://study0/lua?userId=1&method=lua
hello,study2
浏览器打开:http://study0/lua?userId=4&method=lua
hello,study1