[英]nginx file rewrite using multi-level regular expression
I'm developing a file server using nginx. 我正在使用nginx开发文件服务器。 For accessing the server file I need to append a string after each directory.
为了访问服务器文件,我需要在每个目录后附加一个字符串。
Example: My URL is 示例:我的网址是
http://desk09/1.2/junior/kal12/pnr.doc
I need to change this to local file named 我需要将其更改为名为
../junior@1.2/kal12@1.2/pnr.doc
Can I do it using nginx configuration file alone something like below: 我可以单独使用nginx配置文件来执行以下操作吗:
location ~ "^/([1-9]{1}\.[0-9]{1}/(.*)" {
root /usr/home/fs/......
}
I change my answer to make it work recursive: 我更改答案以使其递归工作:
First throw version number from begining and add this to first directory 从头开始抛出版本号,并将其添加到第一个目录
location ~ "^/([0-9]{1}\.[0-9]{1})/([a-zA-Z0-9]+)/(.*\.doc)$" {
rewrite .* /$2@$1/$3;
}
So now url: 所以现在网址:
/1.2/junior/many_sub_dir_here/test.doc -> /junior@1.2/many_sub_dir_here/test.doc
Now location
directive belove will work as recursive location (so You have mark nginx to allow recursive location invocation): 现在
location
指令belove将作为递归位置工作(因此,您已将nginx标记为允许递归位置调用):
location ~ "^(/[a-zA-Z0-9]+@)([0-9]{1}\.[0-9]{1})/(([a-zA-Z0-9]+@[0-9]{1}\.[0-9]{1}/)*)([a-zA-Z0-9]+)/(.*\.doc)$" {
rewrite .* /$1$2/$3$5@$2/$6;
}
For your url it will work as follow 对于您的网址,它将按以下方式工作
/junior@1.2/x1/x2/x3/test.doc --> /junior@1.2/x1@1.2/x2/x3/test.doc
$1 = /junior@
$2 = 1.2
$3 =
$4 =
$5 = x1
$6 = x2/x3/test.doc
/junior@1.2/x1@1.2/x2/x3/test.doc --> /junior@1.2/x1@1.2/x2@1.2/x3/test.doc
$1 = /junior
$2 = 1.2
$3 = x1@1.2/
$4 = x1@1.2/
$5 = x2
$6 = x3/text.doc
/junior@1.2/x1@1.2/x2@1.2/x3/test.doc --> /junior@1.2/x1@1.2/x2@1.2/x3@1.2/test.doc
$1 = /junior@
$2 = 1.2
$3 = x1@1.2/x2@1.2/
$4 = x2@1.2/
$5 = x3
$6 = test.doc
This last url won't anymore matches regex in above location
block so here we comes with final location block like this: 最后一个网址将不再与上面的
location
块中的正则表达式匹配,因此在这里,我们附带了最终的位置块,如下所示:
location ~ ".*(@[0-9]{1}\.[0-9]{1})/[^@/]+\.doc$" {
root root /usr/home/fs;
try_files $uri =404;
}
I hope I made no mistakes in regex and everything will work. 我希望我在正则表达式中没有任何错误,并且一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.