環境描述:
192.168.0.16 啓動一個Apache,訪問80端口 返回 192.168.0.16 esbrunning
192.168.0.17 啓動一個Apache,訪問80端口 返回192.168.0.17 esbrunning
啓動一個openresty,訪問8000端口輪詢轉發到192.168.016和192.168.0.17的80端口
步驟一:
(1)192.168.0.16和192.168.0.17啓動一個Apache,配置好80端口
(2)192.168.0.17安裝一個openresty,配置server的端口是8000
listen 8000;
server_name 192.168.0.17;
user nobody;
worker_processes 3;
步驟二:
(1)修改nginx.conf的http模塊,添加upstream
upstream tomcatproxy {
server 192.168.0.16:80 weight=1;
server 192.168.0.17:80 weight=1;
}
(2)修改nginx.conf的http模塊,添加upstream的檢查模塊
lua_shared_dict healthcheck 1m;
init_worker_by_lua_block {
local hc = require "resty.upstream.healthcheck"
local ok, err = hc.spawn_checker{
shm = "healthcheck", -- defined by "lua_shared_dict"
upstream = "tomcatproxy", -- defined by "upstream"
type = "http",
http_req = "GET /index.html HTTP/1.0\r\nconnection: keep-alive\r\n\r\n",
-- raw HTTP request for checking
interval = 2000, -- run the check cycle every 2 sec
timeout = 1000, -- 1 sec is the timeout for network operations
fall = 3, -- # of successive failures before turning a peer down
rise = 2, -- # of successive successes before turning a peer up
valid_statuses = {200, 302}, -- a list valid HTTP status code
concurrency = 10, -- concurrency level for test requests
}
if not ok then
ngx.log(ngx.ERR, "failed to spawn health checker: ", err)
return
end
}
(3)修改nginx.conf的server模塊
location / {
proxy_pass http://tomcatproxy/;
proxy_set_header x-forward-for $proxy_add_x_forwarded_for;
}
(4)重啓openresty
步驟三:
(1)測試一下轉發功能
請到下篇進行lua腳本配置,https://blog.csdn.net/u014686399/article/details/80226161
如果有不清楚的請到 630300475qq羣。