• Nginx系列之1:限流


    安装准备

    cd /usr/local/src/
    wget http://nginx.org/download/nginx-1.18.0.tar.gz 
    # 解压
    tar -zxvf /usr/local/src/nginx-1.18.0.tar.gz 
    # 进入到 nginx-1.18.0 目录下
    cd nginx-1.18.0/
    # 生成 Makefile
    ./configure
    # 编译并且安装(默认输出到/usr/local/nginx,可在configure中看到)
    make && make install
    
    如果中间安装失败,大概率是工具缺失,安装
    yum -y install make pcre pcre-devel zlib zlib-devel gcc-c++ libtool openssl openssl-devel
    如果中间遇到pcre-devel-8.32-17.el7.x86_64: [Errno 256] No more mirrors to try. 之类的错误
    解决方式:
    
    1、yum clean all
    2、yum makecache
    
    防火墙相关
    练习期间,推荐将防火墙关闭,避免因其导致无法访问而卡住:
    systemctl stop firewalld.service 
    或
    systemctl stop firewalld
    # 开启 80
    firewall-cmd --zone=public --add-port=80/tcp --permanent
    # 重启防火墙
    firewall-cmd --reload
    # 查询端口号80是否开启
    firewall-cmd --query-port=80/tcp
    # 查询哪些端口已开启
    firewall-cmd --list-port
    
    
    一、启动  
    cd /usr/local/nginx/sbin
    ./nginx
    
    或直接
    /usr/local/nginx/sbin/nginx 
    
    建立链接
    ln -s /usr/local/nginx/nginx /usr/local/bin
    然后可以直接启动
    nginx
    
    二、重启
    
    更改配置重启nginx  
    
    kill -HUP 主进程号或进程号文件路径
    或者使用
    cd /usr/local/nginx/sbin
    ./nginx -s reload
    
    判断配置文件是否正确 
    
    nginx -t -c /usr/local/nginx/conf/nginx.conf
    或者
    cd  /usr/local/nginx/sbin
    ./nginx -t
    
    三、停止
    
    nginx -s quit
    暴力停止
    nginx -s stop
    或
    停止操作是通过向nginx进程发送信号(什么是信号请参阅linux文 章)来进行的
    步骤1:查询nginx主进程号
    ps -ef | grep nginx
    在进程列表里 面找master进程,它的编号就是主进程号了。
    步骤2:发送信号
    从容停止Nginx:
    kill -QUIT 主进程号
    快速停止Nginx:
    kill -TERM 主进程号
    
    四、查看模块
    看一下 Nginx 下安装成功的版本及模块,命令:
    /usr/local/nginx/nginx -V

    对客户端限流

    limit_req_zone

    http {
        # 将请求客户端的IP存放到perip区域,区域大小为10M,并限制同一IP地址的请求每秒钟只处理10次
        limit_req_zone $binary_remote_addr zone=perip:10m rate=10r/s;
    
        server {
            listen       8080;
            server_name  localhost;
    
        # 每个IP最大并发1条连接
            # 该语句还可直接放置到http模块下,这样下属的server都应用该配置
            # 该语句还可放置到server中的location模块中,这样仅指定的location应用该配置
            #limit_conn perip 1;
            # 每个连接限速300 k/s
            #limit_rate 300k; 
        # 当有大量请求爆发时,可以缓存20条请求
            # 设置了nodelay,缓存队列的请求会立即处理,若请求数 > rate+burst 时,立即返回503;如果没设置,则会按照rate排队等待处理
            # 该语句还可直接放置到http模块下,这样下属的server都应用该配置
            # 该语句还可放置到server中的location模块中,这样仅指定的location应用该配置
            limit_req zone=perip burst=20 nodelay;
    
    
    测试
    ab -c 10 -n 1000 http://192.168.10.11:8080/
    
    结果:
    Concurrency Level:      10
    Time taken for tests:   0.093 seconds
    Complete requests:      1000
    Failed requests:        979
       (Connect: 0, Receive: 0, Length: 979, Exceptions: 0)
    Write errors:           0
    Non-2xx responses:      979
    Total transferred:      691297 bytes
    HTML transferred:       496478 bytes
    Requests per second:    10763.45 [#/sec] (mean)
    Time per request:       0.929 [ms] (mean)
    Time per request:       0.093 [ms] (mean, across all concurrent requests)
    Transfer rate:          7266.35 [Kbytes/sec] received
    
    发现错误率蛮高的(979)错误的页面都被默认引导到50x.html

    对服务器限流

    ngx_http_upstream_module

    对服务器进行(反向代理)限流
    upstream MyName {
        server 192.168.0.1:8080 weight=1 max_conns=10;
        server 192.168.0.2:8080 weight=1 max_conns=10;
    }
    
    max_conns=number
    限制到代理服务器的同时活动连接的最大数量(1.11.5)。 默认值为零,表示没有限制。 如果服务器组不驻留在共享内存中,则每个工作进程的限制都有效。
    如果启用了空闲保持活动连接,多个工作线程和共享内存,则到代理服务器的活动和空闲连接的总数可能会超过max_conns值。
  • 相关阅读:
    CentOS7 安装Docker 18.09.5
    CentOS7 安装Jenkins 2.164.2
    Python3从零开始爬取今日头条的新闻【一、开发环境搭建】
    Win10 安装Oracle11g2、配置PL/SQL Developer11环境
    IDEA 使用Mybatis效率飞起来的必备工具:MybatisCodeHelperPro 最新破解版,亲测可用!
    Navicat Premium 12 (64位)实现连接Oracle 11 (64位)
    VMware14 安装CentOS7 实现宿主机ping通虚拟机、虚拟机ping通宿主机、虚拟机能上网且能ping通百度
    Java中util.Date通过mybatis向数据库中datetime的操作!
    Java中try-catch-finally语句中return的执行顺序总结
    java中this用法总结
  • 原文地址:https://www.cnblogs.com/starcrm/p/12977077.html
Copyright © 2020-2023  润新知