使用nginx进行负载均衡和接口定向分发
jopen
11年前
现有项目中使用到了nginx进行负载均衡和接口的定向分发。
客户端对服务端接口的访问是restful风格的url,例如
http://biz.abc.com:27788/interface/A1000/
http://biz.abc.com:27788/interface/A2000/
系统接口按类别分为核心业务接口和查询业务接口,在部署上按接口规模和类别,分布在不同的应用集群中。
在前端,部署了nginx服务器进行接口的负载均衡和定向分发。
nginx配置示例如下:
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; include loadbalancing.conf; server { listen 8808; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
loadbalancing.conf配置如下:
#负载均衡配置 map $zone $loadbalancing { A1000 server_main_func_a; A2000 server_main_func_b; A3000 server_main_query_a; A4000 server_main_query_b; default default_server; } #主服务器组a upstream server_main_func_a { server 192.168.100.1:7788; server 192.168.100.2:7788; server 192.168.100.3:7788; } #主服务器组b upstream server_main_func_b { server 192.168.100.51:7788; server 192.168.100.52:7788; server 192.168.100.53:7788; } #查询服务器组a upstream server_main_query_a { server 192.168.100.101:7788; server 192.168.100.102:7788; server 192.168.100.103:7788; } #查询服务器组b upstream server_main_query_b { server 192.168.100.151:7788; server 192.168.100.152:7788; server 192.168.100.153:7788; } #缺省服务器组 upstream default_server { server 192.168.100.201:7788; server 192.168.100.202:7788; server 192.168.100.203:7788; } server { listen 27788; server_name biz.abc.com; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } #带命令字地址访问 location ~ /interface/ { proxy_pass_header Server; proxy_set_header Host $http_host; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Scheme $scheme; if ( $request_uri ~ ^/interface/(\w*) ) { set $zone $1; proxy_pass http://$loadbalancing; } } location = /interface { proxy_pass_header Server; proxy_set_header Host $http_host; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Scheme $scheme; set $zone $request_uri; proxy_pass http://$loadbalancing; } }
对于不同类别的接口,应用层面都有一个集群来处理,实现负载。
-----------------
当系统某些应用功能更新时,不会影响用户的其他功能的使用。