配置 Nginx 打开链接自动下载指定类型的文件
当点击连接时,在浏览器中自动下载文件,可以在 Nginx 中添加如下配置:
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
if ($request_filename ~* ^.*?\.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx)$){
add_header Content-Disposition "attachment;";
}
}
}
主要是在请求头中添加 Content-Disposition,即:
add_header Content-Disposition "attachment;";