ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

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

2026/9/30 17:32:32 拓冰建站 浏览量
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/文件名 浏览器强制下载静态资源