前端项目有个需求,在匹配路径诸如:https://www.xxxx.com/dev/sample/0.1/3232/imt/的时候访问前端vue项目
其中标黄的数字部分,是可以变化的,规则是:只要满足4四位以上的数字就可以正常显示页面。
这其中遇到两个问题:
1,匹配的路径比较长,如果使用nginx 的location root指定访问路径,真实路径匹配问题比较麻烦,需要多次rewrite才可以,
使用alias的时候,普通匹配没有问题,如果使用alias的正则匹配,会出现多次重定向导致访问出错。
2,在使用正则的时候,location 匹配时候出现 '{}' 时候,nginx会报错,这应该是和后面的指令块的“大括号”有歧义。
经过查询文档,对于对一个问题发现,alias使用正则匹配的时候,需要正则捕获,并且捕获到的分组要应用到alias上。
nginx中如下配置解决问题:
location ~* "^/dev/sample/0.1/\d{4,}/imt/(.*)$" {
alias /webdata/webdir/website/wp-content/uploads/2019/11/$1;
index index.html index.htm index.php;
}
| alias path; |
| — |
| location |
Defines a replacement for the specified location. For example, with the following configuration
location /i/ {
alias /data/w3/images/;
}
on request of “/i/top.gif”, the file /data/w3/images/top.gif will be sent.
The path value can contain variables, except $document_root and $realpath_root.
If alias is used inside a location defined with a regular expression then such regular expression should contain captures and alias should refer to these captures (0.7.40), for example:
location ~ ^/users/(.+\.(?:gif|jpe?g|png))$ {
alias /data/w3/images/$1;
}
When location matches the last part of the directive’s value:
location /images/ {
alias /data/w3/images/;
}
it is better to use the root directive instead:
location /images/ {
root /data/w3;
}如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!