首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >uwsgi X-Sendfile模拟mimetype失踪?

uwsgi X-Sendfile模拟mimetype失踪?
EN

Stack Overflow用户
提问于 2016-05-03 09:07:23
回答 1查看 523关注 0票数 1

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

代码语言:javascript
复制
[uwsgi]
collect-header = X-Sendfile X_SENDFILE
response-route-if-not = empty:${X_SENDFILE} static:${X_SENDFILE}

现在我访问我们的站点,使用sendfile()正确响应内容。唯一的缺陷是内容-类型缺失,甚至我已经在wsgi的响应中显式地设置了它。我试验了很多方法,唯一的解决办法是:

代码语言:javascript
复制
[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}

这行得通,但有点傻。我非常希望内容类型可以由文件的扩展名来决定。有可能吗?

EN

回答 1

Stack Overflow用户

发布于 2016-05-04 02:54:52

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

代码语言:javascript
复制
    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 (将文件扩展名映射到内容类型)。而且,由于我的配置不包含设置此varabile的任何选项,所以mime dict将为空。

因此,添加一些“静态”选项(如mimefile)将使mime迪克生成。另外,将收集头更改为拉头,以确保没有显示真实的文件路径。

代码语言:javascript
复制
[uwsgi]
mimefile = /etc/mime.types
pull-header = X-Sendfile X_SENDFILE
response-route-if-not = empty:${X_SENDFILE} static:${X_SENDFILE}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36999995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档