0.0.4 • Published 5 years ago
egg-phenex v0.0.4
egg-phenex
基于eggjs的接口请求限制插件
- 支持根据请求路由,分别配置请求速率限制
- 安装,配置即可用,不侵入业务代码
依赖于egg-redis插件,请在业务框架中安装开启
如果用nginx配置了反向代理,请在nginx中配置X-Forwarded-For请求头,并在egg框架配置中设置 proxy = true
Install
$ npm i egg-phenex --save
Usage
// {app_root}/config/plugin.js
exports.phenex = {
enable: true,
package: 'egg-phenex',
};
Configuration
// {app_root}/config/config.default.js
// 依赖egg-redis插件,请配置多实例
config.redis = {
clients: {
session: {
host: '127.0.0.1',
port: 6379,
password: 12345678,
db: 0,
},
rate_limit: {
host: '127.0.0.1',
port: 6379,
password: 12345678,
db: 1,
},
},
};
// {app_root}/config/config.default.js
exports.rateLimit = {
router: [
{
path: '/api/A', // 限制路由路径
gap: 1000, // 相同ip下请求的间隔,单位毫秒
max: 9999,
time: '1m', // 时间单位 s m h d y, 默认为分钟
message: '请求异常,超过限制', // 自定义请求超限错误信息
},
{
path: '/api/B/**', // 限制路由路径
gap: 2000, // 相同ip下请求的间隔,单位毫秒
max: 20,
time: '17m', // 时间单位 s m h d y ...
message: '自定义错误信息', // 自定义请求超限错误信息
},
],
};
see config/config.default.js for more detail.
Example
Questions & Suggestions
Please open an issue here.