너 바보 아니야

Nginx分发-指定文件名称

2020-12-07

Nginx分发-指定文件名称

一般在我们上传完资源文件之后,为了避免文件名冲突,会将文件名改成毫无意义的一段字符
其实,nginx是支持下载时重命名的,让文件名更加友好

eg: xxx.com/storage/uploads/2020-12-07-728632757453485.png?n=test

有后缀:

1
2
3
4
5
location ~* .*\.(txt|jpg|png)(\?.*)?$ { 
if ($request_uri ~* ^.*\/(.*)\.(txt|jpg|png)(\?n=([^&]+))$) {
add_header Content-Disposition "attachment;filename=$arg_n.$2";
}
}

无后缀:

1
2
3
4
5
location ~* [^/]+(?!.*/) {
if ($request_uri ~* [^/]+(?!.*/)(\?n=([^&]+))$) {
add_header Content-Disposition "attachment;filename=$arg_n";
}
}
  • Notice

如若需要,请戳 [ About ] 下联系我 欢迎联系.

That’s it! enjoy :)

标签: nginx
使用支付宝打赏
使用微信打赏

欢迎点击上方按钮对我打赏,谢谢你给我吃糖果