只能针对目录重设 Nginx 的 Content-Type 吗?

2018-05-03 20:16:21 +08:00
 Loyalsoldier

想访问 http://example.com/a.png,解析出来的是 HTML 内容并执行,而不是 png 图片。

可是分别设置如下 location,只有 /test/ 这个 location 成功了。

location /test/ {
  default_type text/html;
  types {
    text/html  png;
  }
}
location \.png {
  default_type text/html;
  types {
    text/html  png;
  }
}

官网文档也没有讲清楚:

http://nginx.org/en/docs/http/ngx_http_core_module.html#types

请问重设 Nginx 的 Content-Type 只能针对目录吗?不能针对以特定后缀结尾的 URL 吗?

6349 次点击
所在节点    NGINX
3 条回复
isCyan
2018-05-03 20:30:59 +08:00
location 匹配文件后缀要用正则表达式
location 中使用正则要开头 ~* (不区分大小写) 或者 ~ (区分大小写)
我觉得改成
location ~* \.png$ 就可以了
Loyalsoldier
2018-05-03 20:35:33 +08:00
@isCyan #1

是我大意了……
Lax
2018-05-04 01:01:44 +08:00
全局修改的话,按照 nginx 默认配置文件都有个 include mime.types,改那里面的。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/451862

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX