【发布时间】:2018-04-08 16:46:42
【问题描述】:
我有一个 Django 1.11 站点,由 Ubuntu 16 上的 Apache 2.4.18 + ModWSGI 提供服务,但它无限期挂起。奇怪的是,如果我停止 Apache,它才会返回请求,从而完美地呈现页面,这意味着 Django 正在正确返回请求,但是某些东西阻止了 Apache 发送数据。
我的 Apache 站点.conf:
<VirtualHost *:80>
ServerName www.mysite.com
ServerAlias www.mysite.com
ServerAdmin sysadmin@mysite.com
DocumentRoot /usr/local/mysite
AllowEncodedSlashes On
Alias /media/ /usr/local/mysite/media/
Alias /static/ /usr/local/mysite/static/
<Directory /usr/local/mysite>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
Allow from all
# New directive needed in Apache 2.4.3.
Require all granted
</Directory>
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
LogLevel debug
ErrorLog ${APACHE_LOG_DIR}/mysite-error.log
CustomLog ${APACHE_LOG_DIR}/mysite-access.log combined
# Stop GIL deadlocks from crashing Python/Modwsgi due to Python C-extensions?
# Without this, you may get a "Premature end of script" error.
# https://code.google.com/p/modwsgi/wiki/ApplicationIssues#Python_Simplified_GIL_State_API
WSGIApplicationGroup %{GLOBAL}
WSGIDaemonProcess www.mysite.com python-path=/usr/local/mysite/.env/lib/python2.7/site-packages processes=1 display-name=%{GROUP} user=www-data group=www-data
WSGIProcessGroup www.mysite.com
WSGIScriptAlias / /usr/local/mysite/wsgi/mysite.wsgi
<Directory /usr/local/mysite/wsgi>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
我的 Django wsgi:
import os
import time
import traceback
import signal
import sys
from django.core.wsgi import get_wsgi_application
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings.settings'
os.environ['CELERY_LOADER'] = 'django'
sys.path.append(os.path.join(os.path.realpath(os.path.dirname(__file__)), '../src'))
sys.path.append(os.path.join(os.path.realpath(os.path.dirname(__file__)), '../src/mysite'))
try:
application = get_wsgi_application()
print 'WSGI without exception'
except Exception:
print 'handling WSGI exception'
# Error loading applications
if 'mod_wsgi' in sys.modules:
traceback.print_exc()
os.kill(os.getpid(), signal.SIGINT)
time.sleep(2.5)
我的 Apache 日志中唯一感兴趣的是几行,例如:
[Fri Oct 27 02:37:08.079977 2017] [wsgi:error] [pid 14053:tid 139644805011200] [client 10.182.122.159:45695] Timeout when reading response headers from daemon process 'www.mysite.com': /usr/local/mysite/wsgi/mysite.wsgi
显然,似乎有些东西没有收到正确的信号,导致它一直等到发生超时,但我无法弄清楚原因是什么。我尝试重构我的 Apache 配置,并禁用我的 Django 应用程序中可能超时的部分,但没有任何效果。我该如何诊断?
【问题讨论】:
标签: python django apache mod-wsgi