【问题标题】:Nginx try_files & rewrite & content typeNginx try_files & 重写 & 内容类型
【发布时间】:2013-11-27 02:06:47
【问题描述】:

我目前正在从 lighttpd 迁移到 nginx。 我有一些奇怪的文件(不要问为什么): 1.说一个名为'news'的文件,实际上应该更像news.txt 2.一个名为'.html'的文件,实际上应该是index.html

使用 lighttpd,只需重写这些东西就可以了。 Nginx 仍然会使用 try_files 或 rewrite 定位这些文件,但我无法控制返回的内容类型。我的意思是如果文件名为“.html”,则内容类型为“application/octet-stream”。

我知道我可以使用 more_set_headers 来实现这一点,但有没有其他方法可以做到这一点?我的意思是为什么 nginx 认为名为 '.html' 的文件不是 html 文件?

【问题讨论】:

    标签: nginx content-type


    【解决方案1】:

    我的意思是为什么 nginx 认为名为 '.html' 的文件不是 html 文件?

    在类 unix 系统中,开头的点通常用作隐藏文件的指示符。在这种情况下,点后面的部分不是文件扩展名。

    我知道我可以使用 more_set_headers 来实现这一点,但有没有其他方法可以做到这一点?

    您应该使用 default_type 指令而不是 3-rd 方模块。

    例如:

    location =/.html {
        default_type text/html;
    }
    

    【讨论】:

    • 我试过这样:location = / { default_type text/html; } try_files $uri/index.html $uri/.html $uri.html $uri =404; 得到了 403。
    • try_files 指令未被继承。在您的情况下,它仅适用于未被位置捕获的请求。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    • 1970-01-01
    • 2012-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多