七層負載&redis

                                                          #負載均衡      

負載均衡搭建

在反向代理的基礎(chǔ)上,定義一個服務(wù)器組,將墮胎服務(wù)器ip輸入組內(nèi),location內(nèi)include直接取值。

root@lb01 conf.d]# vim lb.conf 
upstream server {
                server 172.16.1.7;
                server 172.16.1.8;
        }
server {
        listen 80;
        server_name nginx.jty.com;

        location / {
        proxy_pass http://server;
        include proxy_params;
        }
}

負載均衡常見典型故障

如果后臺服務(wù)連接超時,Nginx是本身是有機制的,如果出現(xiàn)一個節(jié)點down掉的時候,Nginx會更據(jù)你具體負載均衡的設(shè)置,將請求轉(zhuǎn)移到其他的節(jié)點上,但是,如果后臺服務(wù)連接沒有down掉,但是返回錯誤異常碼了如:504、502、500,這個時候你需要加一個負載均衡的設(shè)置,如下:proxy_next_upstream http_500 | http_502 | http_503 | http_504 |http_404;意思是,當(dāng)其中一臺返回錯誤碼404,500…等錯誤時,可以分配到下一臺服務(wù)器程序繼續(xù)處理,提高平臺訪問成功率。

server {
    listen 80;
    server_name www.oldboy.com;

    location / {
        proxy_pass http://node;
        proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
    }
}

Nginx負載均衡調(diào)度算法

輪詢                按時間順序逐一分配到不同的后端服務(wù)器(默認(rèn))
weight        加權(quán)輪詢,weight值越大,分配到的訪問幾率越高
ip_hash       每個請求按訪問IP的hash結(jié)果分配,這樣來自同一IP的固定訪問一個后端服務(wù)器
url_hash          按照訪問URL的hash結(jié)果來分配請求,是每個URL定向到同一個后端服務(wù)器
least_conn      最少鏈接數(shù),那個機器鏈接數(shù)少就分發(fā)
Nginx負載均衡[rr]輪詢具體配置

默認(rèn)就是輪詢狀態(tài),無需多配置
upstream load_pass {
server 10.0.0.7:80;
server 10.0.0.8:80;
}

Nginx負載均衡[wrr]權(quán)重輪詢具體配置

weight=x,則兩臺服務(wù)器訪問比重為x:1
upstream load_pass {
server 10.0.0.7:80 weight=5;
server 10.0.0.8:80;
}

Nginx負載均衡ip_hash

具體配置不能和weight一起使用。
ip_hash會導(dǎo)致一臺服務(wù)器訪問壓力大,但可以解決session問題(可以保存用戶賬號密碼,使用戶下次訪問無需輸入賬號密碼)。

#如果客戶端都走相同代理, 會導(dǎo)致某一臺服務(wù)器連接過多
upstream load_pass {
    ip_hash;
    server 10.0.0.7:80 weight=5;
    server 10.0.0.8:80;
}
Nginx負載均衡后端狀態(tài)

后端Web服務(wù)器在前端Nginx負載均衡調(diào)度中的狀態(tài)

down 當(dāng)前的server暫時不參與負載均衡
backup 預(yù)留的備份服務(wù)器(當(dāng)其他服務(wù)器故障時,才會使用這臺預(yù)留的備份服務(wù)器)
max_fails 允許請求失敗的次數(shù)
fail_timeout 經(jīng)過max_fails失敗后, 服務(wù)暫停時間
max_conns 限制最大的接收連接數(shù)

Nginx負載均衡會話保持

1.配置Nginx
server {
    listen 80;
    server_name php.jty.com;
         root /code/admin;
         index index.php index.html;

    location ~ \.php$ {
        root /code/zh;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
        }
}
2.安裝phpmyadmin (web01和web02上都裝)
3.配置phpmyadmin連接遠程的數(shù)據(jù)庫
[root@web01 code]# cd admin/
[root@web01 admin]# cp config.sample.inc.php config.inc.php
[root@web01 admin]# vim config.inc.php
/* Server parameters */
$cfg['Servers'][$i]['host'] = '172.16.1.51';
第30行
30 $cfg['Servers'][$i]['host'] = '172.16.1.51';
4.配置授權(quán)

[root@web01 conf.d]# chown -R www.www /var/lib/php/

使用瀏覽器訪問頁面,獲取cookie信息

[root@web01 admin
]# ll /var/lib/php/session/
總用量 4
-rw-------. 1 www www 2424 8月 21 18:41 sess_80a339880e754b1919d3004754f40f15

5.將web01上配置好的phpmyadmin以及nginx的配置文件推送到web02主機上
[root@web01 code]# scp -rp  phpMyAdmin-4.8.4-all-languages root@172.16.1.8:/code/
[root@web01 code]# scp /etc/nginx/conf.d/php.conf  root@172.16.1.8:/etc/nginx/conf.d/
6.在web02上重載Nginx服務(wù)

[root@web02 code]# systemctl restart nginx

7.授權(quán)

[root@web02 code]# chown -R www.www /var/lib/php/

8.接入負載均衡

[root@lb01 conf.d]# vim proxy_php.com.conf
upstream php {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
server {
listen 80;
server_name php.oldboy.com;
location / {
proxy_pass http://php;
include proxy_params;
}
}

[root@lb01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 conf.d]# systemctl restart nginx

使用負載均衡的輪詢功能之后,會發(fā)現(xiàn),如果將session保存在本地文件的話,永遠都登錄不上去php.oldboy.com。這時候要使用redis解決會話登錄問題。

使用redis解決會話登錄問題
1.安裝redis內(nèi)存數(shù)據(jù)庫

[root@db01 ~]# yum install redis -y
2.配置redis監(jiān)聽在172.16.1.0網(wǎng)段上

[root@db01 ~]# sed -i '/^bind/c bind 127.0.0.1 172.16.1.51' /etc/redis.conf
3.啟動redis

[root@db01 ~]# systemctl start redis
[root@db01 ~]# systemctl enable redis
4.php配置session連接redis

1.修改/etc/php.ini文件

[root@web ~]# vim /etc/php.ini
session.save_handler = redis
session.save_path = "tcp://172.16.1.51:6379"
;session.save_path = "tcp://172.16.1.51:6379?auth=123" #如果redis存在密碼,則使用該方式
session.auto_start = 1

2.注釋php-fpm.d/www.conf里面的兩條內(nèi)容,否則session內(nèi)容會一直寫入/var/lib/php/session目錄中

;php_value[session.save_handler] = files
;php_value[session.save_path] = /var/lib/php/session
5.重啟php-fpm

[root@web01 code]# systemctl restart php-fpm
6.將web01上配置好的文件推送到web02

[root@web01 code]# scp /etc/php.ini root@172.16.1.8:/etc/php.ini
[root@web01 code]# scp /etc/php-fpm.d/www.conf root@172.16.1.8:/etc/php-fpm.d/www.conf
5.上web02上重啟php-fpm

[root@web02 code]# systemctl restart php-fpm

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容