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.
nginx only allows requests like the one below to start without leading
/
:This can be seen in nginx v1.20.1
ngx_http_parse.c
source code file. The parsing of request line after HTTP method is done starting from line 280 in the file.It does not support arbitrary text strings after
GET
. You cannot modify this behavior with anyrewrite
rules, since the request validation is done much earlier thanrewrite
rule processing.I am also very confident that nginx implementation is according to the HTTP RFC. Therefore I think you have misunderstood something here.