Clash 代理规则语法 Clash 的规则文件采用 JSON 格式,规则由 match 和 replace 两个部分组成: match: 用于匹配符合条件的请求。 replace: 用于指定替代规则。 规则可以通过多个 match 和 replace 组合成一个完整的代理规则。 基本规则结构 { "rules": [ { "match": { "ip": ["192.168.1.1"], "port": [80, 443], "domain": "example.com" }, "replace": { "host": "proxy.example.com", "port": 808, "next": [ { "host": "target.example.com", "port": 80 } ] } } ] } 规则字段说明 match: ip: 匹配特定 IP 地址。 port: 匹配特定端口。 domain: 匹配特定域名。 path: 匹配特定路径。 scheme: 匹配特定协议(如 http, https)。 query: 匹配特定查询参数。 header: 匹配特定 HTTP 头。 replace: host: 指定代理的目标主机。 port: 指定代理的目标端口。 next: 指定后续的代理规则(支持链式代理)。 remove-header: 去掉特定 HTTP 头。 add-header: 添加特定 HTTP 头。 body: 修改或添加请求体。...
Clash 代理规则语法
Clash 的规则文件采用 JSON 格式,规则由 match 和 replace 两个部分组成:
match: 用于匹配符合条件的请求。replace: 用于指定替代规则。
规则可以通过多个 match 和 replace 组合成一个完整的代理规则。
基本规则结构
{
"rules": [
{
"match": {
"ip": ["192.168.1.1"],
"port": [80, 443],
"domain": "example.com"
},
"replace": {
"host": "proxy.example.com",
"port": 808,
"next": [
{
"host": "target.example.com",
"port": 80
}
]
}
}
]
}
规则字段说明
-
match:ip: 匹配特定 IP 地址。port: 匹配特定端口。domain: 匹配特定域名。path: 匹配特定路径。scheme: 匹配特定协议(如http,https)。query: 匹配特定查询参数。header: 匹配特定 HTTP 头。
-
replace:host: 指定代理的目标主机。port: 指定代理的目标端口。next: 指定后续的代理规则(支持链式代理)。remove-header: 去掉特定 HTTP 头。add-header: 添加特定 HTTP 头。body: 修改或添加请求体。
常见代理规则示例
domain 过滤(域名过滤)
禁止访问某些域名:
{
"rules": [
{
"match": {
"domain": "ads.example.com"
},
"replace": {
"next": []
}
}
]
}
效果:访问 ads.example.com 被代理到空白页面。
端口转发
将局域网端口转发到远程服务器:
{
"rules": [
{
"match": {
"ip": "192.168.1.1",
"port": [80, 443]
},
"replace": {
"host": "remote.example.com",
"port": 808
}
}
]
}
效果:局域网内的 168.1.1 端口 80 和 443 转发到 remote.example.com 的 808 端口。
限制访问某个网络
限制某个子网的访问:
{
"rules": [
{
"match": {
"ip": ["192.168.1./24"]
},
"replace": {
"next": []
}
}
]
}
效果:阻止对 168.1./24 子网的访问。
限速
限制某个域名的下载速度:
{
"rules": [
{
"match": {
"domain": "example.com"
},
"replace": {
"限速": 100M
}
}
]
}
效果:访问 example.com 的速度被限制为 100Mbps。
黑名单规则
禁止访问某些网站:
{
"rules": [
{
"match": {
"domain": ["blackhole1.com", "blackhole2.com"]
},
"replace": {
"next": []
}
}
]
}
效果:访问 blackhole1.com 和 blackhole2.com 被代理到空白页面。
重写域名
将某个域名重写为另一个域名:
{
"rules": [
{
"match": {
"domain": "example.com"
},
"replace": {
"host": "proxy.example.com"
}
}
]
}
效果:访问 example.com 被代理到 proxy.example.com。
链式代理
多级代理规则:
{
"rules": [
{
"match": {
"domain": "origin.example.com"
},
"replace": {
"host": "first.proxy.example.com",
"port": 808,
"next": [
{
"host": "second.proxy.example.com",
"port": 808
}
]
}
}
]
}
效果:origin.example.com → first.proxy.example.com → second.proxy.example.com。
高级规则应用
Clash 支持更复杂的规则组合,
- 正则表达式匹配:
{ "match": { "domain": "(?:\\.com|\\.net)" } }匹配所有
.com和.net域名。 - 路径匹配:
{ "match": { "path": "/api/(.*)" }, "replace": { "next": [ { "host": "internalapi.example.com", "path": "/$1" } ] } }将
/api/路径的请求转发到internalapi.example.com。 - 查询参数匹配:
{ "match": { "query": "param=value" } }匹配带有
param=value查询参数的请求。
Clash 配置文件结构
Clash 的配置文件通常位于 /etc/clash/clash.json 或用户目录下的 .clash.json 文件中,配置文件包含以下主要部分:
{
"port": 789, // 代理端口
"listen": "...", // 监听地址
"max_connections": 100, // 最大连接数
"timeout": 10000, // 代理超时
"DNS": "192.168.1.1", // DNS 服务器
"HTTP": "http://localhost:789", // HTTP 代理
"HTTPS": "http://localhost:789", // HTTPS 代理
"rules": [ // 代理规则
...
]
}
注意事项
- 规则优先级:Clash 的规则是按顺序执行的,先匹配的规则优先生效。
- 链式代理:
replace中的next可以链式调用多个代理规则。 - 正则表达式:Clash 支持使用正则表达式,需要在
match和replace中使用regex字段。 - 测试规则:建议在测试环境中验证规则是否正确,避免误删或误改重要规则。

相关文章







