I encountered a path matching issue while configuring Nginx to internally forward requests. When the path /api
does not have a trailing slash, the subrequest is successfully handled by the verify
controller and returns a response, but the success
controller does not receive any requests, resulting in a 404 error in the browser. Adding a trailing slash /api/
resolves the issue. Here is the relevant configuration:
server {
listen 80;
server_name localhost;
location /api {
auth_request /auth;
proxy_pass http://localhost:8080/success;
}
location = /auth {
internal;
set $query '';
if ($request_uri ~* "[^?]+?(.*)$") {
set $query $1;
}
proxy_pass http://localhost:8080/verify?$query;
proxy_pass_request_body off;
proxy_set_header Content-Type "";
}
}
The backend controller is running correctly and can handle requests:
@RestController
@SpringBootApplication
public class Application {
@GetMapping("/verify")
public ResponseEntity<String> verify(String token) {
// Verification logic
}
@GetMapping("/success")
public String success() {
return "success";
}
}
Question:
Why does adding a trailing slash /
to the path /api
affect the path matching? Is this behavior based on general Nginx syntax rules, or is it a specific quirk of the ngx_http_auth_request_module
?
Thank you for your help!