Clash config.yaml Explained: Ports, DNS, Proxies, Proxy Groups, and Rules
A section-by-section guide to config.yaml, covering ports, modes, DNS, proxies, proxy groups, rules, YAML indentation, and common mistakes.
Ports: How apps connect to Clash
port is the HTTP proxy port, socks-port is the SOCKS proxy port, and mixed-port accepts both HTTP and SOCKS connections on the same port. For example, if you set only mixed-port: 7890, enter 127.0.0.1:7890 in a browser or command-line tool, using whichever proxy type the tool supports. Here, 7890 is an example; use the port in your active config.
- Use separate ports: Set
port: 7890andsocks-port: 7891. Ifmixed-portis already listening on 7890, don't also setportto 7890. - Proxy port vs. control port:
external-controller: 127.0.0.1:9090configures the control API, not a proxy listener. Setting your browser's proxy port to 9090 won't route web traffic through Clash. - Listening vs. routing traffic: A successful listener only means the core is waiting for connections. The client's System Proxy toggle changes the system settings; apps that don't use the system proxy need a proxy configured individually or traffic routed through TUN.
For local use only: Keep
allow-lan: false. If an app suddenly can't connect, first check that its configured port matches the port the core is actually listening on. Don't enable LAN access to troubleshoot a local connection.
Mode: How the core chooses an outbound route
mode is a top-level field, aligned with mixed-port. With mode: rule, the core checks rules in order and uses the route specified by the first matching rule. With mode: global, regular proxy traffic uses the global selection instead of being evaluated against these rules one by one. mode: direct sends traffic directly, without a proxy node.
When troubleshooting why a new rule isn't taking effect, first check whether the client is currently in Rule mode. Some clients update the core's running state when you switch modes, so mode: rule on disk doesn't necessarily mean Rule mode is active right now. TUN and mode are not mutually exclusive: TUN routes traffic into the core, while Rule mode decides where those connections go. Enabling Rule mode alone does not capture all traffic from the device.
DNS: Choosing a resolver
dns is a mapping; enable, listen, enhanced-mode, and nameserver all go under it. nameserver lists upstream resolvers, while listen sets the address that accepts DNS queries. They aren't interchangeable. This mihomo example is for testing local DNS resolution and assumes 192.168.1.1 actually provides DNS. If your network uses a different address, replace it with a working resolver.
dns:
enable: true
listen: 127.0.0.1:1053
enhanced-mode: redir-host
nameserver:
- 192.168.1.1
DNS mode and system DNS are separate
In redir-host mode, clients typically receive real IP addresses. In fake-ip mode, the core can return a synthetic address it manages, then use its mapping to find the original domain when a connection is made. If you see a synthetic address, check the current mode before treating it as a resolution problem. Enabling dns.enable and listening on port 1053 won't make the operating system send queries there automatically. In an environment with dig installed, test this listener directly with dig @127.0.0.1 -p 1053 example.com A. A successful response only confirms that this particular query went through it.
In mihomo, nameserver-policy lets you choose different upstream resolvers for specific domains. It determines where a domain is resolved, not which route traffic to that domain takes. If a proxy node's server address is itself a domain, distinguish a failure to resolve the node address from a failure to resolve the destination. Check the logs to see which domain failed, then inspect the relevant setting. For more fields, see the DNS section in Advanced Config.
Proxies and proxy groups: Define routes, then reference them by name
proxies: Connection settings for individual nodes
proxies is a list. Each node entry typically starts with - name:; its type, server, and port fields should align with name. A node's port is the remote server port, not the top-level local listening port. Authentication and transport fields depend on the protocol and core support, so don't copy an entire set of fields from a different protocol. To test direct connections only, set the top-level node list to proxies: [].
proxy-groups: Give rules a stable target
proxy-groups is also a list. With type: select, you can choose a member manually in the client; type: url-test probes the members according to the group settings and selects an outbound. Note that the same field name appears in two places: top-level proxies contains node definitions, while a group's proxies lists names, such as node names or the built-in DIRECT. Rules refer to a group name or another available outbound, not a node's server address. When renaming something, update every reference too—including capitalization and spaces.
After importing a subscription, an empty top-level node list doesn't necessarily mean the config has no nodes. Some subscriptions put nodes in proxy-providers and have proxy groups reference them with use; others generate a complete config. Before editing, check whether the client loads a local copy or a file generated from the subscription, so your changes aren't overwritten. If the client supports overrides, save your edits there and check the merged config to confirm they're active.
Rules: First match wins; the last rule is the fallback
Each item in rules is a rule. A common format is “type, match value, route.” For example, DOMAIN-SUFFIX,example.com,Local Test matches example.com and its subdomains, then sends the connection to the group named “Local Test.” Use DOMAIN for an exact domain match. MATCH takes no match value, so write it as MATCH,DIRECT.
The core checks rules from top to bottom and uses the first match. To route only api.example.com elsewhere, put its DOMAIN rule before DOMAIN-SUFFIX,example.com,…. Put MATCH last; otherwise, rules below it can never match. For connections that provide only a destination IP, don't assume a domain rule will match. Check the connection details to see whether the core identified a domain or an IP.
mixed-port: 7890
allow-lan: false
mode: rule
proxies: []
proxy-groups:
- name: Local Test
type: select
proxies:
- DIRECT
rules:
- DOMAIN-SUFFIX,example.com,Local Test
- MATCH,DIRECT
This config has no remote nodes, and both routes ultimately connect directly. Use it to check YAML structure and rule references, not to test node latency. After loading it, run curl --proxy http://127.0.0.1:7890 https://example.com/, then check the client's Connections page or the logs. The expected result is a domain-suffix rule match, the “Local Test” group, and its sole member, DIRECT. Simply checking whether the page opens won't tell you whether the specified rule matched or the final fallback was used.
Indentation and load errors: Check three layers
- Start with YAML syntax. Use spaces consistently—two per level is a good convention—and never mix in tabs. Top-level fields start at the beginning of the line. In the example, the group's
typealigns withname; its- DIRECTmember is indented under the group'sproxies. Quote an entire value if it contains characters that YAML could interpret as syntax, such as a colon followed by a space. Don't add a list by repeating the same top-level key; add new items to the existing list instead. - Next, check core validation. In an environment with the mihomo command-line core installed and this file in the current directory, run
mihomo -t -f ./config.yamlto validate it. If the config depends on databases or rule files at relative paths, use the same core data directory as the actual runtime when testing; specify it with-d. Valid syntax doesn't guarantee the config can load: a missing policy group referenced by a rule, a field with the wrong type, or an unsupported protocol must each be addressed based on the first specific error. - Finally, check runtime behavior. Successful validation doesn't mean traffic is reaching the core. After reloading, if the logs report that an address is already in use, check whether an old instance is still listening on 7890. If the port is working but no rule matches, check the app's proxy address, the active mode, and the destination and matched rule in the connection details—in that order. Change one thing at a time, then reload and create a new test connection to avoid misleading results from a reused persistent connection.
Whether dns appears before or after rules at the top level usually doesn't affect routing. What matters is which section each field belongs to, whether names are referenced correctly, and the order of entries within rules. Checking these three points is often more useful than comparing your config line by line with someone else's.