我正在.pdf使用 nginx Web 服务器提供文件。并.pdf使用重写规则设置文件的文件名,具体取决于title args.
我想要做的是,example.com在文件名中添加我的域名
1. 如果titlevar 包含example.com在其中,则使用titlevar 数据作为文件名。
例如
URL : http://example.com/pdf/0D2E8C9DC2F732A6ACC6150811515F697E3C376C.pdf?title=[example.com]ebook
Filename : [example.com]ebook.pdf
Run Code Online (Sandbox Code Playgroud)
2. 如果titlevar 不包含example.com在其中,则将文件名设为[example.com]title var.pdf
例如
URL : http://example.com/pdf/0D2E8C9DC2F732A6ACC6150811515F697E3C376C.pdf?title=ebook
Filename : [example.com]ebook.pdf
Run Code Online (Sandbox Code Playgroud)
3. 如果没有设置标题,则使用文件名作为[example.com]hash.pdf文件名。
例如
URL : http://example.com/pdf/0D2E8C9DC2F732A6ACC6150811515F697E3C376C.pdf
Filename : [example.com]0D2E8C9DC2F732A6ACC6150811515F697E3C376C.pdf
Run Code Online (Sandbox Code Playgroud)
我怎样才能实现这样的目标。?
我目前的配置是这样的
#pdf download block
location ~* /pdf/([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F]+)\.pdf$ {
#if title is not set then use hash.pdf as filename
if ( $arg_title = '' ) {
add_header Content-Disposition 'attachment; filename="[example.com]$1$2$3$4$5$6.pdf"';
}
add_header Content-Disposition 'attachment; filename="[example.com]$arg_title.pdf"';
alias /var/www/example.com/pdf/$1/$2/$3/$4/$5/$1$2$3$4$5$6.pdf;
expires 1y;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码只有在根本没有设置 title var 的情况下才能正常工作,但是如果我设置了 title var 因为[example.com]ebook.pdf它再次添加了域名并且最终文件名变为
[example.com][example.com]ebook.pdf
Run Code Online (Sandbox Code Playgroud)
据我所知,我们可以假设如果变量[example.com]的开头有一个子字符串$arg_title,我们可以安全地将其剪掉。所以这里是你需要的配置:
location ~* /pdf/([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F]+)\.pdf$ {
#First of all we should put pdf path from the request into a variable,
#since $1,$2,$n would contain other data later:
set $hashpath $1/$2/$3/$4/$5/$1$2$3$4$5$6;
#Same applies to hash title:
set $hashtitle $1$2$3$4$5$6;
#Now we try to get the title argument without getting [example.com] in the beginning:
if ( $arg_title ~ "^(\[example\.com\])?(.*)$")
{
#This way if title contained [example.com], it would go to $1 variable, while $2 has our new cleaned title:
set $pdftitle $2;
}
#now we check if title was empty:
if ( $arg_title = '' ) {
#and assign our title that $hashtitle variable we got from the request earlier:
set $pdftitle $hashtitle;
}
#also we check if title has only [example.com] without anything else:
if ( $arg_title = '[example.com]' ) {
set $pdftitle $hashtitle;
}
#After that we can safely put [example.com] in front of our filename, without worrying that there would be a double:
add_header Content-Disposition 'attachment; filename="[example.com]$pdftitle.pdf"';
alias /var/www/example.com/pdf/$hashpath.pdf;
expires 1y;
}
Run Code Online (Sandbox Code Playgroud)
在此处与 nginx 1.9.12 一起工作正常:
实际文件位于此处
| 归档时间: |
|
| 查看次数: |
5126 次 |
| 最近记录: |