第十二周-day52(上)-負(fù)載均衡的設(shè)備轉(zhuǎn)換、動(dòng)靜分離

第十二周-day52-設(shè)備轉(zhuǎn)發(fā)、動(dòng)靜分離、Keepalived高可用.png

一、準(zhǔn)備環(huán)境

web01上

[root@web01 ~]# echo 'this is PC website' >/app/www/lidao.html

web02上

[root@web02 ~]# echo 'this is Mobile website' >/app/www/lidao.html

lb01上curl一下


二、根據(jù)用戶客戶端的設(shè)備 進(jìn)行轉(zhuǎn)發(fā) 請(qǐng)求:

1.配置 upstream 與location

定義upstream移動(dòng)端與PC端

 upstream  default {
     server 10.0.0.7:80 weight=1 max_fails=3 fail_timeout=10s;
     }
     upstream mobile {
     server 10.0.0.8:80 weight=1 max_fails=3 fail_timeout=10s;
     }

移動(dòng)端的服務(wù)器池

     location / {
        if ($http_user_agent ~* "Android|IOS") {
        proxy_pass http://mobile;
        }

完整配置

[root@lb01 nginx]# vim nginx.conf 
...
     upstream  default {
     server 10.0.0.7:80 weight=1 max_fails=3 fail_timeout=10s;
     }
     upstream mobile {
     server 10.0.0.8:80 weight=1 max_fails=3 fail_timeout=10s;
     }
#    include /etc/nginx/conf.d/*.conf;
     server {
     listen 80;
     server_name www.oldboy.com;
     location / {
        if ($http_user_agent ~* "Android|IOS") {
        proxy_pass http://mobile;
        }
         proxy_pass http://default;
         proxy_set_header Host $host;
         proxy_set_header X-Forwarded-For $remote_addr;
        }
     }
[root@lb01 nginx]# 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 nginx]# systemctl reload nginx

記得將之前環(huán)境中的其他模塊一起修改或暫時(shí)注釋掉,不然語(yǔ)法會(huì)報(bào)錯(cuò)

2.curl一下查看結(jié)果

curl -A 可以指定系統(tǒng)

[root@lb01 nginx]# curl 10.0.0.5/lidao.html
this is PC website
[root@lb01 nginx]# curl -A ios 10.0.0.5/lidao.html
this is Mobile website
[root@lb01 nginx]# curl -A Android 10.0.0.5/lidao.html
this is Mobile website

3.可以下載火狐瀏覽器查看

http://www.firefox.com.cn/
安裝插件流程:

三、根據(jù) URI 中的目錄地址實(shí)現(xiàn)代理轉(zhuǎn)發(fā)(動(dòng)靜分離)

添加一臺(tái)測(cè)試web03節(jié)點(diǎn)—10.0.0.9
將web03的配置與web01和web02配置相同
(/app站點(diǎn)目錄與nginx.conf配置文件)

模仿李導(dǎo)繪圖思路

1.準(zhǔn)備環(huán)境

www.oldboy.com/upload/index.html
www.oldboy.com/static/index.html
www.oldboy.com/index.html

#web01:
mkdir -p /app/www/upload/index.html
echo this is upload >/app/www/index.html
[root@web01 ~]# cat /app/www/upload/index.html 
this is upload

#web02:
mkdir -p /app/www/static/index.html
echo this is static >/app/www/index.html
[root@web02 ~]# cat /app/www/static/index.html 
this is static

#web03:
mkdir -p /app/www/index.html  #之前已經(jīng)有首頁(yè)文件,只需修改內(nèi)容
echo this is default >/app/www/index.html
[root@web03 ~]# cat /app/www/index.html 
this is default

2.配置 upstream 與location

定義upstream.

     upstream  upload {
     server 10.0.0.7:80 weight=1 max_fails=3 fail_timeout=10s;
     }
     upstream static {
     server 10.0.0.8:80 weight=1 max_fails=3 fail_timeout=10s;
     }
     upstream default {
     server 10.0.0.9:80 weight=1 max_fails=3 fail_timeout=10s;
     }

添加location

     server {
     listen 80;
     server_name www.oldboy.com;
     location /upload {
         proxy_pass http://upload;
         proxy_set_header Host $host;
         proxy_set_header X-Forwarded-For $remote_addr;
        }
     location /static {
         proxy_pass http://static;
         proxy_set_header Host $host;
         proxy_set_header X-Forwarded-For $remote_addr;
        }
     location /default {
         proxy_pass http://default;
         proxy_set_header Host $host;
         proxy_set_header X-Forwarded-For $remote_addr;
        }
     }

完整配置


[root@lb01 nginx]# vim nginx.conf 
....
     upstream  upload {
     server 10.0.0.7:80 weight=1 max_fails=3 fail_timeout=10s;
     }
     upstream static {
     server 10.0.0.8:80 weight=1 max_fails=3 fail_timeout=10s;
     }
     upstream default { 
     server 10.0.0.9:80 weight=1 max_fails=3 fail_timeout=10s;
     }
#    include /etc/nginx/conf.d/*.conf;
     server {
     listen 80;
     server_name www.oldboy.com;
     location /upload {
         proxy_pass http://upload;
         proxy_set_header Host $host;
         proxy_set_header X-Forwarded-For $remote_addr;
        }
     location /static {
         proxy_pass http://static;
         proxy_set_header Host $host;
         proxy_set_header X-Forwarded-For $remote_addr;
        }
     location / {
         proxy_pass http://default;
         proxy_set_header Host $host;
         proxy_set_header X-Forwarded-For $remote_addr;
        }
     }
}
[root@lb01 nginx]# 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 nginx]# systemctl reload nginx

3.瀏覽器測(cè)試一下

上傳

靜態(tài)
動(dòng)態(tài)(默認(rèn))

四、輪詢算法

ip_hash
只要客戶端ip地址相同就會(huì)被轉(zhuǎn)發(fā)到同一臺(tái)機(jī)器上


六、cookie與session會(huì)話區(qū)別

會(huì)話保持
cookie
session

1.共同點(diǎn)

存放用戶信息
key value類型 變量和變量?jī)?nèi)容

2.區(qū)別

cookie:
存放在瀏覽器
為保證安全性,存放簡(jiǎn)單信息、鑰匙
開(kāi)發(fā)設(shè)置
響應(yīng)服務(wù)器給你設(shè)置

session:
存放在服務(wù)器  
存放敏感信息
存放鎖頭


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

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

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