Nginx的限流

Nginx的限流

nginx还可以用来限流。

limit_req_zone 用来限制单位时间内的请求数目,以及速度限制。
limit_req_conn 用来限制同一时间连接数,即并发限制。

配置

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    limit_req_zone $binary_remote_addr zone=mylimit:10m rate=1r/s;


    server {
        listen       80;
        server_name  localhost;

        limit_req zone=mylimit burst=1 nodelay;
        limit_req_status 598;

        location / {
          root /usr/share/nginx/html;
          index index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

在这里插入图片描述

[20:03:43] xiaoyu:nginx $ ab -n 4  -c 2 http://localhost/
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient).....done


Server Software:        nginx/1.23.1
Server Hostname:        localhost
Server Port:            80

Document Path:          /
Document Length:        615 bytes

Concurrency Level:      2
Time taken for tests:   0.043 seconds
Complete requests:      4
Failed requests:        2
   (Connect: 0, Receive: 0, Length: 2, Exceptions: 0)
Non-2xx responses:      2
Total transferred:      1924 bytes
HTML transferred:       1230 bytes
Requests per second:    93.28 [#/sec] (mean)
Time per request:       21.441 [ms] (mean)
Time per request:       10.720 [ms] (mean, across all concurrent requests)
Transfer rate:          43.82 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:     4   19  16.1     33      33
Waiting:        4   19  16.2     33      33
Total:          4   19  16.1     33      33

Percentage of the requests served within a certain time (ms)
  50%     33
  66%     33
  75%     33
  80%     33
  90%     33
  95%     33
  98%     33
  99%     33
 100%     33 (longest request)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值