1, the installation openresty dependent modules:
[root@Centos opt]# yum -y install pcre-devel openssl openssl-devel postgresql-devel
2, compile and install openresty:
[root @ Centos opt] # tar -zxvf openresty- 1.15 . 8.2 . tar .gz ... (content omitted) ... [root @ Centos opt] # cd openresty - 1.15 . 8.2 / [root @ Centos openresty - 1.15 . 8.2 ] # LL total volume of 112 drwxrwxr the -X-. 47 Hacker 1003 4096 . 11 dated 12 is 23 is : 35 Build drwxrwxr the -X-. 46 is Hacker 1003 4096 . 8 dated 29 13 is : 33 is 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 ... (content omitted) ... [root @ Centos openresty - 1.15 . 8.2 ] # && gmake gmake install ... (content omitted) ...
3, edit nginx.conf file, edit the content as follows:
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, test whether the effect:
(1) The default load balancing mode:
[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) all requests to transfer 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) all requests to transfer 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]#
Reference books: "OpenResty Best Practices" PDF version, "OpenResty entirely Development Guide"