alibaba/nacos · error · IllegalArgumentException

Illegal url path expression

Error message

Illegal url path expression

What it means

Thrown by ValidatorUtils.checkContextPath() when the configured context path contains two or more consecutive forward slashes (matched by the regex (\/){2,}, i.e. the pattern (\/)\1+). A null context path is allowed (early return), but any path with '//' triggers this rejection. This is called from checkInitParam() during NacosClient/NamingService initialization.

Source

Thrown at client/src/main/java/com/alibaba/nacos/client/utils/ValidatorUtils.java:50

    
    private static final Pattern CONTEXT_PATH_MATCH = Pattern.compile("(\\/)\\1+");
    
    public static void checkInitParam(NacosClientProperties properties) throws NacosException {
        checkContextPath(properties.getProperty(PropertyKeyConst.CONTEXT_PATH));
    }
    
    /**
     * Check context path.
     *
     * @param contextPath context path
     */
    public static void checkContextPath(String contextPath) {
        if (contextPath == null) {
            return;
        }
        Matcher matcher = CONTEXT_PATH_MATCH.matcher(contextPath);
        if (matcher.find()) {
            throw new IllegalArgumentException("Illegal url path expression");
        }
    }
    
}

View on GitHub (pinned to 9b989acdf1)

Solutions

  1. Inspect the value of PropertyKeyConst.CONTEXT_PATH (property key 'contextPath') in your Nacos client Properties.
  2. Remove duplicate consecutive slashes — a single leading slash is sufficient (e.g. '/nacos').
  3. If building the path programmatically, normalize it with a utility that collapses runs of '/' before passing it to the client builder.

Example fix

// before
properties.setProperty(PropertyKeyConst.CONTEXT_PATH, "//nacos");

// after
properties.setProperty(PropertyKeyConst.CONTEXT_PATH, "/nacos");
Defensive patterns

Strategy: validation

Validate before calling

String contextPath = properties.getProperty(PropertyKeyConst.CONTEXT_PATH);
if (contextPath != null && contextPath.contains("//")) {
    throw new IllegalArgumentException("contextPath must not contain consecutive slashes: " + contextPath);
}
ValidatorUtils.checkContextPath(contextPath);

Try / catch

try {
    ValidatorUtils.checkInitParam(properties);
} catch (IllegalArgumentException e) {
    // log and provide a corrected contextPath
    String fixed = properties.getProperty(PropertyKeyConst.CONTEXT_PATH).replaceAll("/+", "/");
    properties.setProperty(PropertyKeyConst.CONTEXT_PATH, fixed);
}

Prevention

When it happens

Trigger: Setting PropertyKeyConst.CONTEXT_PATH to a value like '/nacos//' or '//nacos' or '/a//b'. The regex (\/)\1+ matches any run of 2+ slashes.

Common situations: Users prepend an extra slash when the server is behind a reverse proxy (e.g. CONTEXT_PATH='//nacos'); copy-paste from URLs where a trailing slash is doubled; misconfigured Spring property concatenation that joins two slash-terminated segments.

Related errors


AI-assisted analysis of alibaba/nacos@9b989acdf1 (2026-08-14). Data as JSON: /api/errors/7ade63677fbad7df. Report an issue: GitHub.