《云计算》-Nginx高级-Nginx反向代理-Nginx的TCP/UDP调度器、Nginx常见的调优及优化部署

案例1:Nginx反向代理
案例2:Nginx的TCP/UDP调度器
案例3:Nginx常见的调优及优化部署

    
    
  • 1
  • 2
  • 3

1 案例1:Nginx反向代理
1.1 问题

使用Nginx实现Web反向代理功能,实现如下功能:

后端Web服务器两台,可以使用httpd实现
Nginx采用轮询的方式调用后端Web服务器
两台Web服务器的权重要求设置为不同的值
最大失败次数为1,失败超时时间为30秒

    
    
  • 1
  • 2
  • 3
  • 4

1.2 方案

使用4台RHEL7虚拟机,其中一台作为Nginx代理服务器,该服务器需要配置两块网卡,IP地址分别为192.168.4.5和192.168.2.5,两台Web服务器IP地址分别为192.168.2.100和192.168.2.200。客户端测试主机IP地址为192.168.4.100。如图-1所示。
在这里插入图片描述
图-1
1.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:部署实施后端Web服务器

1)部署后端Web1服务器

后端Web服务器可以简单使用yum方式安装httpd实现Web服务,为了可以看出后端服务器的不同,可以将两台后端服务器的首页文档内容设置为不同的内容。

[root@web1 ~]# yum  -y  install  httpd
[root@web1 ~]# echo "192.168.2.100" > /var/www/html/index.html
[root@web1 ~]# systemctl restart httpd
[root@web1 ~]# firewall-cmd --set-default-zone=trusted

    
    
  • 1
  • 2
  • 3
  • 4

2)部署后端Web2服务器

[root@web2 ~]# yum  -y  install  httpd
[root@web2 ~]# echo "192.168.2.200" > /var/www/html/index.html
[root@web2 ~]# systemctl restart httpd
[root@web2 ~]# firewall-cmd --set-default-zone=trusted

    
    
  • 1
  • 2
  • 3
  • 4

步骤二:配置Nginx服务器,添加服务器池,实现反向代理功能

1)修改/usr/local/nginx/conf/nginx.conf配置文件

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
http {
.. ..
upstream webserver {
                server 192.168.2.100:80;
                server 192.168.2.200:80;
        }
.. ..
server {
        listen        80;
        server_name  localhost;
            location / {
            proxy_pass http://webserver;
        }
}

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

2)重启nginx服务

[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

    
    
  • 1

3)客户端使用浏览器访问代理服务器测试轮询效果

[root@client ~]# curl http://192.168.4.5            //使用该命令多次访问查看效果

    
    
  • 1

步骤二:配置upstream服务器集群池属性

1)设置失败次数,超时时间,权重

weight可以设置后台服务器的权重,max_fails可以设置后台服务器的失败次数,fail_timeout可以设置后台服务器的失败超时时间。

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
http {
.. ..
upstream webserver {
                server 192.168.2.100 weight=1 max_fails=1 fail_timeout=10;
                server 192.168.2.200 weight=2 max_fails=2 fail_timeout=10;
        }
//weight设置服务器权重值
//max_fails设置最大失败次数
//fail_timeout设置失败超时时间,单位为秒
.. ..
server {
        listen        80;
        server_name  localhost;
            location / {
            proxy_pass http://webserver;
        }
}

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

2)重启nginx服务

[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

    
    
  • 1

3)关闭一台后端服务器(web1)

[root@web1 ~]# systemctl stop httpd

    
    
  • 1

4)客户端使用浏览器访问代理服务器测试轮询效果

[root@client ~]# curl http://192.168.4.5            //使用该命令多次访问查看效果

    
    
  • 1

5)再次启动后端服务器的httpd(web1)

[root@web1 ~]# systemctl start httpd

    
    
  • 1

6)客户端再次使用浏览器访问代理服务器测试轮询效果

[root@client ~]# curl http://192.168.4.5            //使用该命令多次访问查看效果

    
    
  • 1

步骤三:配置upstream服务器集群的调度算法

1)设置相同客户端访问相同Web服务器

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
http {
.. ..
upstream webserver {
                 ip_hash;
                server 192.168.2.100 weight=1 max_fails=2 fail_timeout=10;
                server 192.168.2.200 weight=2 max_fails=2 fail_timeout=10;
        }
.. ..
server {
        listen        80;
        server_name  www.tarena.com;
            location / {
            proxy_pass http://webserver;
        }
}

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

2)重启nginx服务

[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

    
    
  • 1

3)客户端使用浏览器访问代理服务器测试轮询效果

[root@client ~]# curl http://192.168.4.5            //使用该命令多次访问查看效果

    
    
  • 1

2 案例2:Nginx的TCP/UDP调度器
2.1 问题

使用Nginx实现TCP/UDP调度器功能,实现如下功能:

后端SSH服务器两台
Nginx编译安装时需要使用--with-stream,开启ngx_stream_core_module模块
Nginx采用轮询的方式调用后端SSH服务器

    
    
  • 1
  • 2
  • 3

2.2 方案

使用4台RHEL7虚拟机,其中一台作为Nginx代理服务器,该服务器需要配置两块网卡,IP地址分别为192.168.4.5和192.168.2.5,两台SSH服务器IP地址分别为192.168.2.100和192.168.2.200。客户端测试主机IP地址为192.168.4.100。如图-2所示。
在这里插入图片描述
图-2
2.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:部署支持4层TCP/UDP代理的Nginx服务器

1)部署nginx服务器

编译安装必须要使用–with-stream参数开启4层代理模块。

[root@proxy ~]# yum –y install gcc pcre-devel openssl-devel        //安装依赖包
[root@proxy ~]# tar  -xf   nginx-1.12.2.tar.gz
[root@proxy ~]# cd  nginx-1.12.2
[root@proxy nginx-1.12.2]# ./configure   \
> --with-http_ssl_module                                //开启SSL加密功能
> --with-stream                                       //开启4层反向代理功能
[root@proxy nginx-1.12.2]# make && make install           //编译并安装

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

步骤二:配置Nginx服务器,添加服务器池,实现TCP/UDP反向代理功能

1)修改/usr/local/nginx/conf/nginx.conf配置文件

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
stream {
            upstream backend {
               server 192.168.2.100:22;            //后端SSH服务器的IP和端口
               server 192.168.2.200:22;
}
            server {
                listen 12345;                    //Nginx监听的端口
                proxy_connect_timeout 1s;
                proxy_timeout 3s;
                 proxy_pass backend;
             }
}
http {
.. ..
}

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

2)重启nginx服务

[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

    
    
  • 1

3)客户端使用访问代理服务器测试轮询效果

[root@client ~]# ssh 192.168.4.5 -p 12345            //使用该命令多次访问查看效果

    
    
  • 1

3 案例3:Nginx常见的调优及优化部署
3.1 问题

本案例要求对Nginx服务器进行适当优化,解决如下问题,以提升服务器的处理性能:

如果客户端访问服务器提示“Too many open files”如何解决
如何解决客户端访问头部信息过长的问题
如何让客户端浏览器缓存数据
如何自定义返回给客户端的404错误页面
如何查看服务器状态信息
开启gzip压缩功能,提高数据传输效率

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

然后客户机访问此Web服务器验证效果:

使用ab压力测试软件测试并发量
编写测试脚本生成长头部信息的访问请求
客户端访问不存在的页面,测试404错误页面是否重定向

    
    
  • 1
  • 2
  • 3

3.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:构建Nginx服务器

1)源码安装Nginx软件

[root@proxy ~]# yum -y install gcc pcre-devel openssl-devel        //安装常见依赖包
[root@proxy ~]# tar  -xf   nginx-1.12.2.tar.gz
[root@proxy ~]# cd  nginx-1.12.2
[root@proxy nginx-1.12.2]# ./configure   \
> --with-http_ssl_module                                 //开启SSL加密功能
> --with-stream                                        //开启TCP/UDP代理模块
[root@proxy nginx-1.12.2]# make && make install         //编译并安装

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2)启用Nginx服务并查看监听端口状态

[root@proxy ~]# /usr/local/nginx/sbin/nginx
[root@proxy ~]# netstat  -anptu  |  grep nginx
tcp        0        0 0.0.0.0:80        0.0.0.0:*        LISTEN        10441/nginx

    
    
  • 1
  • 2
  • 3

步骤二:优化Nginx并发量

1)优化前使用ab高并发测试

[root@proxy ~]# ab -n 2000 -c 2000 http://192.168.4.5/
Benchmarking 192.168.4.5 (be patient)
socket: Too many open files (24)                //提示打开文件数量过多

    
    
  • 1
  • 2
  • 3

2)修改Nginx配置文件,增加并发量

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
worker_processes  2;                    //与CPU核心数量一致
events {
worker_connections 65535;        //每个worker最大并发连接数
use epoll;
}
.. ..
[root@proxy ~]# nginx -s reload

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

3)优化Linux内核参数(最大文件数量)

[root@proxy ~]# ulimit -a                        //查看所有属性值
[root@proxy ~]# ulimit -Hn 100000                //设置硬限制(临时规则)
[root@proxy ~]# ulimit -Sn 100000                //设置软限制(临时规则)
[root@proxy ~]# vim /etc/security/limits.conf
    .. ..
*               soft    nofile            100000
*               hard    nofile            100000

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

4)优化后测试服务器并发量

[root@proxy ~]# ab -n 2000 -c 2000 http://192.168.4.5/

    
    
  • 1

步骤三:优化Nginx数据包头缓存

1)优化前,使用脚本测试长头部请求是否能获得响应

[root@proxy ~]# cat lnmp_soft/buffer.sh 
#!/bin/bash
URL=http://192.168.4.5/index.html?
for i in {1..5000}
do
    URL=${URL}v$i=$i
done
curl $URL                                //经过5000次循环后,生成一个长的URL地址栏
[root@proxy ~]# ./buffer.sh
.. ..
<center><h1>414 Request-URI Too Large</h1></center>        //提示头部信息过大

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

2)修改Nginx配置文件,增加数据包头部缓存大小

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
http {
client_header_buffer_size    1k;        //默认请求包头信息的缓存    
large_client_header_buffers  4 4k;        //大请求包头部信息的缓存个数与容量
.. ..
}
[root@proxy ~]# nginx -s reload

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

3)优化后,使用脚本测试长头部请求是否能获得响应

[root@proxy ~]#cat cat buffer.sh 
#!/bin/bash
URL=http://192.168.4.5/index.html?
for i in {1..5000}
do
    URL=${URL}v$i=$i
done
curl $URL
[root@proxy ~]# ./buffer.sh

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

步骤四:浏览器本地缓存静态数据

1)使用Firefox浏览器查看缓存

以Firefox浏览器为例,在Firefox地址栏内输入about:cache将显示Firefox浏览器的缓存信息,如图-3所示,点击List Cache Entries可以查看详细信息。
在这里插入图片描述
图-3

2)清空firefox本地缓存数据,如图-4所示。
在这里插入图片描述
图-4

3)修改Nginx配置文件,定义对静态页面的缓存时间

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
expires        30d;            //定义客户端缓存时间为30天
}
}
[root@proxy ~]# cp /usr/share/backgrounds/day.jpg /usr/local/nginx/html
[root@proxy ~]# nginx -s reload

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

4)优化后,使用Firefox浏览器访问图片,再次查看缓存信息

[root@client ~]# firefox http://192.168.4.5/day.jpg

    
    
  • 1

在Firefox地址栏内输入about:cache,查看本地缓存数据,查看是否有图片以及过期时间是否正确。

步骤五:自定义报错页面

1)优化前,客户端使用浏览器访问不存在的页面,会提示404文件未找到

[root@client ~]# firefox http://192.168.4.5/xxxxx        //访问一个不存在的页面

    
    
  • 1

2)修改Nginx配置文件,自定义报错页面

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
error_page   404  /40x.html;    //自定义错误页面
.. ..
[root@proxy ~]# vim /usr/local/nginx/html/40x.html        //生成错误页面
Oops,No NO no page …
[root@proxy ~]# nginx -s reload

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3)优化后,客户端使用浏览器访问不存在的页面,会提示自己定义的40x.html页面

[root@client ~]# firefox http://192.168.4.5/xxxxx        //访问一个不存在的页面

    
    
  • 1

4)常见http状态码

常见http状态码可用参考表-1所示。
在这里插入图片描述
表-1 主机列表

步骤六:如何查看服务器状态信息

1)编译安装时使用–with-http_stub_status_module开启状态页面模块

[root@proxy ~]# yum -y install gcc pcre-devel openssl-devel        //安装常见依赖包
[root@proxy ~]# tar  -zxvf   nginx-1.12.2.tar.gz
[root@proxy ~]# cd  nginx-1.12.2
[root@proxy nginx-1.12.2]# ./configure   \
> --with-http_ssl_module                        //开启SSL加密功能
> --with-stream                                //开启TCP/UDP代理模块
> --with-http_stub_status_module                //开启status状态页面
[root@proxy nginx-1.12.2]# make && make install    //编译并安装

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2)修改Nginx配置文件,定义状态页面

[root@proxy ~]# cat /usr/local/nginx/conf/nginx.conf
… …
location /status {
                stub_status on;
        }
… …
[root@proxy ~]# nginx

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2)优化后,查看状态页面信息

[root@proxy ~]# curl  http://192.168.4.5/status
Active connections: 1 
server accepts handled requests
 10 10 3 
Reading: 0 Writing: 1 Waiting: 0

    
    
  • 1
  • 2
  • 3
  • 4
  • 5

Active connections:当前活动的连接数量。

Accepts:已经接受客户端的连接总数量。

Handled:已经处理客户端的连接总数量(一般与accepts一致,除非服务器限制了连接数量)。

Requests:客户端发送的请求数量。

Reading:当前服务器正在读取客户端请求头的数量。

Writing:当前服务器正在写响应信息的数量。

Waiting:当前多少客户端在等待服务器的响应。

步骤七:对页面进行压缩处理

1)修改Nginx配置文件

[root@proxy ~]# cat /usr/local/nginx/conf/nginx.conf
http {
.. ..
gzip on;                            //开启压缩
gzip_min_length 1000;                //小文件不压缩
gzip_comp_level 4;                //压缩比率
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
                                    //对特定文件压缩,类型参考mime.types
.. ..
}

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
                                            <div class="more-toolbox">
            <div class="left-toolbox">
                <ul class="toolbox-list">
                    
                    <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#csdnc-thumbsup"></use>
                    </svg><span class="name">点赞</span>
                    <span class="count"></span>
                    </a></li>
                    <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-Collection-G"></use>
                    </svg><span class="name">收藏</span></a></li>
                    <li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;1582594662_002&quot;}"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-fenxiang"></use>
                    </svg>分享</a></li>
                    <!--打赏开始-->
                                            <!--打赏结束-->
                                            <li class="tool-item tool-more">
                        <a>
                        <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                        </a>
                        <ul class="more-box">
                            <li class="item"><a class="article-report">文章举报</a></li>
                        </ul>
                    </li>
                                        </ul>
            </div>
                        </div>
        <div class="person-messagebox">
            <div class="left-message"><a href="https://blog.csdn.net/xie_qi_chao" target="_blank">
                <img src="https://profile.csdnimg.cn/B/F/6/3_xie_qi_chao" class="avatar_pic" username="xie_qi_chao">
                                        <img src="https://g.csdnimg.cn/static/user-reg-year/1x/2.png" class="user-years">
                                </a></div>
            <div class="middle-message">
                                    <div class="title"><span class="tit"><a href="https://blog.csdn.net/xie_qi_chao" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">解启超</a></span>
                                        </div>
                <div class="text"><span>发布了317 篇原创文章</span> · <span>获赞 48</span> · <span>访问量 3万+</span></div>
            </div>
                            <div class="right-message">
                                        <a href="https://im.csdn.net/im/main.html?userName=xie_qi_chao" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                    </a>
                                                        <a class="btn btn-sm attented bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">已关注</a>
                                </div>
                        </div>
                </div>
</article>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尹汇川

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值