Configuration 8-minute read
Clash Configuration File: YAML Structure Explained, Section by Section—from port to rules
A section-by-section guide to the ports, DNS, proxies, proxy-groups, and rules in config.yaml, covering field meanings, indentation, and common syntax mistakes.
Ports: Choosing between port, socks-port, and mixed-port
Start at the top of the file. port is the local HTTP proxy listener, while socks-port listens for SOCKS connections. For example, in mihomo, set port: 7890 and socks-port: 7891; apps can then use whichever protocol they support. Set mixed-port: 7890 instead to serve HTTP and SOCKS on the same listener. Enable only the fields you need, and don't assign two separate listeners to the same address and port.
This guide uses mixed-port: 7890 and allow-lan: false for local testing. In the app, choose “HTTP proxy,” enter 127.0.0.1 as the address, and set the port to 7890. If the client has its own port setting, use the port it actually listens on after applying the configuration; some GUI clients override the value in the file. A node's port is the upstream service port—not the local listener described here.
DNS: nameserver and enhanced-mode
dns is a mapping. Set enable: true to turn on the core's DNS module; nameserver lists the upstream resolvers. This example uses 223.5.5.5 and 119.29.29.29, both public DNS addresses. If your device needs to resolve internal company domains, check which DNS servers your organization requires before deciding how to configure those domains. Enabling the core's DNS module does not change your operating system's DNS settings, and it cannot restore a domain name to a request that the app has already resolved.
This example sets enhanced-mode: redir-host, which uses the actual address returned for the requested domain. With fake-ip, the resolver can return a mapped address from a reserved range first; when the core receives a connection, it uses that mapping to recover the domain. After switching modes, if a device feature or LAN app relies on real addresses, check which domain it actually queries, then consider excluding that name from fake-IP responses with fake-ip-filter. Restart the test app or clear its DNS cache before testing so an old response doesn't get mistaken for the new configuration.
This example doesn't set dns.listen or change the system DNS server to 127.0.0.1. 7890 is the proxy listener, not a DNS port. To provide local DNS service to other programs, configure a DNS listen address and port separately, and specify which clients should use it.
Proxies and proxy groups: Connecting proxies to proxy-groups
The top-level proxies key holds the proxy list. Each entry has a name, a type that specifies the protocol, and a server and port for the upstream. This guide uses an HTTP upstream to illustrate the fields. Selecting it will forward traffic only if an HTTP proxy is actually listening on 127.0.0.1:18080. Use a dedicated upstream port; pointing it back to this Clash instance at 7890 would send requests in a loop.
The top-level proxy-groups key holds the policy group list. Each group has a name, but the group's proxies field contains names—not full proxy definitions. The “Manual Select” example uses type: select and lists the built-in DIRECT policy and the “Local Test Proxy” node as options. Check the current selection on the client's “Proxies” or “Proxy Groups” page. A client or core may remember the previous selection, so don't assume the active route from the order of options in the YAML file.
- If you rename a node, update its name in any groups that reference it. If you rename a group, update the rules that refer to it.
- Groups can reference other groups, but avoid circular references: if Group A references Group B, Group B must not reference Group A.
- If your configuration comes from a remote subscription, automatic updates may overwrite manual edits. First make a separate local copy. If the client supports overrides, put changes you want to keep in the override configuration instead.
Rules: Ordering from DOMAIN to MATCH
rules is an ordered list. In mode: rule, the core checks rules from top to bottom and applies the policy on the first match. A typical domain rule has three parts: the rule type, the match value, and the policy. The final MATCH rule has no match value, so it contains only the type and policy. Put narrow exceptions before broader rules, and keep the fallback last.
| Syntax | What it matches |
|---|---|
DOMAIN,example.org,Manual Select |
Matches only example.org, not its subdomains. |
DOMAIN-SUFFIX,example.org,Manual Select |
Matches example.org and its subdomains, but not notexample.org. |
MATCH,DIRECT |
If none of the preceding rules match, use the built-in direct-connection policy. |
When troubleshooting, first check whether the client is in “Rule” mode, then inspect the destination and matched rule in the connection details. If an app sends only an IP address to the proxy and the core can't recover or identify the domain, a DOMAIN rule won't match. Conversely, if a connection matches “Manual Select” but still goes direct, the group may simply be set to DIRECT. Check the rule match and the final route separately before deciding which section to change.
Complete config.yaml example
Here is a complete configuration for local testing with mihomo. Select DIRECT in the policy group first to test the proxy listener and rule matching; you don't need to start the example HTTP upstream. Once an upstream is listening on 18080, switch to “Local Test Proxy” to verify forwarding.
mixed-port: 7890
allow-lan: false
mode: rule
log-level: info
dns:
enable: true
enhanced-mode: redir-host
nameserver:
- 223.5.5.5
- 119.29.29.29
proxies:
- name: Local Test Proxy
type: http
server: 127.0.0.1
port: 18080
proxy-groups:
- name: Manual Select
type: select
proxies:
- DIRECT
- Local Test Proxy
rules:
- DOMAIN,example.org,Manual Select
- MATCH,DIRECT
To test, use an app configured with the local HTTP proxy to visit https://example.org/. In the connection details, check whether the hostname is example.org and whether the DOMAIN rule matched. Then visit https://example.com/ and see whether it falls through to MATCH. If the app is reusing an existing connection, close it and make a new request; verify policy changes with new connections.
Config errors: Check indentation, references, and listeners
- Import fails with a parse error? Check indentation first. Top-level keys such as
dns:,proxies:,proxy-groups:, andrules:must start at the left margin. Follow the example and indent each level by two spaces. Under a node's- name:, thetype:key on the next line should align withname:. Don't mix tabs and spaces at the same level, or use full-width punctuation in place of standard colons and commas. If a node name contains a colon followed by a space, or a space followed by#(which YAML treats as the start of a comment), quote the entire name. If the error points to a line, check the line above it too. - File parses, but the configuration won't apply? Check names and fields. Verify that rule references match group names and group references match node names exactly, including capitalization and trailing spaces. Use each top-level key only once; to add rules, append them under the existing
rules:key. Also check which core the client is actually using: Clash Meta was renamed to mihomo, but older clients may still use the original Clash. Being able to parse YAML doesn't mean a core supports the same protocols and fields. - Can't connect after applying the configuration? Trace the route from listener to upstream. If the log says the port is already in use, check whether another instance is listening on
7890. If you change it to7892, update the proxy port in the app as well. If the listener shows connections but selecting the test node returns “connection refused,” check the upstream service on18080. If there are no connections at the listener, first confirm the app is using the local HTTP proxy instead of changing DNS or rules.