GrepBoard/Manuals
605 manuals ·

Fix nginx proxy_pass cannot have URI part in regex location

·Tested on nginx 1.30.6 (Docker) · 2026-10·Test scope ↓·7 steps

nginx rejects proxy_pass with a path (even a trailing /) in a regex location or if block. Drop the path and use rewrite … break, or use a prefix location.

output
nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /etc/nginx/conf.d/default.conf:11

A proxy_pass inside a regex location (~, ~*), a named location (@), an if block or a limit_except block has a path after the address. A single trailing / counts as a path. In those places nginx cannot tell which part of the request URI the path should replace, so it refuses the configuration. Remove the path from proxy_pass and change the URI with rewrite … break, or use a prefix location if the regex is not really needed.

[1] Open the line nginx -t points to. The error above came from line 11 (proxy_pass) of this configuration:

/etc/nginx/conf.d/default.conf
    location ~ ^/api/(.*)$ {        proxy_pass http://127.0.0.1:8080/;    }

The latest nginx source adds or inside predicate location to the message. A slightly longer wording is the same check.

[2] Pick a fix. All three were tested with the same request, /api/users/7?fields=name. The test backend echoes the URI it received as backend got: ….

SituationFixWhat the backend receives
Only the /api/ prefix needs to goPrefix location /api/ + proxy_pass …/;/users/7?fields=name
The regex is requiredproxy_pass without a path + rewrite … break/users/7?fields=name
The path is built from variablesproxy_pass …/$1$is_args$args;/users/7?fields=name (encoding issue, see [5])

Before changing anything, copy the current file outside conf.d. Changing the location type or a rewrite can change the URI sent to the backend even when nginx -t passes, so keep this copy to roll back in [7].

console
$ cp -a /etc/nginx/conf.d/default.conf /root/default.conf.bak

[3] If the regex is not needed, use a prefix location. With a path in proxy_pass, a prefix location replaces the part of the URI that matched the location with that path.

/etc/nginx/conf.d/default.conf
    location /api/ {        proxy_pass http://127.0.0.1:8080/;    }
console
$ curl -s 'http://127.0.0.1/api/users/7?fields=name'
output
backend got: /users/7?fields=name

[4] To keep the regex, remove the path from proxy_pass and rewrite the URI with rewrite … break. The nginx docs say to specify proxy_pass without a URI here, and that a URI changed by rewrite … break is passed to the backend in full.

/etc/nginx/conf.d/default.conf
    location ~ ^/api/(.*)$ {        rewrite ^/api/(.*)$ /$1 break;        proxy_pass http://127.0.0.1:8080;    }
console
$ curl -s 'http://127.0.0.1/api/users/7?fields=name'
output
backend got: /users/7?fields=name

The query string came along, and a path containing %20 (/api/files/a%20b.txt) reached the backend still encoded as /files/a%20b.txt.

[5] Building the path from variables passes nginx -t but goes wrong in two places. Per the docs, a proxy_pass with variables sends that value as is.

/etc/nginx/conf.d/default.conf
    location ~ ^/api/(.*)$ {        proxy_pass http://127.0.0.1:8080/$1;    }
console
$ curl -s 'http://127.0.0.1/api/users/7?fields=name'
output
backend got: /users/7

The query string is gone. Appending $is_args$args as below brings it back as /users/7?fields=name.

/etc/nginx/conf.d/default.conf
        proxy_pass http://127.0.0.1:8080/$1$is_args$args;

With that setting, though, a path containing %20 came back as 400.

console
$ curl -s 'http://127.0.0.1/api/files/a%20b.txt'
output
<html><head><title>400 Bad Request</title></head>…

The nginx docs say a location is matched against the normalized URI, after %XX is decoded. So $1 holds the decoded a b.txt, which appears to have been put into the request line without re-encoding. If paths can contain spaces, non-ASCII characters or %2F, use the rewrite from [4]. A hostname inside a variable also needs a resolver (see no resolver defined).

[6] If the error came from inside if, move the condition into a map and choose the path with rewrite. This replaces a setup that sent requests with an X-Beta header under /beta/:

/etc/nginx/conf.d/default.conf
map $http_x_beta $api_prefix {    default "";    ~.      /beta;} server {    …    location /api/ {        rewrite ^/api/(?<rest>.*)$ $api_prefix/$rest break;        proxy_pass http://127.0.0.1:8080;    }}
console
$ curl -s -H 'X-Beta: 1' 'http://127.0.0.1/api/users/7?fields=name'
output
backend got: /beta/users/7?fields=name

The named capture (?<rest>) is deliberate. The first attempt used (.*) and $1, and with the header present the backend got /beta/?fields=name. Evaluating the regex in the map appears to overwrite the numbered capture; switching to a named capture fixed it.

[7] Once nginx -t passes, reload and send a real request, then check the URI in the backend's access log. nginx -t only checks syntax; only a real request shows whether the backend gets the right URI. If 404s or 400s rise after the reload and the cause is not obvious, restore the copy from [2] first.

console
$ cp -a /root/default.conf.bak /etc/nginx/conf.d/default.conf$ nginx -t && nginx -s reload

If the backend returns 404 or 502, continue with reading nginx 502 Bad Gateway logs.

Analyze your Nginx log in the browser ↗ — nothing is uploaded.

Check it with the Regex tester (JS · grep -E · grep -P) tool ↗ — calculated in your browser.

#nginx#proxy_pass#location#rewrite#regex