NGINX: check whether $remote_user equals to the first part of the location
It should be possible and you can also define a name of a variable when matching with regex.
Here in the code fragment below, I assign the result of the regex to the variable $username. Then I can check it and if matching the $remote_user (be sure that this one is correctly set, in case just debug it as I did) then I added an extra header to the response so you can see it with the Firebug network console
or just by using the curl
command .
Have a look to see if this one helps you.
location / { auth_basic "ElasticSearch"; auth_basic_user_file /etc/nginx/search_passwords; location ~ /(?<username>([^/]*))/ { # capture and store in the username variable if ($remote_user = $username ) { add_header 'matched' 'true $username'; break; } }}
See also this How to I get variables from location in nginx? for more info.