如何使用nginx做負載平衡

如何使用nginx做負載平衡

我需要同時使用伺服器作為負載平衡器和網路伺服器。

這是我的網站 nginx 設定:

upstream load_balancer {
    least_conn;
    server localhost;
        #server srv2.example.com;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name localhost;

    root /var/www;

    index index.php index.html index.htm;

    location / {
        proxy_pass http://load_balancer;
    }

    location ~* \.php$ {
        fastcgi_index   index.php;
            fastcgi_pass    unix:/var/run/php5-fpm.sock;
            include         fastcgi_params;
            fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
    }
}

是否可以同時使用一台伺服器作為負載平衡器和 Web 伺服器?怎麼做?

先非常感謝!

答案1

對的,這是可能的;

您可以使用不同的連接埠建立虛擬主機。

(Load Balancer [Port 80] + Virtualhost [Port 81])因此,它實際上會讓一台伺服器監聽兩個連接埠;一台提供網頁服務,一台負責負載平衡。

然後,當將伺服器上的網站連結到負載平衡器時,您將使用 IPlocalhost:81

您可以透過鍵入以下內容來建立本地網站:

sudo nano /etc/nginx/sites-available/example.com

然後貼上(可能需要根據需要編輯。)

 server {
        listen   81;

        root /var/www/example.com/public_html;
        index index.html index.htm;

        server_name example.com;
}

希望這可以幫助!

相關內容