如何在Linux中使用nginx設置負載平衡

本篇文章給大家介紹的內容是關于如何在linux中使用nginx設置負載平衡,下面我們來看具體的內容。

如何在Linux中使用nginx設置負載平衡

先決條件

必須具有root訪問權限或sudo訪問權限。使用權限訪問連接你的服務器控制臺。在后端服務器上配置你的站點。

步驟1:安裝nginx服務器

首先,使用ssh訪問登錄到你的服務器,Windows用戶可以在服務器中使用putty或ssh的替代方法。現在使用Linux軟件包管理器安裝nginx。nginx包在默認的yum和apt存儲庫下可用。

使用Apt-get:

$?sudo?apt-get?install?nginx

使用Yum:

$?sudo?yum?install?nginx

使用DNF:

$?sudo?dnf?install?nginx

步驟2:設置虛擬主機

讓我們為域創建一個nginx虛擬主機配置文件。下面是最小設置配置文件。

/etc/nginx/conf.d/www.example.com.conf

upstream?remote_servers??{ ???server?remote1.example.com; ???server?remote2.example.com; ???server?remote3.example.com; } server?{ ???listen???80; ???server_name??example.com?www.example.com; ???location?/?{ ?????proxy_pass??http://remote_servers; ???} }

步驟3:其他有用指令

還可以使用一些更有用的設置來使用nginx自定義和優化負載均衡器。例如set、weight和ip散列(哈希),如下面的配置。

Weight

upstream?remote_servers??{ ???server?remote1.example.com?weight=1; ???server?remote2.example.com?weight=2; ???server?remote3.example.com?weight=4; }

IP Hash

upstream?remote_servers?{ ???ip_hash; ???server???remote1.example.com; ???server???remote2.example.com; ???server???remote3.example.com??down; ?}

步驟4:重新啟動nginx服務

完成所有更改后,使用以下命令重新啟動nginx服務。

$?sudo?systemctl?restart?nginx.service

本篇文章到這里就已經全部結束了,更多其他精彩內容可以關注php中文網的其他相關欄目教程!!!

? 版權聲明
THE END
喜歡就支持一下吧
點贊14 分享