我有一个带有数千个模型的Django项目的异常值。它是我们使用Django REST框架的数据的宁静API。当我启动Django的runserver
时,由于验证了项目中的数据模型,需要几分钟才能出现。加载后,它可以按预期工作。
mod_wsgi
表现出类似的行为。发布后或重新启动apache后,我们第一次在浏览器中提出页面需要几分钟。在该第一页加载之后,整个站点几乎立即响应。通过阅读文档,这似乎是mod_wsgi
将应用程序加载到GLOBAL
应用程序组时。我一直在尝试找到一种方法来在部署后立即启动此加载过程(触摸wsgi.py(或apache重新启动,以避免部署后必须在浏览器中抬起网站,这在生产是我们在圆形网络上有几个Web服务器的生产。
我曾尝试将GLOBAL
应用程序组添加到WSGIScriptAlias
和添加WSGIImportScript
,但似乎都没有用。
这是我的.conf
文件,用于所讨论的VirtualHost。我显然缺少一些东西。
<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>
用于预加载,而不是:
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}
在WSGIScriptAlias
上同时使用process-group
和application-group
足以触发WSGI脚本文件的预加载。WSGIImportScript
也这样做了,但是如果您在WSGIScriptAlias
上使用两个选项,则不需要它。在WSGIScriptAlias
上使用了这些选项后,您也不需要WSGIProcessGroup
或WSGIApplicationGroup
。
确实要确保您还添加:
WSGIRestrictedEmbedded On
在VirtualHost
之外,因此您也没有在所有Apache Worker过程中设置Python。您只需要在mod_wsgi守护程序过程中。
尝试一下,看看您到达哪里。
您拥有:
LogLevel info
在Apache配置中,应该导致MOD_WSGI登录更多有关它正在做的事情以及何时加载内容,以便您可以验证发生的事情。