我有一个django项目,其中站点是从管理员动态部署的。它包括以下apache设置:
<VirtualHost *:80>
ServerName branch1.domain.com
ServerAlias *.domain.com
DocumentRoot /home/project/www/deploy/apache/multisite/
WSGIDaemonProcess project_multisite user=project group=project processes=5 threads=5
WSGIProcessGroup project_multisite
然后 .htaccess 文件(由app生成)位于/ home / project / www / deploy / apache / multisite /选择哪个 apache.wsgi 文件将使用:
AddHandler wsgi-script .wsgi
RewriteEngine On
RewriteRule ^media/ - [L]
RewriteRule ^static/ - [L]
{% for site in sites %}
RewriteRule ^site_{{site.id}}/apache.wsgi - [L]
RewriteCond %{HTTP_HOST} {{site.domain}}$ [NC]
RewriteRule ^(.*)$ /site_{{site.id}}/apache.wsgi/$1 [QSA,PT,L]
{% endfor %}
问题是,随机(大约50%的请求)apache崩溃并出现错误:
AccessInit: hash collision: 3 for both 1 and 1
[Thu Feb 12 11:48:02 2015] [error] [client 89.176.103.182] Premature end of script headers: apache.wsgi,
我找到discussion about this issue并将以下代码添加到所有 apache.wsgi 文件几乎解决了我的问题:
import sys
import PIL.Image
sys.modules['Image'] = PIL.Image
问题是,从那时起,PIL的某些功能停止工作,我在使用JPEG图像时开始出现此错误:
Feb 12 20:33:11 2015] [error] Internal Server Error: /admin/main/gallery/1/upload-photos/
[Thu Feb 12 20:33:11 2015] [error] Traceback (most recent call last):
[Thu Feb 12 20:33:11 2015] [error] File "/home/project/www/deploy/apache/multisite/site_1/../../../../deploy/env/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
[Thu Feb 12 20:33:11 2015] [error] response = wrapped_callback(request, *callback_args, **callback_kwargs)
[Thu Feb 12 20:33:11 2015] [error] File "/home/project/www/deploy/apache/multisite/site_1/../../../../deploy/env/lib/python2.7/site-packages/django/utils/decorators.py", line 105, in _wrapped_view
[Thu Feb 12 20:33:11 2015] [error] response = view_func(request, *args, **kwargs)
[Thu Feb 12 20:33:11 2015] [error] File "/home/project/www/deploy/apache/multisite/site_1/../../../../deploy/env/lib/python2.7/site-packages/django/views/decorators/cache.py", line 52, in _wrapped_view_func
[Thu Feb 12 20:33:11 2015] [error] response = view_func(request, *args, **kwargs)
[Thu Feb 12 20:33:11 2015] [error] File "/home/project/www/deploy/apache/multisite/site_1/../../../../deploy/env/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 206, in inner
[Thu Feb 12 20:33:11 2015] [error] return view(request, *args, **kwargs)
[Thu Feb 12 20:33:11 2015] [error] File "/home/project/www/deploy/apache/multisite/site_1/../../../../src/main/admin/branch.py", line 166, in upload_photos_form
[Thu Feb 12 20:33:11 2015] [error] photo.thumb_admin.save(afile.name, afile)
[Thu Feb 12 20:33:11 2015] [error] File "/home/project/www/deploy/apache/multisite/site_1/../../../../deploy/env/lib/python2.7/site-packages/django_resized/forms.py", line 43, in save
[Thu Feb 12 20:33:11 2015] [error] thumb.save(new_content, format=img.format, quality=self.field.quality, **img.info)
[Thu Feb 12 20:33:11 2015] [error] File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 1439, in save
[Thu Feb 12 20:33:11 2015] [error] save_handler(self, fp, filename)
[Thu Feb 12 20:33:11 2015] [error] File "/usr/lib/python2.7/dist-packages/PIL/JpegImagePlugin.py", line 471, in _save
[Thu Feb 12 20:33:11 2015] [error] ImageFile._save(im, fp, [("jpeg", (0,0)+im.size, 0, rawmode)])
[Thu Feb 12 20:33:11 2015] [error] File "/usr/lib/python2.7/dist-packages/PIL/ImageFile.py", line 476, in _save
[Thu Feb 12 20:33:11 2015] [error] fh = fp.fileno()
[Thu Feb 12 20:33:11 2015] [error] UnsupportedOperation: fileno
我正在使用:
我的问题是如何解决这个问题所以我不会得到这两个错误中的任何一个。谢谢你的帮助。
更新: 应用程序的行为取决于apache.wsgi中PIL导入覆盖的位置: 第一个变体(下面)导致上面描述的 UnsupportedOperation:fileno (可能是因为导入了debian标准版本的PIL):
import os
import sys
import site
import PIL.Image
sys.modules['Image'] = PIL.Image
os.environ['DJANGO_SETTINGS_MODULE'] = 'site_{{site.id}}'
www_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../../../')
sys.path = [
os.path.join(www_dir, 'deploy/settings/'),
os.path.join(www_dir, 'src'),
os.path.join(www_dir, 'deploy/env/lib/python%s/site-packages' % sys.version[:3])
] + sys.path
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
第二个变体(下面)结果仍然在 AccessInit:哈希冲突:3和1 。所以它实际上根本没有帮助:
import os
import sys
import site
os.environ['DJANGO_SETTINGS_MODULE'] = 'site_{{site.id}}'
www_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../../../')
sys.path = [
os.path.join(www_dir, 'deploy/settings/'),
os.path.join(www_dir, 'src'),
os.path.join(www_dir, 'deploy/env/lib/python%s/site-packages' % sys.version[:3])
] + sys.path
import PIL.Image
sys.modules['Image'] = PIL.Image
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()