nginx负载平衡 - 我是否必须拥有API的多个副本?

时间:2016-05-13 10:21:35

标签: api nginx parse-server

我想使用NGINX

来平衡我的Parse API

我的API目前在一台服务器上的nginx上运行,如果我想对其进行负载均衡,我是否必须在每台主机上托管我的API?

我希望它看起来像这样

----------                             /----- [ api-0.myhost.com ]
| client | --------> [ api.myhost.com ] ----- [ api-1.myhost.com ]
----------                             \----- [ api-2.myhost.com ]

在这种情况下,我是否必须安装nginx并将我的API部署到非常api-X.myhost.com

或者我只是在api.myhost.comapi-X.myhost.com上部署我的API我只是安装nginx?

1 个答案:

答案 0 :(得分:0)

您只需在api.host.com上安装nginx,然后配置nginx进行负载均衡,如下所示:

upstream api-app {
              least_conn;
              server api-0.myhost.com:port weight=1 max_fails=1;
              server api-2.myhost.com:port weight=1 max_fails=1;
              server api-3.myhost.com:port weight=1 max_fails=1;   

        }

        server {
              listen 80;
              listen  443 ssl;
              server_name api.myhost.com;



              ssl_certificate /etc/ssl/certs/api_ssl-bundle.crt;
              ssl_certificate_key /etc/ssl/private/api_com.key;

              client_max_body_size 2000M;
              large_client_header_buffers 32 128k;
              location / {
               proxy_set_header X-Real-IP $remote_addr;
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

                proxy_buffers 64 128k;
                proxy_buffer_size 256k;
                proxy_pass http://api-app;
                proxy_connect_timeout   1200;
                proxy_send_timeout      1200;
                proxy_read_timeout      1200;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "";
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
              }
        }

port是您的api-app在其主机上收听的端口。