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
总用量
drwxrwxr-x. hacker 11月 : build
drwxrwxr-x. hacker 8月 : bundle
-rwxrwxr-x. hacker 8月 : configure
-rw-rw-r--. hacker 8月 : COPYRIGHT
-rw-r--r--. root root 11月 : Makefile
drwxrwxr-x. hacker 8月 : patches
-rw-rw-r--. hacker 8月 : README.markdown
-rw-rw-r--. hacker 8月 : README-windows.txt
drwxrwxr-x. hacker 8月 : 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 ;
lua\_shared\_dict ups\_zone 1m; # 定义upstream共享内存空间
upstream web-cluster {
server 127.0.0.1:;
server 127.0.0.1:;
}
upstream web- {
server 127.0.0.1:;
}
upstream web- {
server 127.0.0.1:;
}
server {
listen ;
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, -)
if port == "" then
return "web-8087"
elseif port == "" 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
server
[root@Centos conf]# curl http://127.0.0.1/web/lua
server
[root@Centos conf]# curl http://127.0.0.1/web/lua
server
[root@Centos conf]# curl http://127.0.0.1/web/lua
server
[root@Centos conf]# curl http://127.0.0.1/web/lua
server
[root@Centos conf]# curl http://127.0.0.1/web/lua
server
[root@Centos conf]# curl http://127.0.0.1/web/lua
server
[root@Centos conf]# curl http://127.0.0.1/web/lua
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:
[root@Centos conf]# curl http://127.0.0.1/web/lua
server
[root@Centos conf]# curl http://127.0.0.1/web/lua
server
[root@Centos conf]# curl http://127.0.0.1/web/lua
server
[root@Centos conf]# curl http://127.0.0.1/web/lua
server
[root@Centos conf]# curl http://127.0.0.1/web/lua
server
[root@Centos conf]# curl http://127.0.0.1/web/lua
server
[root@Centos conf]# curl http://127.0.0.1/web/lua
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:
[root@Centos conf]# curl http://127.0.0.1/web/lua
server
[root@Centos conf]# curl http://127.0.0.1/web/lua
server
[root@Centos conf]# curl http://127.0.0.1/web/lua
server
[root@Centos conf]# curl http://127.0.0.1/web/lua
server
[root@Centos conf]# curl http://127.0.0.1/web/lua
server
[root@Centos conf]# curl http://127.0.0.1/web/lua
server
[root@Centos conf]# curl http://127.0.0.1/web/lua
server
[root@Centos conf]#
参考书籍:《OpenResty最佳实践》PDF版,《OpenResty完全开发指南》
手机扫一扫
移动阅读更方便
你可能感兴趣的文章