跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[程序员] limit_req_zone 的使用疑问

发表于

今天在使用 NGINX 的 limit_req_zone 时,发现统计出来的数据和过滤结果不一致的问题,情况如下:


一、过滤:

nginx 配置文件添加:

limit_req_zone $binary_remote_addr zone=mylimit:100m rate=2000r/s;

location / {} 中添加:

limit_req zone=mylimit  burst=1000;

重启 nginx 后,在 error.log 的日志中,发现了大量的这样的日志:

2024/07/08 10:13:47 [error] 30#30: *75739 limiting requests, excess: 1.000 by zone "mylimit", client: x.x.x.x, server: xxx-zj.cn, request: "POST /rela/getid HTTP/1.1", host: "xxx-zj.cn",

那么问题就来了,公司的访问量绝对不会高到 2000R/S ,更别说单个 IP 了。因此采用了两种方法统计了一下请求量


二、统计:

1.通过 grep 日志文件中的每一秒,wc -l 汇总数量:

cat access.log |grep "\[08/Jul/2024:10:13:47" | wc -l 

结果是 178 ,在统计几个,也都是 200+ 300+的数量,绝对不超过 1000


2.通过 nginx_status 判断

写一个脚本,统计 10s 内的 http://127.0.0.1/nginx_status ,并相减,求每秒的 requests

for i in {1..10}
do
    # 获取当前的请求数
    status=$(curl -s http://127.0.0.1/nginx_status)
    current_requests=$(echo "$status" | grep 'server accepts handled requests' -A 1 | tail -n 1 | awk '{print $3}')
    
    # 将当前请求数存储到数组中
    requests+=($current_requests)
    
    # 等待 1 秒
    sleep 1
done

#### 计算每秒请求数的差值
echo "Requests difference per second:"
for (( i=1; i<${#requests[@]}; i++ ))
do
    diff=$((requests[i] - requests[i-1]))
    echo "Second $i: $diff requests"
done

运行脚本后,输出:
Requests difference per second:
Second 1: 258 requests
Second 2: 325 requests
Second 3: 334 requests
Second 4: 300 requests

...省略

结果也都是只有几百,远远没有到达 2000 的过滤阈值,请问大佬么,这是为什么?

Featured Replies

No posts to show

创建帐户或登录来提出意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.