用户名和密码方式限制

2019-09-28  本文已影响0人  a幕城

1.安装密码生成工具

[root@web01 ~]# yum install httpd-tools -y

2.生成密码

[root@web01 ~]# htpasswd -b -c /etc/nginx/auth_conf  oldxu 123456

3.修改nginx配置文件

[root@web01 ~]# cat  /etc/nginx/conf.d/mirror.oldxu.com.conf 
server {
    listen 80;
    server_name mirror.oldxu.com;
    root /code;
    charset utf8;
        autoindex on;           #开启目录索引,提供下载
        autoindex_exact_size off;   #以人性化方式显示大小
        autoindex_localtime on;     #与本地时间保持一致

    location / {
        index index.html;
    }

    location /centos {
        auth_basic "hello test";
        auth_basic_user_file "/etc/nginx/auth_conf";
    }
}

4.nginx实现限速

[root@web01 ~]# cat /etc/nginx/conf.d/mirror.oldxu.com.conf 
limit_req_zone $binary_remote_addr zone=req_od:10m rate=1r/s;

server {
    listen 80;
    server_name mirror.oldxu.com;
    root /code;
    charset utf8;
    autoindex on;               #开启目录索引,提供下载
    autoindex_exact_size off;   #以人性化方式显示大小
    autoindex_localtime on;     #与本地时间保持一致
    
    limit_req zone=req_od burst=3 nodelay;

    location / {
        index index.html;
    }

    location /centos {
        auth_basic "hello test";
        auth_basic_user_file "/etc/nginx/auth_conf";
    }
}

limit_req_zone $binary_remote_addr zone=req_one:10m rate=1r/s;
#第一个参数:$binary_remote_addr表示通过这个标识来做限制,限制同一客户端ip地址。
#第二个参数:zone=req_one:10m表示生成一个大小为10M,名为req_one的内存区域,用来存储访问的频次信息。
#第三个参数:rate=1r/s表示允许相同标识的客户端的访问频次,这里限制的是每秒1次,还可以30r/m。

limit_req zone=req_one burst=3 nodelay;
#第一个参数:zone=req_one 设置使用哪个配置区域来做限制,与上面limit_req_zone 里的name对应。
#第二个参数:burst=3,设置一个大小为3的缓冲区,当有大量请求过来时,超过了访问频次限制的请求可以先放到这个缓冲区内。
#第三个参数:nodelay,超过访问频次并且缓冲区也满了的时候,则会返回503,如果没有设置,则所有请求会等待排队。

5.连接限制

[root@web01 ~]# cat /etc/nginx/conf.d/mirror.oldxu.com.conf 
limit_conn_zone $binary_remote_addr zone=conn_od:10m;

server {
    listen 80;
    server_name mirror.oldxu.com;
    root /code;
    charset utf8;
    autoindex on;           #开启目录索引,提供下载
    autoindex_exact_size off;   #以人性化方式显示大小
    autoindex_localtime on;     #与本地时间保持一致
    limit_conn conn_od 2;

    location / {
        index index.html;
    }

    location /centos {
        auth_basic "hello test";
        auth_basic_user_file "/etc/nginx/auth_conf";
    }
}

6.速率限制

[root@web01 ~]# cat /etc/nginx/conf.d/mirror.oldxu.com.conf 
limit_conn_zone $binary_remote_addr zone=conn_od:10m;

server {
    listen 80;
    server_name mirror.oldxu.com;
    root /code;
    charset utf8;
        autoindex on;               #开启目录索引,提供下载
        autoindex_exact_size off;   #以人性化方式显示大小
        autoindex_localtime on;     #与本地时间保持一致
    limit_conn conn_od 2;
    limit_rate_after 100m;
    limit_rate 100k;

    location / {
        index index.html;
    }

    location /centos {
        auth_basic "hello test";
        auth_basic_user_file "/etc/nginx/auth_conf";
    }
}

7.综合案例、限制web服务器请求数处理为1秒一个,触发值为5、限制并发连接数为1、限制下载速度为100k

[root@web01 conf.d]# cat mirror.oldxu.com.conf 
limit_req_zone  $binary_remote_addr zone=req_od:10m rate=1r/s;
limit_conn_zone $binary_remote_addr zone=conn_od:10m;

server {
    listen 80;
    server_name mirror.oldxu.com;
    root /code;
    charset utf8;
    autoindex on;
    autoindex_exact_size off;
    autoindex_localtime on;
    limit_req zone=req_od burst=5 nodelay;
    limit_conn conn_od 1;
    limit_rate_after 100m;
    limit_rate 100k;
    
    error_page 503 @errpage;
    location @errpage {
        default_type text/html;
        return 200 ' Oldxu提示--->请充值会员';
    }
    location / {
        index index.html;
    }
}

11.nginx状态指标,俗称7种状态   监控Nginx


    location /nginx_status {
        stub_status;
    }

Active connections: 2               
server accepts handled requests
            2     2      17     
Reading: 0 Writing: 1 Waiting: 1

Active connections      活跃的连接数
accepts                 总的TCP连接数
handled                 成功握手的TCP连接数
accepts -   handled     失败的TCP连接数
requests                总的请求数
Reading                 读取到请求头的数量。
Writing                 响应客户端到的数量。
Waiting                 客户端与服务端的连接数

vim /etc/nginx/nginx.conf
    keepalive_timeout 65;       #长连接超时时间
    keepalive_timeout 0;        #模拟短连接效果
上一篇 下一篇

猜你喜欢

热点阅读