總覽:
1、七層負載均衡
2、在使用proxy_pass反向代理時,最後結尾添加或者不添加/有什麼區別?
3、根據設備調度不同的集羣(瀏覽器)(手機)
4、四層負載均衡
5、nginx四層配置+七層負載+web集羣
6、配置阿里雲四層負載均衡 實現端口轉發
7、架構圖
七層負載均衡:
根據url 調度不同的集羣 url.cheng.com
10.0.0.5
10.0.0.7 /pass
10.0.0.8 /user
1.web01和web02配置 (只不過代碼不一樣)
[root@web01 conf.d]# cat url.cheng.com.conf
server {
listen 80;
server_name url.cheng.com;
root /code;
location / {
index index.html;
}
}
[root@web01 code]# cat index.html
Hello PC.....
[root@web02 code]# cat index.html
Hello phone....
2.lb【10.0.0.5】配置
[root@lb01 conf.d]# cat proxy_url.cheng.com.conf
upstream user {
server 172.16.1.8;
}
upstream pass {
server 172.16.1.7;
}
server {
listen 80;
server_name url.cheng.com;
location /user {
proxy_pass http://user/;
include proxy_params;
}
location /pass {
proxy_pass http://pass/;
include proxy_params;
}
}
3.檢測語法並重啓nginx服務
[root@lb01 ~]# nginx -t
[root@lb01 conf.d]# systemctl restart nginx
2、在使用proxy_pass反向代理時,最後結尾添加或者不添加/有什麼區別?
在nginx中配置proxy_pass時,當在後面的URL上加上了/,相當於是絕對路徑,則nginx不會把localtion中匹配的路徑部分代理走;如果沒有/,則會把匹配的路徑也給代理走。
下面四種情況分別http://192.168.1.4/proxy/test.html
進行訪問。
第一種
location /proxy/ {
proxy_pass http://127.0.0.1:81/;
}
會被代理到 http://127.0.0.1:81/test.html 這個url
第二種(相對於第一種方式,最後少一個/)(proxy_pass參數中如果不包含URL路徑,則會將localtion的pattern識別的路徑作爲絕對路徑)
location /proxy/ {
proxy_pass http://127.0.0.1:81;
}
會被代理到 http://127.0.0.1:81/proxy/test.html 這個url
第三種:
location /proxy/ {
proxy_pass http://127.0.0.1:81/ftlynx/;
}
會被代理到 http://127.0.0.1:81/ftlynx/test.html 這個url。
第四種情況(相對於第三種,最後少一個 / ):
location /proxy/ {
proxy_pass http://127.0.0.1:81/ftlynx;
}
會被代理到 http://127.0.0.1:81/ftlynxtest.html 這個url
3、根據不同設備調度不同的集羣(瀏覽器端)(手機端)
10.0.0.5--------》lb【負載均衡】
10.0.0.7 pc
10.0.0.8 phone
1.所有的web【01-02】都需要配置 ( 代碼不一樣)
[root@web01 conf.d]# cat agent.cheng.com.conf
server {
listen 80;
server_name agent.cheng.com;
root /code;
location / {
index index.html;
}
}
2.代理配置【10.0.0.5】
[root@lb01 conf.d]# cat proxy_agent.cheng.com.conf
upstream pc {
server 172.16.1.7:80;
}
upstream phone {
server 172.16.1.8:80;
}
server {
listen 80;
server_name agent.cheng.com;
location / {
#默認都走PC
proxy_pass http://pc;
include proxy_params;
default_type text/html;
charset utf-8;
#如果是Android或iphone,則走phone
if ( $http_user_agent ~* "android|iphone|iPad") {
proxy_pass http://phone;
}
#如果是IE瀏覽器,要麼拒絕訪問,要麼返回一個正版的瀏覽器下載界面
if ( $http_user_agent ~* "Trident"){
return 200 '<a href="https://www.cnblogs.com/yinwu/p/11569452.html" target="_blank">點擊訪問正版</a>';
}
}
}
4、四層負載均衡
1、什麼是四層OSI
傳輸層 TCP/IP UDP/TCP
四層是基於轉發的方式
2、四層負載均衡使用場景
1、四層負載均衡+七層負載均衡
2、dns+多機房+四層負載均衡+七層負載均衡
3、SOA鬆耦合架構
4、基於端口的轉發
5、nginx四層配置+nginx七層負載+web集羣
操作部分:
1.服務器10.0.0.6安裝nginx
[root@lb02 ~]# yum install nginx
2.將10.0.0.5(七層負載)這臺機器nginx下的所有文件分別推送至10.0.0.6和10.0.0.4
[root@lb01 ~]# scp -rp /etc/nginx [email protected]:/etc/
[root@lb01 ~]# scp -rp /etc/nginx [email protected]:/etc/
3.檢測並重啓nginx服務
[root@lb02 ~]# nginx -t
[root@lb02 ~]# systemctl restart nginx
4.將zh.cheng.com域名作解析,測試10.0.0.6七層負載是否配置?
5.將所有域名做解析至10.0.0.4
6.定義四層配置文件路徑:
[root@lb03 ~]# vim /etc/nginx/nginx.conf
include /etc/nginx/conf.c/*.conf;
7.進行初始化操作 【注意:若不刪除七層負載的配置,無法正常使用四層,因爲都是佔用80端口導致nginx啓動失敗】
[root@lb03 ~]# rm -f /etc/nginx/conf.d/default.conf
[root@lb03 ~]# mkdir /etc/nginx/conf.c
8.配置四層負載均衡
[root@lb03 conf.c]# cat all.conf
stream {
upstream zh {
server 172.16.1.5:80;
server 172.16.1.6:80;
}
server {
listen 80;
proxy_pass zh;
proxy_timeout 3s; #超時時間
proxy_connect_timeout 3s; #連接時間
}
}
[root@lb03 conf.c]# nginx -t
[root@lb03 conf.c]# systemctl restart nginx
9.通過瀏覽器訪問以及查看5和6的日誌進行驗證
通過以下日誌我們可以看出輪詢的效果!!!
nginx是1.9版本以後才引入的四層負載均衡 stream模塊實現,但stream不能出現在http層
–with-stream
-with-stream_ssl_module
-with-stream_realip_module
1.stream模塊介紹:
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;
}
}
2.基於端口的轉發:
需求: 用戶連接10.0.0.4的6666端口,其實連接的是172.16.1.7的22/TCP端口
需求: 用戶連接10.0.0.4的5555端口,其實連接的是172.16.1.51的3306/TCP端口
1.將7和51這臺機器的WAN口斷掉
2.配置四層負載
[root@lb03 conf.c]# cat all.conf
stream {
upstream zh {
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 zh;
proxy_timeout 3s; #超時時間
proxy_connect_timeout 3s; #連接時間
}
}
3.測試轉發後的端口是否能正常登陸
[root@lb03 conf.c]# ssh [email protected] 6666 ------>>>可正常登陸
4.四層負載均衡怎麼記錄日誌 必須在stream層,不能出現在http層?
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;
6.配置阿里雲四層負載均衡 實現端口轉發
公網666轉到內網的22
公網80 轉到內網的多臺7層負載均衡的80
1、根據url調度
2、根據設備調度
3、四層負載均衡
4、四層負載均衡使用場景
4+7
dns+4+7
SOA
5、四層+七層負載的配置
6、四層轉發的配置
7、四層日誌記錄
8、阿里雲四層負載
7.架構圖
轉載自程寅武
https://www.cnblogs.com/yinwu/p/11575421.html