使用Nginx限制服务器脚本

2024-10-06 13:46:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我的服务器上有一个(python)脚本,我不想让同一个人/机器人经常执行它。每5秒可以,但如果有人试图更频繁地运行它,它只会损害我的服务器。。。在

因为这个应用只是一个小的周末项目,我不想使用一些复杂的欺诈/ddos防护系统。使用Nginx ngx_http_limit_req_module应该有一种简单的方法来实现这一点。在

我发现手册有点混乱。。。您是否有此方案的示例?在

每5秒只允许每个ip连接到某个位置。


Tags: 项目方法服务器脚本http示例方案机器人
1条回答
网友
1楼 · 发布于 2024-10-06 13:46:46

The rate is specified in requests per second (r/s). If a rate of less than one request per second is desired, it is specified in request per minute (r/m). For example, half-request per second is 30r/m.

# Allow not more than 12 request per minute at an average, with bursts not exceeding 1 requests.
limit_req_zone $binary_remote_addr zone=one:10m rate=12r/m;

server {
    location /path/to/python/script.py {
        limit_req zone=one burst=1;
    }
}

相关问题 更多 >