Nginx isn't passing on the last forward slash of an arg to a script

Viet Le asked:

Have an issue where nginx isn’t passing on the last forward slash of an arg to a script


location / {​​
rewrite "^/([a-zA-Z0-9]+.*)/([a-zA-Z0-9]+.*)$"
/test2.php?t=$1&file=$2 last;

test2.php is just simply getting and echoing $t and $file

Actual outcome: test_t/company/Default/icon_categoryHotel.png

Expected outcome: test_t/company/Default/icon_category/Hotel.png

My answer:

You say that you want the first path component, without any slashes, to be in the first variable, and everything after it, including slashes, in the second variable.

Let’s see how your current regex looks, at regex101:


When we give it the string to match: /test_t/company/Default/icon_category/Hotel.png the first group is test_t/company/Default/icon_category and the second group is Hotel.png.

The reason for this is that the first group is followed by a literal character /, and the .* match quantifier is greedy, meaning in this case it will keep going until the last / in the string after the capture group. This is why you see nearly the entire path in the first variable.

To fix that, you need to make the quantifier lazy, so that it stops matching at the first occurrence of whatever comes after it in the regex. To accomplish this we change .* to .*?.

Now on regex101 you can see the first capture group is test_t as you wanted, but the second capture group does not include the forward slash. This is because the literal / is between the two capture groups, thus it won’t be included in either of them.

Because you want it to be included in the second variable, we will move it inside the second capture group, so /( becomes (/. Here is how the third iteration looks on regex101.


This results in the first variable being test_t and the second being /company/Default/icon_category/Hotel.png.

If you want to make further changes, you can try them out on regex101 yourself to see the results instantly. This way you can learn how everything works before you make any changes to your live configuration.

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.