这是一个简单的http/https/ws/wss均衡负载、故障转移路由/代理,通过刷新配置不中断地:
- 监听占用等待
- 动态后端
- 路径继承
- 自定权重
- 故障转移
- 自定义头
- 多种轮询
- 请求路径过滤
- 请求头过滤
- 请求数据过滤
支持嵌入到其他项目中/独立运行
命令行参数:
Usage of ./main:
-adminPath string
adminPath, eg:/123/12/
-adminPort int
adminPort, eg:10908
-c string
config (default "main.json")
-dbFile string
dbFile, eg./log/20060102150405.sqlite ,defalut no db File
-decrypt string
decrypt with pri.pem
-encrypt string
encrypt with pub.pem
-genKey
gen new pub.pem and pri.pem
-logFile string
logFile, defalut no log file
-noDebugLog
noDebugLog
-noLog
noLog
-reload
reload, when adminPort/adminPath set
-restart
restart, when adminPort/adminPath set
-stop
stop, when adminPort/adminPath set
示例:
[
{
"addr": "127.0.0.1:10000",
"routes": [
{
"name": "route1",
"path": ["/"],
"pathAdd": true,
"filiters": [{
"reqUri": {
"accessRule": "!{stop}",
"items": {
"stop": "(main\\.json)"
}
}
}],
"backs": [
{
"name": "back1",
"to": "./",
"weight": "1"
}
]
}
]
}
]curl http://127.0.0.1:10000/
<!doctype html>
<meta name="viewport" content="width=device-width">
<pre>
<a href="front.run">front.run</a>
<a href="main.json">main.json</a>
<a href="main.log">main.log</a>
</pre>
curl http://127.0.0.1:10000/main.json -I
HTTP/1.1 403 Forbidden
X-Front-Error: ErrPatherCheckFail
Date: Wed, 30 Oct 2024 17:50:22 GMT
curl http://127.0.0.1:10000/main.log -I
HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Length: 1700014
Content-Type: text/x-log; charset=utf-8
Last-Modified: Wed, 30 Oct 2024 17:51:23 GMT
Date: Wed, 30 Oct 2024 17:51:23 GMT
处理流程:
请求将从上至下尝试route的filiter(reqAddr、reqHost、reqUri、reqHeader),匹配成功时,再根据route中back的filiter进行过滤,并按rollRule进行排序,然后逐个尝试,若某个back成功响应,则结束本次请求的处理
配置:
为json数组格式[],下面为数组中的其中一个{},下述字段倾斜的,表示不会根据配置动态加载
config:
- addr: string 监听端口 例:
0.0.0.0:8081 - matchRule: string 匹配规则,默认
prefix。prefix:当未匹配到时,返回最近的/匹配,all:当未匹配到时,返回404 - reqIdLoop: uint 请求id环大小,用于日志识别请求,默认
1000 - copyBlocks:{} 转发的块
- size: string 转发的块大小,默认
16K - num: int 转发的块数量,默认
1000
- size: string 转发的块大小,默认
- retryBlocks: {} 重试, 当停用时,不进行重试。其他情况:1.当所有块都在使用中时,不进行重试。2.当请求没有
Content-Length时,将会重试。- size: string 重试的块大小,当请求body超过配置的大小时,将会导致无法继续失败回落,默认
1M - num: int 重试的块数量,默认
0,为0时停用重试
- size: string 重试的块大小,当请求body超过配置的大小时,将会导致无法继续失败回落,默认
- tls: {} 启用tls, 默认空
- pub: string 公钥pem路径,支持从
http/https获取 - key: string 私钥pem路径,支持从
http/https获取 - decrypt: []string cmd解密key,公/私钥会通过stdin输入,并使用stdout输出作为实际使用公/私钥,为空时不执行
- pub: string 公钥pem路径,支持从
- routes: [] 路由
- name: string 路由名称,将在日志中显示
- path: []string 路由路径
- rollRule: string 可选
order(按顺序,每次都从第一个开始尝试)loop(轮流)disableC_MinFirst(禁用数较少的优先)dealingC_MinFirst(连接数较少的优先)chosenC_MinFirst(被选择较少的优先)- (使用rand.Shuffle随机,默认)
- alwaysUp: bool 总是在线,当所有后端都在禁用时,启用被禁用次数最少的一个后端
- setting... 将会给backs默认值
- backs: [] 后端
- name: string 后端名称,将在日志中显示
- to: string 后端地址,说明如下:
- 为空时,将会默认返回200
- 含有
://时,例s://www.baidu.com,会根据客户端自动添加http or ws在地址前 - 不含
://时,将会尝试解析成本地文件,当指向zip文件,将尝试读取
- weight: string uint 权重,按routes中的全部back的权重比分配,当权重为0时,将停止新请求的进入
- setting...
setting: setting代指下述各配置
-
pathAdd: bool 将客户端访问的路径附加在path上 例:/api/req => /ws => /ws/api/req
-
splicing: int 当客户端支持cookie时,将会固定使用后端多少秒,默认不启用
-
ctxToSec: float64 当后端响应超过(ws则指初次返回时间)指定秒,将会errBanSec,并响应504
-
errToSec: float64 当得到后端响应时,超过(ws则指初次返回时间)指定秒,将会errBanSec
-
errBanSec: int 当后端错误时(指连接失败,不指后端错误响应),将会禁用若干秒
-
insecureSkipVerify: bool 忽略不安全的tls证书
-
verifyPeerCer: string 路径,校验服务器证书,使用intermediate_ca
-
proxy: string 使用proxy进行请求,支持
socks5:\\,http:\\,https:\\(仅http、https、ws、wss有效) -
filiters: []
filiters中同个{}为和关系,不同{}为或关系
- reqAddr:{} 请求后端前,请求Addr过滤器
- accessRule:string 布尔表达式,为true时才通过,例
{id}|(!{id2}&{id3}) - items: map[string]string
- id: matchExp
- accessRule:string 布尔表达式,为true时才通过,例
- reqHost:{} 请求后端前,请求Host过滤器
- accessRule:string 布尔表达式,为true时才通过,例
{id}|(!{id2}&{id3}) - items: map[string]string
- id: matchExp
- accessRule:string 布尔表达式,为true时才通过,例
- reqUri:{} 请求后端前,请求路径过滤器
- accessRule:string 布尔表达式,为true时才通过,例
{id}|(!{id2}&{id3}) - items: map[string]string
- id: matchExp
- accessRule:string 布尔表达式,为true时才通过,例
- reqHeader:{} 请求后端前,请求头处理器
- accessRule:string 布尔表达式,为true时才通过
- items: map[string]{}
- id:
- key: string header头
- matchExp: string
- id:
- resHeader:{} 返回后端的响应前,请求头处理器
- accessRule:string 布尔表达式,为true时才通过
- items: map[string]{}
- id:
- key: string header头
- matchExp: string
- id:
- reqBody:{} 请求后端前,请求数据过滤器(仅route层有效)
- action: string 可选
access、deny - reqSize: string 限定请求数据大小,默认为
1M - matchExp: string
access时如不匹配将结束请求。deny时如匹配将结束请求
- action: string 可选
- reqAddr:{} 请求后端前,请求Addr过滤器
-
dealer: {}
- reqUri:[] 请求后端前,路径处理器
- action: string 可选
replace。 - matchExp: string
replace时结合value进行替换 - value: string
replace时结合matchExp进行替换。
- action: string 可选
- reqHeader:[] 请求后端前,请求头处理器
- action: string 可选
replace、add、del、set。 - key: string 具体处理哪个头
- matchExp: string
replace时结合value进行替换 - value: string
replace时结合matchExp进行替换。add时将附加值。set时将覆盖值。
- action: string 可选
- reqFunc:{} 请求后端前,调用方法
- dealer: func(r *http.Request) 当通过编程方式启动时,将调用注册的方法
- resHeader:[] 返回后端的响应前,请求头处理器
- action: string 可选
add、del、set。 - key: string 具体处理哪个头
- matchExp: string
replace时结合value进行替换 - value: string
replace时结合matchExp进行替换。add时将附加值。set时将覆盖值。
- action: string 可选
- resBody:[] 返回后端响应前,数据处理器(仅http、https有效),使用转发块进行处理
- action: string 可选
replace。 - matchExp: string
replace时结合value进行替换 - value: string
replace时结合matchExp进行替换。
- action: string 可选
- resStatus:{} 返回后端响应前,修改响应码(仅to为空、http、https时有效)
- matchExp: string 正则表达式
- value: int
- resFunc:{} 返回后端响应前,调用方法(仅http、https、ws、wss时有效)
- dealer: func(req *http.Request, res *http.Response) 当通过编程方式启动时,将调用注册的方法
- reqUri:[] 请求后端前,路径处理器
可以使用的环境变量(仅能单独使用,不能和字符串拼合等):
$remote_addr:当存在X-Real-IP头时,取其值,否则取请求端的远程地址。在dealer.reqHeader.value、dealer.resHeader.value可用