Nginx case-insensitive location with case-sensitive proxy_pass

nginxPROXY

We've got several locations with reverse proxies setup. Our current config up and running is:

upstream backends {
    server Backend1;
    server Backend2;
}

server {
    ... other server config stuff here ...

    location /App1 {
        ... proxy header stuff ...

        proxy_pass http://backends/App1;
     }
}

Now what we're trying to do is set it up such that the case-sensitive backends (in this case Backend1/2 http://backends/App1) can be case insensitive to the public such that you can access http://example.com/app1 OR http://example.com/App1 (or any variation of)

Our new config that we're trying to get to work is something like:

location ~* /App1 {
    proxy_pass http://backends/App1;
}

But no dice when we reload the config. We're seeing the error:

"proxy_pass" cannot have URI part in location given by regular
expression, or inside named location, or inside "if" statement, or
inside "limit_except"…

Any thoughts/insight on how we can accomplish this?

Best Answer

The implicit rewrite performed by proxy_pass will not work in the presence of a regular expression location. See this document for more.

However, you can use a rewrite ... break to perform the same function.

location ~* /App1(?<stuff>.*)$ {
    rewrite ^ /App1$stuff break;
    proxy_pass http://backends;
}

The regular expression is extended with a named capture to grab the remainder of the URI, then the rewrite statement builds a new URI for passing upstream.

See this document for details.