1.七層根據(jù)url不同調(diào)度
根據(jù)url 調(diào)度不同的集群 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配置
[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/;
include proxy_params;
}
location /pass {
proxy_pass http://pass;
include proxy_params;
}
}
#根據(jù)配置初始化
[root@lb01 conf.d]# systemctl restart nginx
2.反向代理時加/與不加/的區(qū)別
PS: 在使用proxy_pass反向代理時,最后結(jié)尾添加/和不添加/有什么區(qū)別?
在nginx中配置proxy_pass時戳晌,當(dāng)在后面的url加上了/,相當(dāng)于是絕對根路徑痴柔,
則nginx不會把location中匹配的路徑部分代理走;
如果沒有/躬厌,則會把匹配的路徑部分也給代理走。proxy_pass反向代理時,最后結(jié)尾添加/和不添加/有什么區(qū)別?
1.不添加 /
用戶如果請求: http://url.oldxu.com/user
會被代理至后端: http://url.oldxu.com/user
1.添加 /
用戶如果請求: http://url.oldxu.com/user
會被代理至后端: http://url.oldxu.com/
3.七層根據(jù)不同的設(shè)備匹配調(diào)度
根據(jù)設(shè)備調(diào)度不同的集群 ( 瀏覽器 ) ( 手機 )
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 / {
#默認(rèn)都走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 ~* "MSIE" ) {
return 200 '<a target="_blank">點擊下載正版瀏覽器google.exe</a>';
}
}
}
多級負(fù)載下如何透傳真實客戶端IP竞帽?
x-forwar
realip (知道經(jīng)過了那些代理 代理的IP又是多少)
4.四層負(fù)載均衡
4.1什么是四層負(fù)載均衡
四層負(fù)載均衡是基于轉(zhuǎn)發(fā)方式扛施,在傳輸層工作。
4.2四層負(fù)載均衡作用
四層負(fù)責(zé)轉(zhuǎn)發(fā)屹篓,所以它沒有端口限制疙渣。因為一個七層負(fù)載均衡端口限制65535,所以我們一般會在七層負(fù)載均衡集群前面加上一個四層負(fù)載均衡作為轉(zhuǎn)發(fā)堆巧,七層以輪詢的方式作為調(diào)度下發(fā)任務(wù)妄荔。
4.3四層負(fù)載均衡適用的場景
1.四層負(fù)載均衡 + 七層負(fù)載均衡
2.dns + 多機房 + 四層負(fù)載均衡+七層負(fù)載均衡
3.SOA 松耦合架構(gòu):就是將一個網(wǎng)站的每個功能都拆開來,組成一個集群谍肤。比如:京東網(wǎng)站某一天的登錄界面壞了啦租,但注冊界面還是可以注冊的。? 登錄 passport.jd.com
? 注冊 reg.jd.com
? 商品詳情 pro.jd.com
4.4基于端口的轉(zhuǎn)發(fā)
nginx 7層 web01 MySQL
nginx 4層 + web02 NFS
nginx 7層 web03 Redis
10.0.0.6
nginx是1.9版本以后才引入的四層負(fù)載均衡
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;
}
}
5.nginx四層負(fù)載均衡配置
5.1定義四層配置路徑
[root@lb-4 nginx]# vim /etc/nginx/nginx.conf
.......
events {
worker_connections 1024;
}
index /etc/nginx/conf.c/*.conf; #不能寫在http層
http {
.......
5.2初始化環(huán)境
[root@lb-4 ~]# rm -f /etc/nginx/conf.d/default.conf
[root@lb-4 nginx]# mkdir /etc/nginx/conf.c
5.3配置四層負(fù)載均衡
[root@lb-4 ~]# cat /etc/nginx/conf.c/all.conf
stream { #四層的配置模塊荒揣,里面包含著七層(http層)和server層
upstream all {
server 172.16.1.5:80; #寫的就是七層負(fù)載均衡的IP地址
server 172.16.1.6:80;
}
server {
listen 80;
proxy_pass all;
proxy_timeout 3s;
proxy_connect_timeout 3s; #連接的超時時間
}
}
6.基于端口轉(zhuǎn)發(fā)
需求: 用戶連接10.0.0.4的6666端口,其實連接的是172.16.1.7的22/TCP端口
需求: 用戶連接10.0.0.4的5555端口,其實連接的是172.16.1.51的3306/TCP端口
[root@lb-4 conf.c]# cat blog.oldxu.com.conf
stream{
upstream ssh{
172.16.1.7:22;
}
upstream mysql{
172.16.1.51:3306;
}
server{
listen 6666;
proxy_pass ssh;
}
server{
listen 5555;
proxy_pass mysql;
}
}
7.四層負(fù)載均衡的日志
必須在stream層(四層的配置文件中),不能出現(xiàn)在http層
[root@lb-4 ~]# vim /etc/nginx/conf.c/all.conf
stream{
#定義日志名字和內(nèi)容
log_format proxy '$remote_addr - [$time_local] $status $protocol'
' "$upstream_addr" "$upstream_bytes_sent" "$upstream_connect_time"' ;
access_log /var/log/nginx/all.log proxy; #定義日志位置篷角,引用日志模塊名字
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;
}
}
8.配置阿里云四層負(fù)載均衡 實現(xiàn)端口轉(zhuǎn)發(fā)
? 公網(wǎng)666轉(zhuǎn)到內(nèi)網(wǎng)的22
? 公網(wǎng)80 轉(zhuǎn)到內(nèi)網(wǎng)的多臺7層負(fù)載均衡的80 ?