I have a question regarding parameter after root url:
URL is : https://test/?version=12.1.0
What I want there is a parameter starting with version=numner.number.number To serve a folder under the root dir "v12.1.0"
location ~ ^\?version=v(\d \.\d \.\d ) {
root /var/www/test/htdocs/v$2;
}
Is that possible? I've been trying wiith try files and root, but I cannot manage to make it work. Should I use map in this situation?
CodePudding user response:
Nginx normalises the URI before processing it. The part of the URI before the ?
is available as the $uri
variable, and the part of the URI after the ?
is available as the $args
variable.
The location
and rewrite
directives operate on the value of the $uri
variable, so your present approach will not work, as the query string has already been remove.
The $args
variable is further split into other individual variables, so that the value of your version=
parameter is available as $arg_version
.
One solution is to use a map
block to decode $arg_version
and use the mapped variable in a try_files
statement as required.
For example:
map $arg_version $mypath {
~*^(v\d \.\d \.\d )$ $1/;
default "";
}
server {
...
root /var/www/test/htdocs;
location ... {
try_files $mypath$uri =404;
}
}
We could use $arg_version
as is, but the map
ensures that the path prefix is sanitised and contains the necessary /
separator.
Note that the map
block lives outside of the server
block. See this document for details.
Depending on your specific requirements, you could also use the mapped variable directly with root
. For example:
root /var/www/test/htdocs/$mypath;