November 8, 2026 · Troubleshooting · About 9 min

Xray Routing Rules: Advanced JSON Configuration Guide

A hands-on guide to Xray-core routing for users who need more than basic client settings. Understand rule evaluation and domainStrategy, configure domain and IP matches in JSON, and trace routing mistakes through logs.

At a glance

This guide is for Xray users who need to route traffic by domain, IP address, port, or inbound tag instead of relying on a client’s basic proxy switch. It explains how Xray evaluates routing rules, what domainStrategy changes, and how to add rules without sending traffic to a nonexistent outbound. You’ll work through a JSON example, validate it with the same Xray binary that runs your configuration, and use logs and controlled tests to locate a routing mistake.

How Xray evaluates routing rules

Xray routing decides which outbound handles a connection. A routing rule can match details such as the requested domain, destination IP, port, protocol, network, or inbound tag. When a rule matches, its outboundTag tells Xray which outbound to use. The tag is a reference, not an outbound definition: a tag named proxy must also exist in the configuration’s outbounds array.

Rule order is significant. Xray checks routing rules in order, and the first matching rule determines the route. Put narrow exceptions before broader rules. For example, a rule for one internal hostname should be above a rule that sends a whole domain category to a proxy. If the broad rule matches first, the later exception will never get a chance to apply.

Inbound requestRule matchingFirst matchOutbound tagDestination

Conditions inside a rule also matter. A rule that includes a domain and an inbound tag is more specific than one that includes only a domain: both conditions must be satisfied for that rule to match. Multiple values within a matcher, such as two domains in the domain array, give Xray alternatives to check. Read the whole rule as one decision, rather than assuming each property creates a separate rule.

Rule matching is separate from DNS configuration, client system-proxy settings, and the connection between Xray and the remote server. A correct rule cannot repair an invalid node, and a working node does not prove that a request reached the outbound you intended. Keep these layers separate when testing.

Understand domainStrategy before adding IP rules

The domainStrategy setting controls when Xray resolves a domain name to an IP address for routing decisions. It is particularly important when a request initially contains a hostname but one of your rules matches an IP range. A domain matcher can work without turning the destination into an IP address; an IP matcher needs an address to compare.

AsIs

Resolution for IP rules
Do not resolve a domain for routing
Domain rules
Match the domain available to Xray
IP destination
Can match a literal destination IP
Useful when
Routing by domain without routing-time DNS lookup

An IP rule will not gain a destination address by resolving a hostname under this strategy.

IPIfNonMatch or IPOnDemand

IPIfNonMatch
Resolve after domain rules fail to match
IPOnDemand
Resolve when an IP rule requires an address
Domain plus IP rules
Can use both forms of destination matching
Check before use
DNS behavior and the intended rule order

Choose a strategy deliberately: resolution can affect which rule matches and when a DNS lookup occurs.

For a rule set that first tries domain matches and then needs IP rules as a fallback, IPIfNonMatch is often a clear starting point. It does not mean that every request is immediately resolved before any domain rule is checked. With IPOnDemand, an IP rule can trigger resolution when that rule is reached. In either case, a DNS result depends on the DNS behavior and environment in which the core is running; do not assume that it will always be the same address you see in a browser.

Test the strategy with both a domain request and a literal IP request. If domain-based rules behave correctly but an IP-range rule never matches for hostname traffic, check the strategy and DNS path before rewriting the rule. Also check whether the application is using the Xray inbound at all: traffic that bypasses the inbound cannot be classified by its routing rules.

Build a routing example with explicit tags

The following is a routing fragment, not a complete Xray configuration. Merge it into the existing routing object and make sure every referenced outbound tag is defined in outbounds. It uses sample domains and an example private network; replace them with values appropriate for your environment. The data files required by geosite: and geoip: matchers must also be available to the core.

{
  "domainStrategy": "IPIfNonMatch",
  "rules": [
    {
      "type": "field",
      "domain": ["full:portal.internal.example"],
      "outboundTag": "direct"
    },
    {
      "type": "field",
      "domain": ["domain:media.example"],
      "outboundTag": "proxy"
    },
    {
      "type": "field",
      "ip": ["geoip:private", "10.20.0.0/16"],
      "outboundTag": "direct"
    },
    {
      "type": "field",
      "domain": ["geosite:category-ads-all"],
      "outboundTag": "block"
    },
    {
      "type": "field",
      "port": "53",
      "network": "udp",
      "outboundTag": "direct"
    }
  ]
}

This order demonstrates why the rules need to be read from top to bottom. The exact hostname is an exception placed before the broader media.example domain matcher. The IP rule follows the domain rules, so with IPIfNonMatch, a domain that did not match those earlier rules can be resolved and checked against the IP list. The remaining entries illustrate category and port/network matching; move them to the position that reflects your intended policy.

Matcher prefixes are not interchangeable. full:portal.internal.example is for that exact domain, while domain:media.example matches the domain and its subdomains. A keyword: matcher looks for a substring, which can be broader than expected; use it only when that broader match is intentional. A regexp: matcher is useful for carefully defined patterns but is easy to overmatch. When a simple exact or domain matcher expresses the requirement, prefer it over a regular expression.

The example uses direct, proxy, and block as readable labels, not built-in destinations. Check that the configuration defines each one and that the selected outbound has the required behavior. In particular, do not add a rule with outboundTag: "proxy" if the actual outbound is tagged proxy-out. Tag spelling, capitalization, and existence all need to agree.

Conclusion: write the desired exception first

Before adding a broad category or IP rule, write down one request that must be an exception and place its narrow matcher above the broad rule. Then test both that request and one ordinary request from the broader group.

Edit and validate without losing the working configuration

Clients such as v2rayN may generate a temporary configuration from selected nodes and client settings. Editing that generated file directly can be fragile: a later node switch, subscription update, or restart may replace it. First identify whether you are editing a persistent custom configuration or a file generated for the current run. Keep a copy of the known-good configuration and change one routing behavior at a time.

  1. Back up the config

    Copy the JSON file you intend to change and record its original path. If the client generates the runtime configuration, locate its custom routing or routing-template settings instead of assuming that a temporary file will persist.

  2. Confirm the tags

    In the same configuration, compare each rule’s outboundTag with the tags in outbounds. Check spelling, capitalization, and whether the destination outbound is actually the one you intend to use.

  3. Change one rule

    Add one narrow rule or change one matcher, then save the JSON. Avoid changing DNS settings, node parameters, inbound ports, and routing order together; that makes a successful or failed test difficult to explain.

  4. Test with Xray

    Use the same Xray executable that the client launches and validate the edited file before reconnecting. For a standalone configuration, run xray run -test -config config.json from a terminal in the directory containing the file.

  5. Retest each path

    Reconnect using the intended node, make one request for the exception and one for the ordinary case, then check the result and the logs. If the client generates a different runtime file, validate that effective configuration instead of only testing your template.

The validation command checks whether Xray can load the file; it does not prove that a particular application’s traffic reaches the expected outbound. Some client-managed configurations also need client-specific settings or an environment that the standalone command does not reproduce. Treat a successful test as a syntax and configuration-load check, then verify routing through the client that will actually carry the traffic.

For example, a local SOCKS inbound may listen on 127.0.0.1:10808, while a local HTTP inbound may use 127.0.0.1:10809 in a common desktop setup. These are example ports, not universal defaults. Check the active inbound configuration and the client’s current settings before using either value in a test. A request sent to the wrong local port never reaches the rule set you are trying to debug.

Trace a wrong route with logs and controlled tests

Start with one reproducible request, not a large batch of unrelated traffic. Record the time, hostname or literal IP, application, selected node, and whether the request used a SOCKS, HTTP, or TUN path. Then inspect the client’s Xray log around that time. Log wording and detail vary with the core build and client, so use the first relevant error and the available connection information rather than expecting every interface to display an identical route trace.

Why does the domain rule match but the IP rule not?

Check domainStrategy first. With AsIs, Xray does not resolve a hostname just to test an IP rule. If resolution is intended, test IPIfNonMatch or IPOnDemand and confirm the core can use its configured DNS path.

Why does my exception still go through the proxy?

Move the exception above the broader matching rule, then check whether the request has the hostname you expected. A rule matching an IP, a different hostname, or a different inbound tag will not match merely because the browser displays a familiar site name.

Why did Xray reject the edited JSON?

Run the test command against the file you edited. Look for missing commas, unmatched braces, duplicate object keys, misspelled properties, and tags that do not exist. If a client generated the runtime file, test the effective file as well as the persistent template.

If the JSON loads but the observed route is wrong, check these items in order:

Temporarily increasing the core’s log verbosity can expose more detail, but avoid leaving verbose logging enabled indefinitely: it can produce large files and may record connection metadata. Restore the previous log level after the test. Also distinguish routing evidence from connectivity evidence: a DNS error, refused connection, or unreachable server can occur after a rule selected the intended outbound.

Download v2rayNChoose your OS and architecture