Nginx反向代理

1.什么是代理

生活中代理的场景:
房屋中介
订票网站

2.正向代理与反向代理

image.png

以访问GOO为例,客户端连接到VPN相当于正向代理,VPN代理请求访问后端服务器并返回属于反向代理

3.Nginx支持的代理协议

image.png

Nginx反向代理模式

image.png

4.Nginx反向代理参数解释

# 将配置文件写入新文件,调用的时候include引用即可
[root@lb01 ~]# cat /etc/nginx/proxy_params
proxy_set_header Host $http_host;    # lb服务器将用户访问网站HOST信息传递给后端的WEB服务器
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  # 将用户的真实IP传递给后端WEB服务器(主要是日志文件显示)
proxy_connect_timeout 30;    #代理与后端服务器连接超时时间(代理连接超时)
proxy_send_timeout 60;    #后端服务器数据回传给nginx代理超时时间
proxy_read_timeout 60;    #代理等待后端服务器的响应时间
proxy_buffering on;    #把后端返回的内容先放到缓冲区当中,然后再返回给客户端,边收边传,不是全部接收完再传给客户端
proxy_buffer_size 32k;    #设置nginx代理保存用户头信息的缓冲区大小
proxy_buffers 4 128k;    #proxy_buffers缓冲区

5.Nginx简单反向代理实战

需求访问lb01的80端口代理到web01的8080端口

  • web01配置
cat > /etc/nginx/conf.d/web.conf<<EOF
server {
    listen 8080;
    server_name www.test.com;
    location / {
        root /code;
        index index.html;
    }
}
EOF
mkdir /code -p
echo "web01" >/code/index.html
nginx -t
systemctl restart nginx
  • lb01配置
cat > /etc/nginx/conf.d/proxy.conf<<EOF
server {
    listen 80;
    server_name www.test.com;      #和需要代理的WEB01服务器域名保持一致
    location / {
        proxy_pass http://172.16.1.51:8080;    #需要代理的web01服务器IP和端口
        include proxy_params;    #反向代理的配置文件引用
    }
}
EOF
nginx -t 
systemctl restart nginx
  • 访问测试修改windows的host文件或在lb服务器上host配置


    image.png
# ib 服务配置hosts解析,让域名解析lb自身IP
[root@lb01 /etc/nginx/conf.d]# echo '10.0.0.5 www.test.com' >> /etc/hosts
[root@lb01 /etc/nginx/conf.d]# curl www.test.com
web01

6.Nginx负载均衡

  • 为什么需要负载均衡

我们web服务器直接面向用户,往往要承载大量并发请求,单台服务器难以负荷
我们使用多台WEB服务器组成集群,前端使用Nginx负载均衡,将请求分散的打到我们的后端服务器中实现负载的分发
那么会大大提升系统的吞吐率、请求性能、高容灾

  • 负载均衡和反向代理的关系

因为反向代理多台机器,所有可以达到负载均衡的效果


image.png

负载均衡算法

image.png

负载均衡配置参数

image.png

负载均衡实战1

需求,访问www.test.com网站,平均的方式到达服务器web01和web02
服务器要求:一台lb服务器,2台nginx

  • lb服务器配置
# bbs_pools为地址池名称
upstream bbs_pools{
    server 172.16.1.51:8080;
    server 172.16.1.52:8080;

}

server {
    listen 80;
    server_name www.test.com;
    location / {
        proxy_pass http://bbs_pools;  访问域名解析后去bbs_pools地址池查找相应的服务器
        include proxy_params;
    }
}

nginx -t
systemctl restart nginx
  • web服务器配置(两台一样。代码目录下index.html文件为自身主机名)
[root@db01 /etc/nginx/conf.d]# cat web.conf 
server {
    listen 8080;
    server_name www.test.com;
    location / {
        root /code;
        index index.html;
    }
}

echo "$(hostname)" > /code/index.html   #两台WEB分别写入
systemctl restart nginx
  • 测试


    image.png

负载均衡实战2

访问bbs.test.com 跳转到172.16.1.52
访问www.test.com 跳转到172.16.1.51

  • LB配置文件
[root@lb01 /etc/nginx/conf.d]# cat /etc/nginx/conf.d/proxy.conf 
upstream www_pools{
    server 172.16.1.51:8080;

}

upstream bbs_pools{
    server 172.16.1.52:8080;

}

server {
    listen 80;
    server_name www.test.com;
    location / {
        proxy_pass http://www_pools;
        include proxy_params;
    }
}

server {
    listen 80;
    server_name bbs.test.com;
    location / {
        proxy_pass http://bbs_pools;
        include proxy_params;
    }
}

nginx -t 
systemctl restart nginx
  • web01服务配置
[root@db01 ~]# cat /etc/nginx/conf.d/web.conf 
server {
    listen 8080;
    server_name www.test.com;
    location / {
        root /code;
        index index.html;
    }
}

[root@db01 ~]# cat /code/index.html 
www

nginx -t 
systemctl restart nginx
  • web02服务配置
[root@web02 /etc/nginx/conf.d]# cat /etc/nginx/conf.d/bbs.conf 
server {
    listen 8080;
    server_name bbs.test.com;
    location / {
        root /code;
        index index.html;
    }
}

[root@web02 /etc/nginx/conf.d]# cat /code/index.html 
bbs

nginx -t 
systemctl restart nginx
  • 访问测试


    image.png

    image.png

负载均衡实战3

一台服务存在多个域名,访问不同的域名跳转到不同对应页面
此实验可以验证LB服务只负责转发,url请求能否拿到结果还是取决于后端的WEB集群配置

  • LB服务器配置
[root@lb01 /etc/nginx/conf.d]# cat proxy.conf 
upstream www_pools{
    server 172.16.1.51:8080;
    server 172.16.1.52:8080;
}


server {
    listen 80;
    server_name www.test.com bbs.test.com;
    location / {
        proxy_pass http://www_pools;
        include proxy_params;
    }
}

WEB服务器配置(两台必须保持配置一致)

[root@db01 /code]# cat /etc/nginx/conf.d/bbs.conf 
server {
    listen 8080;
    server_name bbs.test.com;
    location / {
        root /code;
        index bbs.html;
    }
}

[root@db01 /code]# cat /etc/nginx/conf.d/www.conf 
server {
    listen 8080;
    server_name www.test.com;
    location / {
        root /code;
        index www.html;
    }
}

#两台机都需执行测试命令
echo "$(hostname) bbs" > /code/bbs.html
echo "$(hostname) www" > /code/www.html
  • 访问测试
#LB服务配置hosts解析
[root@lb01 /etc/nginx/conf.d]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.5 www.test.com bbs.test.com


[root@lb01 /etc/nginx/conf.d]# curl www.test.com
web01 www
[root@lb01 /etc/nginx/conf.d]# curl www.test.com
web02 www
[root@lb01 /etc/nginx/conf.d]# curl bbs.test.com
web01 bbs
[root@lb01 /etc/nginx/conf.d]# curl bbs.test.com
web02 bbs

7.负载均衡调度算法实验

  • weight(加权)
[root@lb01 ~]# cat /etc/nginx/conf.d/proxy.conf 
upstream www_pools{
    server 172.16.1.51:8080 weight=1;    #这里1和2不是指访问的次数,而是指比例
    server 172.16.1.52:8080 weight=2;
}

server {
    listen 80;
    server_name www.test.com bbs.test.com;
    location / {
        proxy_pass http://www_pools;
        include proxy_params;
    }
}

[root@lb01 ~]# curl www.test.com
web02 www
[root@lb01 ~]# curl www.test.com
web02 www
[root@lb01 ~]# curl www.test.com
web01 www
  • ip_hash(通过访问者的IP进行算法分配,IP地址不变请求的服务器不变)
upstream www_pools {
   ip_hash;
   server 172.16.1.7 ;
   server 172.16.1.8 ;
}
  • url_hash(和ip_hash差不多,不过是请求内容来进行计算,内容不变,请求服务器不变,可能造成负载不均衡的现象)
upstream www_pools {
   hash   $request_uri;  
   server 172.16.1.7 ;
   server 172.16.1.8 ;
}
  • 用于测试访问命令
for i in {1..100};do curl -s -H "host:www.mysun.com" 127.0.0.1;done |grep web02|wc -l
  • down参数
[root@lb01 ~]# cat /etc/nginx/conf.d/proxy.conf 
upstream www_pools{
    server 172.16.1.51:8080 down;    #down表示可以在不关闭后端WEB服务器服务去情况下,在负载停止填写了down服务器的转发。
    server 172.16.1.52:8080;
}


server {
    listen 80;
    server_name www.test.com bbs.test.com;
    location / {
        proxy_pass http://www_pools;
        include proxy_params;
    }
}
  • backup参数
[root@lb01 ~]# cat /etc/nginx/conf.d/proxy.conf 
upstream www_pools{
    server 172.16.1.51:8080 backup;    #backup至此台IP服务器是备选的,当地址池中172.16.1.52Nginx服务挂了,才启动备选服务器。当172.16.1.52服务恢复51又将成为备选服务器
    server 172.16.1.52:8080;
}


server {
    listen 80;
    server_name www.test.com bbs.test.com;
    location / {
        proxy_pass http://www_pools;
        include proxy_params;
    }
}

8.根据条件转发实战

根据客户端类型转发
需求:
如果用户是iphone就跳转到iphone页面
如果用户是安卓就跳转到安卓页面
如果用户是pc就跳转到pc页面
如果用户是IE就返回403

  • web服务器nginx配置
[root@web02 /etc/nginx/conf.d]# cat test.conf 
server {
    listen 8080;
    server_name www.sj.com;
    location / {
        root /code/android;
        index index.html;
    }
}
server {
    listen 8081;
    server_name www.sj.com;
    location / {
        root /code/iphone;
        index index.html;
    }
}
server {
    listen 8082;
    server_name www.sj.com;
    location / {
        root /code/pc;
        index index.html;
    }
}
  • 生成测试页面(两台机都执行)
mkdir -p /code/{android,iphone,pc}
echo "$(hostname) PC" > /code/pc/index.html
echo "$(hostname) Iphone" > /code/iphone/index.html
echo "$(hostname) Android" > /code/android/index.html
nginx -t
systemctl restart nginx
  • lb 服务器配置
[root@lb01 /etc/nginx/conf.d]# cat js.conf 
upstream android {
    server 172.16.1.51:8080;
}
upstream iphone {
    server 172.16.1.52:8081;
}
upstream pc {
    server 172.16.1.51:8082;
    server 172.16.1.52:8082;
}

server {
    listen 80;
    server_name www.js.com;
    location / {
        #默认跳转至 pc 站点
        proxy_pass http://pc;
        include proxy_params;

        #如果客户端是 Iphone 则跳转到 iphone 的资源池,~*不区分大小写的正则匹配
        if ($http_user_agent ~* "Iphone") {
            proxy_pass http://iphone;
        } 

        #如果客户端是 Android 则跳转到 android 的资源池
        if ($http_user_agent ~* "Android"){
            proxy_pass http://android;
        } 

        #如果客户端是 IE 浏览器,则返回 403 错误。
        if ($http_user_agent ~* "msie"){
            return 403;
        }
    }
}

nginx -t
systemctl restart nginx
  • 访问测试(curl命令会显示使用的浏览器,也可以模拟自定义)
echo "10.0.0.5 www.js.com" >> /etc/hosts
curl -A "iphone" www.js.com
curl -A "android" www.js.com
curl -A "msie" www.js.com

实战2

需求:
访问图片格式就跳转到web01
访问其他地址就跳转到web02

  • web服务器配置(两太一样)
[root@web01 /code]# cat /etc/nginx/conf.d/tp.conf 
server {
    listen 80;
    server_name tp.test.com;
    location / {
        root /code;
        index index.html;
    }
}

nginx -t 
systemctl restart nginx
  • 生成测试页面(图片只需web01下载即可)
echo "$(hostname) www" > /code/index.html
cd /code/ && wget -O sun.jpg http://pic.51yuansu.com/pic3/cover/02/27/64/59c008e1c7954_610.jpg 

-lb服务器配置

[root@lb01 /etc/nginx/conf.d]# cat jpg.conf 
upstream static {
    server 172.16.1.51;    #有图片的服务器
}
upstream default {
    server 172.16.1.52;
}

server {
    listen 80;
    server_name tp.test.com;    #一个server可以包含多个location
    location / {
            proxy_pass http://default;
        include proxy_params;
    }
    location ~ .*.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {    #匹配包含这些字符的调整到static地址池
            proxy_pass http://static;
            include proxy_params;
    }
}

nginx -t
systemctl restart nginx
  • 访问测试


    image.png

    image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
禁止转载,如需转载请通过简信或评论联系作者。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,367评论 6 512
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,959评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,750评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,226评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,252评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,975评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,592评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,497评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,027评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,147评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,274评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,953评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,623评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,143评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,260评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,607评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,271评论 2 358

推荐阅读更多精彩内容