ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

Ubuntu环境下使用nginx实现强制下载静态资源

Ubuntu环境下使用nginx实现强制下载静态资源

安装Nginx

sudo apt update
sudo apt install nginx

关闭防火墙

sudo ufw allow 'Nginx HTTP'

修改nginx配置

cd /etc/nginx/conf.d
vi nginx.conf

在http配置中添加(/your path/为需要下载的文件路径)

server {listen 80;server_name localhost;location / {root   /your path/;if ($request_filename ~* ^.*?\.(jpg|jpeg|png|gif|txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx)$){add_header Content-Disposition "attachment;";}}
}

重启nginx服务使其生效

systemctl restart nginx.service

如遇如下错误

systemctl: command not found

可用以下方法重启

sudo service nginx restart

至此配置结束,可访问ip/your path/文件名 浏览器强制下载静态资源

返回列表