单个Nginx负载平衡器,用于2个不同的集群(web,api)

我想知道是否有可能使用单个Nginx服务器来负载均衡我的Web服务器和API服务器,如果两者都使用端口80.在这个设置中,将有5个服务器,1个Nginx和4个Apache服务器。 当访问web.example.com时,我希望Web服务器保持平衡。 同样的,我希望api服务器在api.example.com访问时保持平衡。

这是可能的还是我需要另一个nginx服务器?

你有两种可能的方法:

1.两个农场的单个VIP:

在这种情况下,您的VIP将成为您的NGinx服务器单个IP地址。

 http { upstream web.example.com { least_conn; server mywebserver1.loc; server mywebserver2.loc; server mywebserver3.loc; } upstream api.example.com { least_conn; server myapiserver1.loc; server myapiserver2.loc; server myapiserver3.loc; } server { listen 80; server_name web.example.com; location / { proxy_pass http://web.example.com } server { listen 80; server_name api.example.com; location / { proxy_pass http://api.example.com } } 

2.每个农场都有专属的VIP

在这种情况下,您需要在NGinx主机上有两个IP地址。

我们说:

  • Web(eth0)192.168.1.1
  • Api(eth1)的192.168.1.2

     http { upstream web.example.com { least_conn; server mywebserver1.loc; server mywebserver2.loc; server mywebserver3.loc; } upstream api.example.com { least_conn; server myapiserver1.loc; server myapiserver2.loc; server myapiserver3.loc; } server { listen 192.168.1.1:80; # <-- vHost listen on IP server_name web.example.com; location / { proxy_pass http://web.example.com } server { listen 192.168.1.2:80; # <-- vHost listen on IP server_name api.example.com; location / { proxy_pass http://api.example.com } } 

然后,您有多个选项来pipe理upstream指令中的负载平衡和故障转移,如:

  • weight
  • max_fails
  • fail_timeout

http://wiki.nginx.org/NginxHttpUpstreamModule#upstream

另外,您有多种负载平衡方法:

  • least-connected
  • Session persistence

http://nginx.org/en/docs/http/load_balancing.html