我正在为我们的python应用程序进行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()正确响应内容。唯一的缺陷是内容-类型缺失,甚至我已经在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}这行得通,但有点傻。我非常希望内容类型可以由文件的扩展名来决定。有可能吗?
发布于 2016-05-04 02:54:52
在深入研究了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 (将文件扩展名映射到内容类型)。而且,由于我的配置不包含设置此varabile的任何选项,所以mime dict将为空。
因此,添加一些“静态”选项(如mimefile)将使mime迪克生成。另外,将收集头更改为拉头,以确保没有显示真实的文件路径。
[uwsgi]
mimefile = /etc/mime.types
pull-header = X-Sendfile X_SENDFILE
response-route-if-not = empty:${X_SENDFILE} static:${X_SENDFILE}https://stackoverflow.com/questions/36999995
复制相似问题