2019-10-14day40 七層 +四層負載均衡

Nginx day06 七層負載均衡+四層負載均衡

1.七層負載均衡:

? 根據(jù)url 調度不同的集群 url.oldxu.com
? 10.0.0.5
? 10.0.0.7 /pass
? 10.0.0.8 /user

1.web01和web02配置 (只不過代碼不一樣)

[root@web01 conf.d]# cat url.oldxu.com.conf 
server {
    listen 80;
    server_name url.oldxu.com;
    root /code;

    location / {
        index index.html;
}
}

2.lb配置 172.16.1.5

[root@lb01 conf.d]# cat proxy_url.oldxu.com.conf 
upstream user {
    server 172.16.1.8;
}
upstream pass {
    server 172.16.1.7;
}

    server {
        listen 80;
        server_name url.oldxu.com;
        location / {
        proxy_pass http://user/;
        include proxy_params;
}
        location /user {
        proxy_pass http://user/;
}
        location /pass {
        proxy_pass http://pass/;
        include proxy_params;
}
    
}

3.重啟服務

[root@lb01 conf.d]# systemctl restart nginx

PS: 在使用proxy_pass反向代理時,最后結尾添加/和不添加/有什么區(qū)別?

1.不添加 / 
    用戶如果請求:    http://url.oldxu.com/user
    會被代理至后端:  http://url.oldxu.com/user

1.添加 / 
    用戶如果請求: http://url.oldxu.com/user
    會被代理至后端:  http://url.oldxu.com/

2.根據(jù)設備調度不同的集群 ( 瀏覽器 ) ( 手機 )

? 10.0.0.5
? 10.0.0.7 pc
? 10.0.0.8 phone

1.所有的web都需要配置 ( 代碼不一樣)

[root@web01 conf.d]# cat /etc/nginx/conf.d/agent.oldxu.com.conf 
server {
    listen 80;
    server_name agent.oldxu.com;
    root /code;

    location / {
        index index.html;
}
}

2.代理的配置

[root@lb01 conf.d]# cat proxy_agent.oldxu.com.conf 
upstream pc {
    server 172.16.1.7:80;
}

upstream phone {
    server 172.16.1.8:80;
}

    server {
        listen 80;
    server_name agent.oldxu.com;
    location / {
    #默認都走pc
    proxy_pass http://pc;
    include proxy_params;
    default_type text/html;
    charset utf-8;
    #如果是安卓或者iPhone恒傻,則走phone
    if ( $http_user_agent ~* "android|iphone|iPad" ) {
    proxy_pass http://phone;
}
    #如果是IE瀏覽器,要么拒絕,要么返回一個好的瀏覽器下載頁面
    if ( $http_user_agent ~*  "Trident" ) {
    return 200 '<a  target="_blank">點擊下載正版瀏覽器google.exe</a>';
}
}
}

多級負載下如何透傳真實客戶端IP? ( 提供視頻 )
x-forwar
realip (知道經過了那些代理 代理的IP又是多少)

3.四層負載均衡

1.什么是四層 OSI 傳輸層 TCP/IP UDP/TCP

? 四層是基于轉發(fā)方式:

image.png

2.四層負載均衡使用場景

? 1.四層負載均衡 + 七層負載均衡
? 2.dns + 多機房 + 四層負載均衡+七層負載均衡
? 3.SOA 松耦合架構
? 登錄 passport.jd.com
? 注冊 reg.jd.com
? 商品詳情 pro.jd.com

四層負載均衡+七層負載均衡+web集群+NFS+Redis
image.png

10.0.0.4
nginx是1.9版本以后才引入的四層負載均衡

stream模塊實現(xiàn),但stream不能出現(xiàn)在http層
--with-stream
-with-stream_ssl_module
-with-stream_realip_module

stream {
            upstream backend {
                hash $remote_addr consistent;
                server backend1.example.com:12345 weight=5;
                server 127.0.0.1:12345 max_fails=3 fail_timeout=30s;
                server unix:/tmp/backend3;
            }
            server {
                listen 12345;
                proxy_connect_timeout 1s;
                proxy_timeout 3s;
                proxy_pass backend;
            }
        }

nginx四層+nginx七層+web集群--->場景

1.定義四層配置文件路徑:

[root@lb-4 nginx]# vim /etc/nginx/nginx.conf
    include /etc/nginx/conf.c/*.conf;   

2.進行初始化操作

[root@lb-4 ~]# rm -f /etc/nginx/conf.d/default.conf
[root@lb-4 nginx]# mkdir /etc/nginx/conf.c

3.配置四層負載均衡

[root@lb-4 ~]# cat /etc/nginx/conf.c/all.conf 
    stream {
        upstream blog {
            server 172.16.1.5:80;
            server 172.16.1.6:80;
        }

        server {
            listen 80;
            proxy_pass blog;
            proxy_timeout 3s;
            proxy_connect_timeout 3s;
        }
    }

基于端口的轉發(fā)

需求: 用戶連接10.0.0.4的6666端口,其實連接的是172.16.1.7的22/TCP端口
需求: 用戶連接10.0.0.4的5555端口,其實連接的是172.16.1.51的3306/TCP端口

(先拔掉7和51的公網(wǎng)IP)

                    nginx 7層        web01       MySQL
    nginx 4層  +                     web02       NFS
                    nginx 7層        web03       Redis
                    10.0.0.6

10.0.0.4

nginx是1.9版本以后才引入的四層負載均衡
stream模塊實現(xiàn),但stream不能出現(xiàn)在http層

            --with-stream
            -with-stream_ssl_module
            -with-stream_realip_module
示例
stream {
            upstream backend {
                hash $remote_addr consistent;
                server backend1.example.com:12345 weight=5;
                server 127.0.0.1:12345 max_fails=3 fail_timeout=30s;
                server unix:/tmp/backend3;
            }
            server {
                listen 12345;
                proxy_connect_timeout 1s;
                proxy_timeout 3s;
                proxy_pass backend;
            }
        }

nginx四層+nginx七層+web集群--->場景

1.定義四層配置文件路徑

[root@lb-4 nginx]# vim /etc/nginx/nginx.conf
    include /etc/nginx/conf.c/*.conf;   (加到http層外邊)

2.進行初始化操作

[root@lb-4 ~]# rm -f /etc/nginx/conf.d/default.conf
[root@lb-4 nginx]# mkdir /etc/nginx/conf.c

3.配置四層負載均衡

[root@lb-4 ~]# cat /etc/nginx/conf.c/all.conf 
    stream {
        upstream blog {
            server 172.16.1.5:80;
            server 172.16.1.6:80;
        }

        server {
            listen 80;
            proxy_pass blog;
            proxy_timeout 3s;
            proxy_connect_timeout 3s;
        }
    }

4.基于端口的轉發(fā)

需求: 用戶連接10.0.0.4的6666端口,其實連接的是172.16.1.7的22/TCP端口
需求: 用戶連接10.0.0.4的5555端口,其實連接的是172.16.1.51的3306/TCP端口

[root@web03 ~]# cat /etc/nginx/conf.c/all.conf 
stream {
    upstream blog {
        server 172.16.1.5:80;
        server 172.16.1.6:80;
}
    
    upstream ssh {
        server 172.16.1.7:22;
}
upstream mysql {
    server 172.16.1.51:3306;
}

server {
    listen 6666;
    proxy_pass ssh;
}

            server {
                listen 5555;
                proxy_pass mysql;
            }
    server {
        listen 80;
        proxy_pass blog;
        proxy_timeout 3s;
        proxy_connect_timeout 3s;
}
}


5.四層負載均衡怎么記錄日志 必須在stream層,不能出現(xiàn)在http層?

[root@web03 ~]# cat /etc/nginx/conf.c/all.conf 
stream {

log_format  proxy '$remote_addr -  [$time_local]  $status $protocol'
       '   "$upstream_addr" "$upstream_bytes_sent" "$upstream_connect_time"' ;
        access_log /var/log/nginx/tcp.log proxy;
    upstream blog {
        server 172.16.1.5:80;
        server 172.16.1.6:80;
}
    
    upstream ssh {
        server 172.16.1.7:22;
}
upstream mysql {
    server 172.16.1.51:3306;
}

server {
    listen 6666;
    proxy_pass ssh;
}

            server {
                listen 5555;
                proxy_pass mysql;
            }
    server {
        listen 80;
        proxy_pass blog;
        proxy_timeout 3s;
        proxy_connect_timeout 3s;
}
}


?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市徘六,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌待锈,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,865評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件和屎,死亡現(xiàn)場離奇詭異春瞬,居然都是意外死亡柴信,警方通過查閱死者的電腦和手機快鱼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,296評論 3 399
  • 文/潘曉璐 我一進店門纲岭,熙熙樓的掌柜王于貴愁眉苦臉地迎上來线罕,“玉大人,你說我怎么就攤上這事钞楼。” “怎么了燃乍?”我有些...
    開封第一講書人閱讀 169,631評論 0 364
  • 文/不壞的土叔 我叫張陵宛琅,是天一觀的道長刻蟹。 經常有香客問我嘿辟,道長,這世上最難降的妖魔是什么红伦? 我笑而不...
    開封第一講書人閱讀 60,199評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮召调,結果婚禮上,老公的妹妹穿的比我還像新娘唠叛。我一直安慰自己,他們只是感情好玻墅,可當我...
    茶點故事閱讀 69,196評論 6 398
  • 文/花漫 我一把揭開白布壮虫。 她就那樣靜靜地躺著环础,像睡著了一般。 火紅的嫁衣襯著肌膚如雪线得。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,793評論 1 314
  • 那天贯钩,我揣著相機與錄音办素,去河邊找鬼祸穷。 笑死,一個胖子當著我的面吹牛雷滚,可吹牛的內容都是我干的。 我是一名探鬼主播祈远,決...
    沈念sama閱讀 41,221評論 3 423
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼谋减!你這毒婦竟也來了扫沼?” 一聲冷哼從身側響起逃顶,我...
    開封第一講書人閱讀 40,174評論 0 277
  • 序言:老撾萬榮一對情侶失蹤以政,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后盈蛮,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體技矮,經...
    沈念sama閱讀 46,699評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,770評論 3 343
  • 正文 我和宋清朗相戀三年袒炉,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片我磁。...
    茶點故事閱讀 40,918評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡驻襟,死狀恐怖,靈堂內的尸體忽然破棺而出沉衣,到底是詐尸還是另有隱情,我是刑警寧澤豌习,帶...
    沈念sama閱讀 36,573評論 5 351
  • 正文 年R本政府宣布拔疚,位于F島的核電站既荚,受9級特大地震影響,放射性物質發(fā)生泄漏固以。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,255評論 3 336
  • 文/蒙蒙 一诫钓、第九天 我趴在偏房一處隱蔽的房頂上張望篙螟。 院中可真熱鬧,春花似錦遍略、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,749評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽势似。三九已至,卻和暖如春履因,著一層夾襖步出監(jiān)牢的瞬間盹愚,已是汗流浹背栅迄。 一陣腳步聲響...
    開封第一講書人閱讀 33,862評論 1 274
  • 我被黑心中介騙來泰國打工皆怕, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人朗兵。 一個月前我還...
    沈念sama閱讀 49,364評論 3 379
  • 正文 我出身青樓顶滩,卻偏偏與公主長得像寸爆,于是被迫代替她去往敵國和親礁鲁。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,926評論 2 361

推薦閱讀更多精彩內容