1

i'm working on a configuration of uwsgi+nginx for our python web app. I want to add the X-Sendfile emulation (see 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}

Now i visit our site, the content is correctly responsed using sendfile(). The only flaw is Content-Type is missing, even i have set it explicitly in wsgi's response. I've experimented many methods, the only workaround i've found is:

[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}

This works but a little bit silly. I'd really want the content type can be determined by the file's extension. Is it possible?

jayven
  • 770
  • 8
  • 19

1 Answers1

1

After digging into uwsgi's source code, i've found the reason (see 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;
        }
    }

uwsgi will build the mime dict (mapping file extension to content type) only when the variable build_mime_dict is set. And since my configuration does not contain any options that setting this varabile, the mime dict will be empty.

So adding some 'static' option (like mimefile) to it will make the mime dict built. Also changing collect-header to pull-header to ensure the real file path not shown.

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