【问题标题】:uwsgi X-Sendfile emulation mimetype missing?uwsgi X-Sendfile 仿真 mimetype 丢失?
【发布时间】:2016-08-28 06:06:22
【问题描述】:

我正在为我们的 python web 应用程序配置 uwsgi+nginx。 我想添加 X-Sendfile 仿真(参见http://uwsgi-docs.readthedocs.io/en/latest/Snippets.html):

[uwsgi]
collect-header = X-Sendfile X_SENDFILE
response-route-if-not = empty:${X_SENDFILE} static:${X_SENDFILE}

现在我访问我们的网站,使用 sendfile() 正确响应了内容。唯一的缺陷是缺少 Content-Type,即使我已经在 wsgi 的响应中明确设置了它。我尝试了很多方法,我发现的唯一解决方法是:

[uwsgi]

collect-header = X-Sendfile-Content-Type X_SENDFILE_CONTENT_TYPE
collect-header = X-Sendfile X_SENDFILE
response-route-if-not= empty:${X_SENDFILE_CONTENT_TYPE} addheader:Content-Type: ${X_SENDFILE_CONTENT_TYPE} 
response-route-if-not = empty:${X_SENDFILE} static:${X_SENDFILE}

这可行,但有点傻。我真的希望内容类型可以由文件的扩展名确定。有可能吗?

【问题讨论】:

    标签: python mime-types uwsgi x-sendfile


    【解决方案1】:

    在深入研究了uwsgi的源代码后,我找到了原因(见 https://github.com/unbit/uwsgi/blob/2.0.12/core/uwsgi.c#L2677)

        if (uwsgi.build_mime_dict) {
            if (!uwsgi.mime_file)
    #ifdef __APPLE__
                uwsgi_string_new_list(&uwsgi.mime_file, "/etc/apache2/mime.types");
    #else
                uwsgi_string_new_list(&uwsgi.mime_file, "/etc/mime.types");
    #endif
            struct uwsgi_string_list *umd = uwsgi.mime_file;
            while (umd) {
                if (!access(umd->value, R_OK)) {
                    uwsgi_build_mime_dict(umd->value);
                }
                else {
                    uwsgi_log("!!! no %s file found !!!\n", umd->value);
                }
                umd = umd->next;
            }
        }
    

    只有在设置了变量 build_mime_dict 时,uwsgi 才会构建 mime dict(将文件扩展名映射到内容类型)。而且由于我的配置不包含设置此变量的任何选项,因此 mime dict 将为空。

    因此添加一些“静态”选项(如 mimefile)将构建 mime dict。还将 collect-header 更改为 pull-header 以确保不显示真实文件路径。

    [uwsgi]
    mimefile = /etc/mime.types
    pull-header = X-Sendfile X_SENDFILE
    response-route-if-not = empty:${X_SENDFILE} static:${X_SENDFILE}
    

    【讨论】:

      猜你喜欢
      • 2021-10-09
      • 2014-12-10
      • 1970-01-01
      • 2016-01-28
      • 1970-01-01
      • 2016-06-08
      • 2011-04-05
      • 2014-04-02
      • 2023-03-19
      相关资源
      最近更新 更多