I have an Nginx proxy in front my application. I recently learned that, HTTP requests can be made without a leading slash also.
For Example:
POST acme/_search HTTP/1.1
In cases like this, Nginx always returns 400 Bad Request
. I would like to handle this case by either modifying the request_uri or rewriting the request path with a leading slash.
So that the request_uri acme/_search
will be rewritten as /acme/_search
. Any requests with the leading slash are working fine and returns 200
.
I've tried handling it using a rewrite statement in the server block like below:
server {
listen 9000;
if ($request_uri ~* ^[^\/]) {
rewrite ^(.*)$ /$1 break;
}
location / {
proxy_pass http://upstream_http;
allow all;
}
}
^ What I'm trying to do in the above block is, Check if the request_uri
doesn't start with a leading slash & if yes, rewrite the path with a leading slash.
But this configuration is not working. I still get the 400 Bad Request
message logged & returned by Nginx.
I'm using to telnet to make the example request:
telnet localhost 9000
POST acme/_search HTTP/1.1
Would like to know how these kind of HTTP requests without a leading slash can be handled using Nginx as a proxy.