【问题标题】:Serve PDF file by location & query parameter按位置和查询参数提供 PDF 文件
【发布时间】:2021-07-31 20:50:49
【问题描述】:
如何在以下地址提供 PDF 文件:
127.0.0.1/getMeThatFile/willYou?name=jane
存储在某个位置:
/usr/share/nginx/thatFile.pdf
我尝试关注Serve pdf file by location in nginx,但未能成功:
server {
location /getMeThatFile/willYou/ {
alias /usr/share/nginx/;
return 302 thatFile.pdf;
}
}
【问题讨论】:
标签:
nginx
nginx-config
nginx-location
【解决方案1】:
/getMeThatFile/willYou 和 /getMeThatFile/willYou/ 是不同的 URI。您的问题表明该请求将使用第一个,但您的解决方案与第二个匹配。
使用location /getMeThatFile/willYou 匹配两者,或使用location = /getMeThatFile/willYou 仅匹配第一个。详情请见this document。
要返回单个文件,请使用 root 和 try_files。例如:
location = /getMeThatFile/willYou {
root /usr/share/nginx/;
try_files /thatFile.pdf =404;
}
假设thatFile.pdf 始终存在,则永远不会到达=404,但这是必要的,因为try_files 至少需要两个参数。详情请见this document。