Rule Fundamentals
Rules are the core functionality of ClashX, determining which трафик should go through proxies and which should connect directly. Understanding and mastering rule writing makes your proxy configuration smarter and more efficient.
Rules are essentially a set of matching conditions. When a сеть request meets a rule, ClashX processes the request according to the policy specified in the rule.
When you visit a website, ClashX checks the rule list from top to bottom. Once it finds a matching rule, it applies it immediately and stops checking subsequent rules. Therefore, rule order is crucial.
Components of a Rule
Each rule consists of three parts:
Rule Types Explained
DOMAIN - Exact Domain Match
The most precise matching method, only matches exactly identical domains.
DOMAIN Rule Examples
- DOMAIN,www.google.com,🚀 代理
- DOMAIN,api.github.com,🚀 代理
- DOMAIN,www.baidu.com,DIRECT
Note: Only matches when accessing www.google.com. Accessing mail.google.com or google.com will not match.
DOMAIN-SUFFIX - Domain Suffix Match
The most commonly used rule type, matches the specified domain and all its subdomains.
DOMAIN-SUFFIX Rule Examples
- DOMAIN-SUFFIX,google.com,🚀 代理
- DOMAIN-SUFFIX,youtube.com,🎬 流媒体
- DOMAIN-SUFFIX,cn,DIRECT
Note: google.com, www.google.com, and mail.google.com will all be matched.
DOMAIN-KEYWORD - Domain Keyword Match
Matches requests where the domain contains the specified keyword. Flexible but may cause false matches.
DOMAIN-KEYWORD Rule Examples
- DOMAIN-KEYWORD,google,🚀 代理
- DOMAIN-KEYWORD,youtube,🎬 流媒体
- DOMAIN-KEYWORD,ads,REJECT
Warning: Use with caution. "google" will match google.com, mygooglesite.com, and all domains containing this keyword.
IP-CIDR - IP Address Range Match
Matches based on IP address ranges, suitable for connections without domain names.
IP-CIDR Rule Examples
- IP-CIDR,192.168.0.0/16,DIRECT
- IP-CIDR,10.0.0.0/8,DIRECT
- IP-CIDR,91.108.56.0/22,🚀 代理,no-resolve
no-resolve parameter: Skips DNS resolution and matches IP directly, improving производительность.
GEOIP - Geographic Location Match
Matches based on the geographic location of IP addresses.
GEOIP Rule Examples
- GEOIP,CN,DIRECT
- GEOIP,US,🇺🇸 美国节点
- GEOIP,JP,🇯🇵 日本节点
| Rule Type | Precision | Производительность | Recommended Use |
|---|---|---|---|
| DOMAIN | Highest | Fast | Specific domains |
| DOMAIN-SUFFIX | High | Fast | Whole site proxy |
| DOMAIN-KEYWORD | Средне | Средне | Batch matching |
| IP-CIDR | High | Slow (needs resolution) | IP-based matching |
| GEOIP | Средне | Fast | Regional routing |
Rule Syntax Deep Dive
Basic Syntax Format
ClashX rules are written in YAML format and must strictly follow indentation rules.
Standard Rule Syntax
rules:
- RULE-TYPE,MATCH-VALUE,POLICY-NAME
- RULE-TYPE,MATCH-VALUE,POLICY-NAME,OPTIONS
Rule Parameter Options
Some rule types support additional parameters to optimize производительность or behavior.
- no-resolve: No DNS resolution, applicable to IP-CIDR rules
- src-ip: Match based on source IP address
- src-port: Match based on source port
Special Rule Types
Besides basic rules, ClashX also supports some advanced rule types:
Advanced Rule Examples
# PROCESS-NAME - 根据进程名匹配
- PROCESS-NAME,Telegram,🚀 代理
# RULE-SET - 引用规则集
- RULE-SET,proxy-list,🚀 代理
# MATCH - 默认策略(必须放在最后)
- MATCH,🚀 节点选择
The MATCH rule must be placed at the end of the rule list. It will match all трафик not handled by previous rules.
Writing Лучшие практики
Rule Order Оптимизация
Correct rule order can significantly improve matching efficiency and accuracy:
- First: REJECT rules (block ads and tracking)
- Next: Special DOMAIN rules (high-priority websites)
- Middle: DOMAIN-SUFFIX and DOMAIN-KEYWORD rules
- Later: IP-CIDR and GEOIP rules
- Last: MATCH default rule
Производительность Оптимизация Tips
Key points for writing high-производительность rules:
1. Prefer DOMAIN-SUFFIX over DOMAIN-KEYWORD
2. Add no-resolve parameter to IP-CIDR rules
3. Use RULE-SET to manage large набор правилs
4. Avoid excessive regex rules
Common Mistakes to Avoid
Pitfalls to watch out for when writing rules:
- Indentation errors: YAML is strict about indentation, use 2 spaces
- Rule conflicts: Ensure high-priority rules come first
- Policy name errors: Ensure policy names match those defined in proxy-groups
- Overusing KEYWORD: Can easily cause false matches
Rule Organization
Recommended to organize rules by function, using comments as separators:
Rule Organization Example
rules:
# ======= 广告拦截 =======
- DOMAIN-SUFFIX,ads.google.com,REJECT
- DOMAIN-KEYWORD,analytics,REJECT
# ======= 本地直连 =======
- DOMAIN-SUFFIX,localhost,DIRECT
- IP-CIDR,127.0.0.0/8,DIRECT
# ======= 流媒体服务 =======
- DOMAIN-SUFFIX,netflix.com,🎬 流媒体
- DOMAIN-SUFFIX,youtube.com,🎬 流媒体
# ======= 国内网站 =======
- GEOIP,CN,DIRECT
# ======= 默认策略 =======
- MATCH,🚀 节点选择
Practical Case Studies
Case 1: Complete Routing Rules
A complete rule configuration suitable for daily use:
Daily Use Rule Set
rules:
# 广告拦截
- DOMAIN-KEYWORD,adservice,REJECT
- DOMAIN-SUFFIX,doubleclick.net,REJECT
# Apple 服务
- DOMAIN-SUFFIX,apple.com,DIRECT
- DOMAIN-SUFFIX,icloud.com,DIRECT
- DOMAIN-SUFFIX,apple-dns.net,DIRECT
# 流媒体平台
- DOMAIN-SUFFIX,netflix.com,🎬 流媒体
- DOMAIN-SUFFIX,youtube.com,🎬 流媒体
- DOMAIN-SUFFIX,spotify.com,🎬 流媒体
# 社交媒体
- DOMAIN-SUFFIX,twitter.com,🚀 代理
- DOMAIN-SUFFIX,facebook.com,🚀 代理
- DOMAIN-SUFFIX,instagram.com,🚀 代理
# 开发工具
- DOMAIN-SUFFIX,github.com,🚀 代理
- DOMAIN-SUFFIX,stackoverflow.com,🚀 代理
# 中国大陆网站
- GEOIP,CN,DIRECT
# 局域网直连
- IP-CIDR,192.168.0.0/16,DIRECT,no-resolve
- IP-CIDR,10.0.0.0/8,DIRECT,no-resolve
# 默认策略
- MATCH,🚀 节点选择
Case 2: Gaming Acceleration Rules
Rule configuration optimized for gaming:
Gaming Acceleration Rules
rules:
# Steam
- DOMAIN-SUFFIX,steampowered.com,🎮 游戏
- DOMAIN-SUFFIX,steamcommunity.com,🎮 游戏
# Epic Games
- DOMAIN-SUFFIX,epicgames.com,🎮 游戏
# PlayStation
- DOMAIN-SUFFIX,playstation.com,🎮 游戏
- DOMAIN-SUFFIX,playstation.net,🎮 游戏
# Xbox
- DOMAIN-SUFFIX,xbox.com,🎮 游戏
- DOMAIN-SUFFIX,xboxlive.com,🎮 游戏
# 游戏 IP 段(示例)
- IP-CIDR,185.25.182.0/24,🎮 游戏,no-resolve
- MATCH,DIRECT
Case 3: Конфиденциальность Protection Rules
Rule configuration for enhanced privacy protection:
Конфиденциальность Protection Rules
rules:
# 屏蔽广告跟踪
- DOMAIN-KEYWORD,analytics,REJECT
- DOMAIN-KEYWORD,tracking,REJECT
- DOMAIN-KEYWORD,telemetry,REJECT
# 屏蔽常见广告域名
- DOMAIN-SUFFIX,googlesyndication.com,REJECT
- DOMAIN-SUFFIX,googleadservices.com,REJECT
- DOMAIN-SUFFIX,facebook.net,REJECT
# 屏蔽数据收集
- DOMAIN-KEYWORD,crashlytics,REJECT
- DOMAIN-KEYWORD,bugly,REJECT
# 正常流量
- MATCH,🚀 节点选择
Advanced Rule Techniques
Using Rule Providers
Rule providers allow you to load rules from external files or URLs, making management and updates easier:
Rule Provider Configuration Example
rule-providers:
reject:
type: http
behavior: domain
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/reject.txt"
path: ./ruleset/reject.yaml
interval: 86400
proxy:
type: http
behavior: domain
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/proxy.txt"
path: ./ruleset/proxy.yaml
interval: 86400
direct:
type: http
behavior: domain
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/direct.txt"
path: ./ruleset/direct.yaml
interval: 86400
rules:
- RULE-SET,reject,REJECT
- RULE-SET,proxy,🚀 代理
- RULE-SET,direct,DIRECT
- MATCH,🚀 节点选择
Script Rules
Use JavaScript to write custom matching logic:
Script rules provide maximum flexibility, enabling complex matching logic such as time-based routing, dynamic node selection, and other advanced features.
Отладка and Testing Rules
How to verify if rules are working correctly:
Rule Maintenance Recommendations
Methods to keep rules efficient and up-to-date:
- Regular review: Check rules monthly, remove unused ones
- Use version control: Manage файл конфигурацииs with Git
- Subscribe to community набор правилs: Leverage community-maintained набор правилs
- Keep backups: Save working configuration versions
Rule Writing Summary
Mastering rule writing is a key skill for using ClashX. Start with simple DOMAIN-SUFFIX rules and gradually learn more complex rule types. Remember, the best rule configuration is simple, efficient, and meets your actual needs. Don't blindly pursue complexity - practicality is king.