2016-05-03 125 views
0

我正在爲我們的python web應用程序配置uwsgi + nginx。 我想添加X-SENDFILE仿真(見http://uwsgi-docs.readthedocs.io/en/latest/Snippets.html):uwsgi X-Sendfile仿真mimetype是否缺失?

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

這個方法可行,但有點愚蠢。我真的希望內容類型可以由文件的擴展名決定。可能嗎?

回答

0

挖掘到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; 
     } 
    } 

uwsgi將建設啞劇字典(映射文件擴展名到內容類型),只有當變量build_mime_dict設置。由於我的配置不包含任何設置這個varabile的選項,mime字典將是空的。

因此,向它添加一些「靜態」選項(如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}