mod_wsgi Apache 重启后第一页加载需要几分钟
mod_wsgi takes minutes on first page load after Apache restart
我有一个包含数千个模型的 Django 项目异常值。它作为我们使用 Django REST 框架的数据的 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>
用于预加载,而不是:
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 工作进程内部设置 Python。您只在 mod_wsgi 守护进程中需要它。
试一试,看看能达到什么效果。
因为你有:
LogLevel info
在 Apache 配置中,应该会导致 mod_wsgi 记录更多有关它正在做什么以及何时加载内容的信息,以便您可以验证正在发生的事情。
我有一个包含数千个模型的 Django 项目异常值。它作为我们使用 Django REST 框架的数据的 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>
用于预加载,而不是:
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 工作进程内部设置 Python。您只在 mod_wsgi 守护进程中需要它。
试一试,看看能达到什么效果。
因为你有:
LogLevel info
在 Apache 配置中,应该会导致 mod_wsgi 记录更多有关它正在做什么以及何时加载内容的信息,以便您可以验证正在发生的事情。