学而实习之 不亦乐乎

配置 Nginx 打开链接自动下载指定类型的文件

2023-03-08 07:55:35

当点击连接时,在浏览器中自动下载文件,可以在 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;";