Proxy with rewrite

I want to rewrite all other not proxy path.

But I get Error during parsing: Wrong argument count or unexpected line ending after '/mysql'

How can I do ?

Note a and b is docker service, I’m using caddy in docker.

xxx.com, www.xxx.com {
    gzip
    header / Strict-Transport-Security "max-age=31536000;"
    proxy /api a:5000 {
        transparent
    }
    proxy /mysql b:8080 {
        transparent
    }
    rewrite {
        if {path} not /api and {path} not /mysql
        to {path} /
    }
}

Hi @GreatFireWall, welcome to the Caddy community.

You can’t use a full chain of logic on a single if subdirective. The syntax is if a cond b, each instance must take three arguments.

Instead, specify multiple if subdirectives.

  • if specifies a rewrite condition. Multiple ifs are AND-ed together. a and b are any string and may use request placeholders. cond is the condition, with possible values explained below.
  • if_op specifies how the ifs are evaluated; the default is and .

https://caddyserver.com/docs/rewrite

Thanks your guide.
And can you show me an example how to use if_op if it’s or ?

The syntax is if_op [and|or].

The rest is the same - use multiple if subdirectives. With if_op or set, the rewrite can be executed if any of the conditionals match, instead of all of the conditionals having to match.

rewrite {
    if {path} not /api 
    and 
    if {path} not /mysql
    to {path} /
}

Is this right ?

I’m afraid not. and is not a subdirective of rewrite. The rest looks good.

The only subdirectives you can use are regexp, ext, if, if_op, and to. Give the docs another read, specifically the syntax section: https://caddyserver.com/docs/rewrite

You don’t specify between each if statement whether they’re grouped AND or OR. You simply specify all the conditions, then you can optionally specify how all of them are grouped.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.