1、安装 openresty 依赖模块:
[root@Centos opt]# yum -y install pcre-devel openssl openssl-devel postgresql-devel
2、编译安装 openresty:
[root@Centos opt]# tar -zxvf openresty-1.15.8.2.tar.gz ...(略去内容)... [root@Centos opt]# cd openresty-1.15.8.2/ [root@Centos openresty-1.15.8.2]# ll 总用量 112 drwxrwxr-x. 47 hacker 1003 4096 11月 12 23:35 build drwxrwxr-x. 46 hacker 1003 4096 8月 29 13:33 bundle -rwxrwxr-x. 1 hacker 1003 52432 8月 29 13:32 configure -rw-rw-r--. 1 hacker 1003 22924 8月 29 13:32 COPYRIGHT -rw-r--r--. 1 root root 5980 11月 12 23:36 Makefile drwxrwxr-x. 2 hacker 1003 203 8月 29 13:32 patches -rw-rw-r--. 1 hacker 1003 4689 8月 29 13:32 README.markdown -rw-rw-r--. 1 hacker 1003 8972 8月 29 13:32 README-windows.txt drwxrwxr-x. 2 hacker 1003 52 8月 29 13:32 util [root@Centos openresty-1.15.8.2]# ./configure --prefix=/usr/local/openresty --with-luajit --without-http_redis2_module --with-http_iconv_module --with-http_postgres_module ...(略去内容)... [root@Centos openresty-1.15.8.2]# gmake && gmake install ...(略去内容)...
3、编辑 nginx.conf 文件,编辑后内容为:
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
lua_shared_dict ups_zone 1m; # 定义upstream共享内存空间
upstream web-cluster {
server 127.0.0.1:8087;
server 127.0.0.1:8088;
}
upstream web-8087 {
server 127.0.0.1:8087;
}
upstream web-8088 {
server 127.0.0.1:8088;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /forward {
default_type text/html;
content_by_lua_block {
local host = ngx.req.get_uri_args()["host"]
local key = "switchKey"
ngx.shared.ups_zone:set(key, host)
local forward_ip = ngx.shared.ups_zone:get(key)
ngx.say("Successfully, forwarded host is: ", forward_ip)
}
}
location ~ /web/(.*) {
set_by_lua_block $my_ups {
local key = ngx.shared.ups_zone:get("switchKey")
if key == nil or key == "default" then
return "web-cluster"
else
local port = string.sub(key, -4)
if port == "8087" then
return "web-8087"
elseif port == "8088" then
return "web-8088"
end
end
}
proxy_pass http://$my_ups/$1;
}
}
}
4、测试是否生效:
(1)默认负载均衡模式:
[root@Centos conf]# curl http://127.0.0.1/forward?host=default Successfully, forwarded host is: default [root@Centos conf]# curl http://127.0.0.1/web/lua 8088 server [root@Centos conf]# curl http://127.0.0.1/web/lua 8087 server [root@Centos conf]# curl http://127.0.0.1/web/lua 8088 server [root@Centos conf]# curl http://127.0.0.1/web/lua 8087 server [root@Centos conf]# curl http://127.0.0.1/web/lua 8088 server [root@Centos conf]# curl http://127.0.0.1/web/lua 8087 server [root@Centos conf]# curl http://127.0.0.1/web/lua 8088 server [root@Centos conf]# curl http://127.0.0.1/web/lua 8087 server [root@Centos conf]#
(2)将所有请求转移到 8087 server:
[root@Centos conf]# curl http://127.0.0.1/forward?host=127.0.0.1:8087 Successfully, forwarded host is: 127.0.0.1:8087 [root@Centos conf]# curl http://127.0.0.1/web/lua 8087 server [root@Centos conf]# curl http://127.0.0.1/web/lua 8087 server [root@Centos conf]# curl http://127.0.0.1/web/lua 8087 server [root@Centos conf]# curl http://127.0.0.1/web/lua 8087 server [root@Centos conf]# curl http://127.0.0.1/web/lua 8087 server [root@Centos conf]# curl http://127.0.0.1/web/lua 8087 server [root@Centos conf]# curl http://127.0.0.1/web/lua 8087 server [root@Centos conf]#
(3)将所有请求转移到 8088 server:
[root@Centos conf]# curl http://127.0.0.1/forward?host=127.0.0.1:8088 Successfully, forwarded host is: 127.0.0.1:8088 [root@Centos conf]# curl http://127.0.0.1/web/lua 8088 server [root@Centos conf]# curl http://127.0.0.1/web/lua 8088 server [root@Centos conf]# curl http://127.0.0.1/web/lua 8088 server [root@Centos conf]# curl http://127.0.0.1/web/lua 8088 server [root@Centos conf]# curl http://127.0.0.1/web/lua 8088 server [root@Centos conf]# curl http://127.0.0.1/web/lua 8088 server [root@Centos conf]# curl http://127.0.0.1/web/lua 8088 server [root@Centos conf]#