nginx负载均衡案例(1)
生活随笔
收集整理的這篇文章主要介紹了
nginx负载均衡案例(1)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
nginx負載均衡案例詳解
隨著網站、應用訪問量的增加,一臺服務器已經不能滿足應用的需求,而需要多臺服務器集群,這時就會用到負載均衡,nginx負載均衡有多種算法,本篇文章通過舉例講解nginx負載均衡算法中的ip_hash算法,和與ip_hash有所不同的sticky_cookie_insert。
1、ip_hash
ip_hash使用源地址哈希算法,將同一客戶端的請求總是發往同一個后端服務器,除非該服務器不可用。
sticky_cookie_insert(見下文)
準備工作:兩臺安裝好nginx的服務器web-1和web-2,和一臺nginx負載均衡服務器。nginx安裝教程
第一臺:web-1
1、臨時關閉防火墻和selinux
[root@web-1 ~]# systemctl stop firewalld [root@web-1 ~]# setenforce 0 setenforce: SELinux is disabled2、切換到nginx發布目錄
[root@web-1 ~]# cd /usr/share/nginx/html/ [root@web-1 html]# ls 50x.html index.html3、寫入發布內容
[root@web-1 html]# echo "hello web-1" > index.html [root@web-1 html]# cat index.html hello web-14、重啟nginx服務
[root@localhost ~]# systemctl restart nginx5、瀏覽器訪問
web-1搭建完成!
第二臺:wen-2
1、關閉防火墻和selinux
[root@web-2 ~]# systemctl stop firewalld [root@web-2 ~]# setenforce 02、切換到nginx發布目錄
[root@web-2 ~]# cd /usr/share/nginx/html/ [root@web-2 html]# ls 50x.html index.html3、寫入發布內容
[root@web-2 html]# echo "hello web-2" > index.html [root@web-2 html]# cat index.html hello web-24、重啟nginx服務
[root@localhost ~]# systemctl restart nginx5、瀏覽器訪問
web-2搭建完成!
第三臺:負載均衡服務器
1、切換到/etc/nginx/nginx.conf配置文件添加:
[root@localhost ~]# vi /etc/nginx/nginx.confupstream myweb {server http://192.168.*.*;server http://192.168.*.*;ip_hash;}2、進入vi /etc/nginx/conf.d/default.conf文件中更改:
[root@localhost ~]# vi /etc/nginx/conf.d/default.conflocation / {proxy_pass http://myweb;}原來:
改成:
3、刷新配置文件
[root@localhost ~]# nginx -s reload負載均衡搭建完成!
注意:當客戶端訪問負載均衡服務器時,總是訪問同一臺web服務器,除非一臺web服務器不可用,才會訪問另外一臺web。
總結
以上是生活随笔為你收集整理的nginx负载均衡案例(1)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php nginx 负载均衡
- 下一篇: 蓝桥杯--基础(二)