V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
Loyalsoldier
V2EX  ›  NGINX

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

  •  
  •   Loyalsoldier · 2018-05-03 20:16:21 +08:00 · 6340 次点击
    这是一个创建于 2177 天前的主题,其中的信息可能已经有所发展或是发生改变。

    想访问 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 吗?

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

    是我大意了……
    Lax
        3
    Lax  
       2018-05-04 01:01:44 +08:00 via iPad
    全局修改的话,按照 nginx 默认配置文件都有个 include mime.types,改那里面的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2820 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 11:28 · PVG 19:28 · LAX 04:28 · JFK 07:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.