You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx配置:移除静态文件URL后缀以匹配无扩展名存储文件

解决Nginx适配带.png后缀的无扩展名图片访问问题

你可以通过rewrite规则或针对性location匹配来处理带.png后缀的URL,让它指向无扩展名的实际文件,同时保留原有无后缀URL的访问能力。以下是两种可行的配置方案:

方案一:在原有location内统一处理

修改原配置,增加rewrite规则自动剥离URL中的.png后缀:

location /file/ {
  root /opt/;
  # 匹配带.png后缀的/file/路径,去掉后缀后继续处理
  rewrite ^(/file/.+)\.png$ $1 last;
  add_header Content-Disposition attachment;
  add_header Content-Type application/octet-stream;
}

方案二:单独匹配带后缀的请求

新增一个location块专门处理带.png的请求,内部重定向到无后缀路径:

# 处理带.png后缀的/file/请求
location ~* ^(/file/.+)\.png$ {
  root /opt/;
  rewrite ^ $1 last;
}

# 原有的无后缀请求处理逻辑
location /file/ {
  root /opt/;
  add_header Content-Disposition attachment;
  add_header Content-Type application/octet-stream;
}

两种方案都能实现:当用户访问http://example.com/file/ab/cdefg.png时,Nginx会自动查找/opt/file/ab/cdefg这个无扩展名文件,同时不影响http://example.com/file/ab/cdefg的正常访问。

内容的提问来源于stack exchange,提问作者Smoke

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 12:05:22