Apache重新启动后,mod_wsgi在首页加载时需要几分钟

时间:2017-06-27 12:11:30

标签: python django apache mod-wsgi

我有一个包含数千个模型的Django项目的异常值。它作为我们使用Django REST Framework的数据的RESTful API。当我启动Django的runserver时,由于验证了项目中的数据模型,需要几分钟时间才能启动。加载后,它会按预期工作。

mod_wsgi表现出类似的行为。在发布或重新启动Apache之后,我们第一次在浏览器中调出页面需要几分钟时间。在第一页加载之后,整个网站几乎立即响应。通过阅读文档,这似乎是mod_wsgi将应用程序加载到GLOBAL应用程序组时的情况。在部署(触及wsgi.py)或Apache重启之后,我一直试图找到一种方法来立即启动此加载过程,以避免在部署后将网站放入浏览器中,这是在生产中存在问题,因为我们在专用网络上的循环代理后面有几个Web服务器。

我已尝试将GLOBAL应用程序组添加到WSGIScriptAlias并添加WSGIImportScript,但似乎都不起作用。

以下是我所讨论的VirtualHost的.conf文件。我显然遗漏了一些东西。

<VirtualHost *:443>
  SSLEngine On

  ServerName django-project-dev.my.domain.com
  ErrorLog "|/usr/sbin/cronolog /path/to/log/httpd/errorlog/%Y/%Y-%m-django-project-dev-error.log"
  LogLevel info

  WSGIApplicationGroup %{GLOBAL}
  WSGIDaemonProcess django-project-dev-https python-home=/path/to/django/djangoproject/virtualenvs/django-project-dev request-timeout=1800 connect-timeout=300 socket-timeout=600 user=djangoproject group=wharton
  WSGIProcessGroup django-project-dev-https
  WSGIScriptAlias / /path/to/django/djangoproject/html/django-project-dev/config/wsgi.py process-group=django-project-dev-https application-group=%{GLOBAL}
  WSGIImportScript /path/to/django/djangoproject/html/django-project-dev/config/wsgi.py process-group=django-project-dev-https application-group=%{GLOBAL}

  <Directory /path/to/django/djangoproject/html/django-project-dev/config>
    Require all granted
  </Directory>

  Alias /static/ /path/to/django/djangoproject/html/django-project-dev/static/
  <Directory /path/to/django/djangoproject/html/django-project-dev/static>
    Require all granted
  </Directory>

  # This is required for Django REST Framework Auth Pass Thru
  WSGIPassAuthorization On
</VirtualHost>

1 个答案:

答案 0 :(得分:1)

对于预加载,而不是:

  WSGIApplicationGroup %{GLOBAL}
  WSGIDaemonProcess django-project-dev-https python-home=/path/to/django/djangoproject/virtualenvs/django-project-dev request-timeout=1800 connect-timeout=300 socket-timeout=600 user=djangoproject group=wharton
  WSGIProcessGroup django-project-dev-https
  WSGIScriptAlias / /path/to/django/djangoproject/html/django-project-dev/config/wsgi.py process-group=django-project-dev-https application-group=%{GLOBAL}
  WSGIImportScript /path/to/django/djangoproject/html/django-project-dev/config/wsgi.py process-group=django-project-dev-https application-group=%{GLOBAL}

仅使用:

  WSGIDaemonProcess django-project-dev-https python-home=/path/to/django/djangoproject/virtualenvs/django-project-dev request-timeout=1800 connect-timeout=300 socket-timeout=600 user=djangoproject group=wharton
  WSGIScriptAlias / /path/to/django/djangoproject/html/django-project-dev/config/wsgi.py process-group=django-project-dev-https application-group=%{GLOBAL}

process-group上使用application-groupWSGIScriptAlias足以触发WSGI脚本文件的预加载。 WSGIImportScript执行了相同的操作,但如果您在WSGIScriptAlias上同时使用这两个选项,则不需要它。在WSGIScriptAlias上使用这些选项后,您也不需要WSGIProcessGroupWSGIApplicationGroup

请确保您还添加:

WSGIRestrictedEmbedded On

VirtualHost之外,所以你也没有在所有Apache工作进程中设置Python。您只需要在mod_wsgi守护程序进程中使用它。

试一试,看看你到了哪里。

正如你所说:

LogLevel info
Apache配置中的

应该导致mod_wsgi更多地记录它正在做什么以及何时加载东西,以便你可以验证发生了什么。

相关问题