除了把 Nginx (音同 Engine X) 當 Web (HTTP) 伺服器使用外,還可以把它當作一台 負載平衡器 load Balancer.
測試環境 工作平台為 CentOS 7.2 最小安裝. 全部都已安裝 Nginx ,基礎安裝請參考 https://benjr.tw/95761
- Nginx load Balancer : 172.16.15.201
- Nginx Web Server Node1 : 172.16.15.199
- Nginx Web Server Node2 : 172.16.15.200
設定參考文件說明 http://blog.hellojcc.tw/2015/12/07/nginx-beginner-tutorial/
為了確認 Nginx load Balancer 是有效果的,所以修改了 Nginx Web Server Node1 與 Node2 的 /usr/share/nginx/html/index.html 內容 Welcome to nginx-Node#! .
Nginx Web Server Node1 : 172.16.15.199
[root@Node1 ~]# vi /usr/share/nginx/html/index.html <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx-Node1!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
Nginx Web Server Node2 : 172.16.15.200
[root@Node2 ~]# vi /usr/share/nginx/html/index.html <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx-Node2!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
Nginx load Balancer : 172.16.15.201
Load balancing 模式有三種,預設為 round-robin
- round-robin — requests to the application servers are distributed in a round-robin fashion,
Round Robin 是標準輪詢的方式,還可以讓我們自行設定哪一台 server 可以承受較大的負載 (weight) ,下面採用這種方式. - least-connected — next request is assigned to the server with the least number of active connections.
當有新的連線需求產生時,Least Connected 會把連線需求導向目前哪一台 server 連線數最少的.我們可以自行設定哪一台 server 可以承受較大的負載 (weight). - ip-hash — a hash-function is used to determine what server should be selected for the next request (based on the client’s IP address).
會依據 Client 來選擇負載分配,不同的 Client 會分配給不同的 Server.也可以設定 負載 (weight) .
nginx 的設定檔位於 /etc/nginx/nginx.conf 裡面定義了 include /etc/nginx/conf.d/*.conf 所以我們修改 /etc/nginx/conf.d/default.conf 即可,default.conf 應該已經定義了 web server 的相關設定,直接備份下來從新建立 default.conf 如下.
[root@localhost ~]# vi /etc/nginx/conf.d/default.conf upstream ben { server 172.16.15.199 weight=1; server 172.16.15.200 weight=2; } server { listen 80; location / { proxy_pass http://ben; } }
設定區分為兩個區塊
- upstream
雖然沒有定義 Load balancing 模式,系統會直接用預設的 round-robin.這裡定義了兩台 server ,負載 (weight) 也不同. - server
listen 80 – http 80 port 使用 Load balancing.
proxy_pass http://ben; – 前面設定的 upstream ben
順便檢查一下設定檔吧!
[root@localhost ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
透過瀏覽器多 reload 重載幾次網頁,就可以看到 Node1 , Node2 的網頁(如前面幾張圖所示),因為 Node 的 weight 比較高,所以三次 重載網頁可以看到兩次.
沒有解決問題,試試搜尋本站其他內容