Nginx rewrite rule omit forward slash (/) when its first in encoded url

  nginx, pcre, php, regex

i am trying use Nginx rewrite.
somehow its deletes the / key when its first in string

nginx/1.21.3

rewrite rule:

#nginx not relevant conf here
location / {
    rewrite ^(.*)data/([0-9]+)/(.+)?$ $1processor.php?key=$2&data=$3 last;
}
#nginx not relevant conf here

when i use this rewrite rule for any url i tested it was okay. when i tried url like the example below it somehow omitted the / in the beginning.

https://example.com/data/9/%2F*-%2B.%60!%40%23%24%25%5E%26*()_%2B%60-%3D%5B%5D%3B%27%5C%2C.%2F%7B%7D%3A%22%7C%3C%3E%3F

when i reloaded nginx with notices and rewrite_log=on; i got the output:

2021/09/25 13:08:29 [notice] 528#528: *11710 "^(.*)data/([0-9]+)/(.+)?$" matches "/data/199/*-+.`[email protected]#$%^&*()_+`-=[];',./{}:"|<>?", client: 192.168.255.107, server: localhost, request: "GET /data/199/%2F%2A-%2B.%60%21%40%23%24%25%5E%26%2A%28%29_%2B%60-%3D%5B%5D%3B%27%5C%2C.%2F%7B%7D%3A%22%7C%3C%3E%3F HTTP/2.0", host: "example.com", referrer: "https://example.com/"

php (8.0.10) $_GET["data"] output is (as you can see without / thus not exact math):

*-+.`[email protected]#$%^&*()_+`-=[];',./{}:"|<>?

how can i solve it?

Source: Ask PHP

LEAVE A COMMENT