mod_wsgi/3.2 使用 mod_rewrite 将解释器上下文与 Apache2 上的 django 混合...为什么?

mod_wsgi/3.2 使用 mod_rewrite 将解释器上下文与 Apache2 上的 django 混合...为什么?

很长的故事

总结:

使用以下配置,Apache 会与 Python 解释器上下文混淆。我使用此设置部署了三个演示站点,如果经常刷新它们,就会出现竞争条件,来自数据库的一些文本/图像会相互混淆。

虚拟主机配置:

<VirtualHost *:80>
    ServerName demo.motion-m.ca
    ServerAlias *.demo.motion-m.ca
    Options FollowSymLinks
    UseCanonicalName Off
    VirtualDocumentRoot /var/www/vhosts/%0
    RewriteEngine On
    RewriteMap lowercase int:tolower

    LogFormat "%{Host}i %h %u %t \"%r\" %s %b" vcommon
    CustomLog /var/log/apache2/demo.motion-m.ca vcommon

    # Handle everything except the static/media
    RewriteCond %{REQUEST_URI} !^/(media|static)/(.*)$
    RewriteRule ^/(.*) /var/www/vhosts/%{HTTP_HOST}/apache/django.wsgi/$1
    RewriteRule . - [E=APPLICATION_GROUP:${tolower:%{SERVER_NAME}}]


    # Use the subdomain as unique ApplicationGroup/Process identifier
    # or experience funny results
    RewriteCond %{ENV:REDIRECT_SUBDOMAIN} =""
    RewriteCond %{HTTP_HOST} ([a-z0-9][-a-z0-9]+)\.demo.motion-m\.ca\.?(:80)?$ [NC]
    RewriteCond %{DOCUMENT_ROOT}/%1 -d
    RewriteRule ^(.*) %1/$1 [E=SUBDOMAIN:%1,L]
    RewriteRule ^ - [E=SUBDOMAIN:%{ENV:REDIRECT_SUBDOMAIN},L]

    # use the "website" part as group name
    WSGIProcessGroup %{ENV:SUBDOMAIN}
    WSGIApplicationGroup %{ENV:SUBDOMAIN}

    <Location ~ /(media|static)/(.*)>
       Order deny,allow
       Options -Indexes
       Allow from all
    </Location>

    <Directory /var/www/vhosts/*/apache>
       Order allow,deny
       Allow from all
       Options ExecCGI
       AddHandler wsgi-script .wsgi
    </Directory>
</VirtualHost>

django.wsgi

import os, sys, site

site.addsitedir('/usr/lib/python2.5/site-package') 
os.environ['DJANGO_SETTINGS_MODULE'] = 'projectname.settings'

path = os.path.dirname(os.path.abspath(os.path.join(__file__, '../../')))

if path not in sys.path:
    sys.path.insert(0, '/var/www/vhosts/website.demo.motion-m.ca/')
    sys.path.insert(0, '/var/www/vhosts/website.demo.motion-m.ca/projectname/')
    sys.path.insert(2, '/path/to/django-1.3')
    sys.path.insert(3, '/path/to/django-1.3/django')


import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

有任何想法吗 ?

谢谢

答案1

下列内容在我看来可能是错误的:

WSGIApplicationGroup %{ENV:SUBDOMAIN}

为什么有:

RewriteRule . - [E=APPLICATION_GROUP:${tolower:%{SERVER_NAME}}]

如果您没有使用‘APPLICATION_GROUP’。

顺便问一下,您是否使用过:

http://code.google.com/p/modwsgi/wiki/CheckingYourInstallation#Embedded_Or_Daemon_Mode http://code.google.com/p/modwsgi/wiki/CheckingYourInstallation#Sub_Interpreter_Being_Used

独立于 Django 进行验证,为相应的主机/URL 选择了预期的守护进程组和子解释器。

相关内容