How to Block a URL Path in Nginx or Caddy
Block a URL path in nginx or Caddy to virtually patch a vulnerable endpoint: location matching, deny versus return 403, and how to verify the block.
You block a URL path in nginx by adding a location block that matches the path and returns a 403 before the request reaches the application. In Caddy you add a respond directive with a path matcher and a 403 status. Both take minutes and act as a virtual patch: the vulnerable endpoint stays in the product, but nothing on the network can reach it. This is the mitigation X41 D-Sec and BerriAI both recommended for the LiteLLM guardrail endpoint behind CVE-2026-40217, and the same procedure applies to any debug, test or admin route you do not want exposed.
When blocking a path is the right move
A reverse proxy block fits three situations: a vendor has disclosed a flaw in a specific endpoint and the patch is not yet deployed; a product ships an admin or debug route you never use; or you want a second control in front of a feature that executes operator-supplied code. It is not a substitute for patching, because anything that reaches the application without passing through the proxy bypasses it. The LiteLLM CVE-2026-40217 sandbox escape is a textbook case: a single path, admin-only, rarely used, exploited in the wild, with a patch that some teams cannot deploy the same day.
How does nginx match a location?
Nginx first checks prefix locations and remembers the one with the longest matching prefix, then checks regular expression locations in file order and uses the first match. A location declared with the equals modifier matches the URI exactly and ends the search immediately, which makes it the right choice for blocking one known path.
Two details from the nginx documentation matter for a security block. Matching runs against a normalized URI: percent-encoded characters are decoded, dot segments are resolved, and runs of slashes are collapsed, so an attacker cannot slip past the block with encoding tricks on the path itself. And a prefix location without a modifier matches everything under that prefix, so location /guardrails/test_custom_code also matches longer paths that start with that string.
Block the path in nginx
X41's advisory gives the one-line form, which uses a prefix location and denies every client:
location /guardrails/test_custom_code { deny all; return 403; }
An exact-match version that only blocks the precise path looks like this:
location = /guardrails/test_custom_code {
deny all;
return 403;
}
deny all comes from the access module and refuses every client address; return 403 comes from the rewrite module and stops processing with the status code you choose. Either alone is enough. Using both is harmless and makes the intent obvious to the next person who reads the file. Return 444 if you would rather close the connection without any response; the nginx documentation lists it as a non-standard code for exactly that.
Put the block inside the same server block that proxies the application, above or below the existing location that forwards traffic. Because exact matches win over prefix matches, order does not change the result, but keeping security blocks together at the top of the server block makes them easier to audit.
If you need to block only one HTTP method, nginx's documented pattern is a conditional inside the location:
if ($request_method = POST) { return 405; }
A method-specific block has to live inside a location that still forwards the other methods to the application, which means repeating your proxy settings in that location. For an endpoint with no legitimate GET use, block every method instead and keep the configuration short.
Block the path in Caddy
Caddy's respond directive writes a fixed response and accepts a path matcher. The Caddy documentation's example for denying a path is:
respond /secret/* "Access denied" 403 {
close
}
For a single path, drop the wildcard so the matcher is exact:
respond /guardrails/test_custom_code "Access denied" 403 {
close
}
Place it in the site block in front of the reverse_proxy directive. Caddy evaluates directives in a fixed order in which respond runs before reverse_proxy, so the matched request never reaches the upstream. The close option ends the client connection after the response.
How do you verify the block works?
Send a request to the blocked path from outside the proxy host and confirm you receive the 403, then send a normal request to a working path and confirm the application still answers. Repeat from the network segment attackers would use, not only from your workstation.
A minimal test with curl mirrors the shape of a real request:
curl -i -X POST https://gateway.example.com/guardrails/test_custom_code
Check three variants as well: the path with a trailing slash, the path with mixed case, and the path with a percent-encoded character. Nginx normalizes encoding and collapses slashes, but matching is case-sensitive on Linux, so an application that treats paths case-insensitively needs a case-insensitive regex location rather than an exact match. Test the configuration syntax before reloading and reload rather than restart so existing connections survive.
What a reverse proxy block cannot do
- Direct access to the application port. If the application listens on an address other hosts can reach, the proxy is optional for an attacker. Bind the application to localhost or a private interface and add a firewall rule so only the proxy can connect.
- Other hostnames and ports. A block in one server block does not apply to a second virtual host or a second listener that proxies the same backend. Search the whole configuration for every
proxy_passorreverse_proxypointing at the application. - Alternate routes to the same function. CWE-420, the weakness class CISA assigned to the LiteLLM flaw, exists because products expose the same capability through more than one path. Read the vendor advisory for every affected route and block each one.
- Replacing the patch. The block buys time. Schedule the upgrade, and after it is verified decide whether to keep the block. For a test endpoint you never use, keeping it costs nothing. The reasoning is in what a sandbox escape is: any feature that executes submitted code is a repeat target.
Record the change
Log the block in your change system with the CVE, the date, the configuration lines and the removal criteria. When the patch is deployed, the record tells the next engineer why the 403 exists and whether it can go. Pair it with a check of the proxy access logs for hits on the path before the block went in; a hit from an unexpected source is the signal to start incident response rather than close the ticket.