安装Nginx
bash
sudo apt update
sudo apt install nginx
关闭防火墙
bash
sudo ufw allow 'Nginx HTTP'
修改nginx配置
bash
cd /etc/nginx/conf.d
vi nginx.conf
在http配置中添加(/your path/为需要下载的文件路径)
bash
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服务使其生效
bash
systemctl restart nginx.service
如遇如下错误
bash
systemctl: command not found
可用以下方法重启
bash
sudo service nginx restart
至此配置结束,可访问ip/your path/文件名 浏览器强制下载静态资源