Apache-2.2

mod_wsgi/3.2 使用 mod_rewrite 在 Apache2 上將解釋器上下文與 django 混合 .. 為什麼?

  • September 15, 2016

很長的故事

tl;博士:

使用下面的配置,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()

有任何想法嗎 ?

謝謝

以下內容對我來說可能是錯誤的:

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 驗證正在為相應的主機/URLS 選擇預期的守護程序組和子解釋器。

引用自:https://serverfault.com/questions/292110