在运行Django应用程序的同一Windows 10服务器上托管多个子域

时间:2020-06-04 09:30:37

标签: django windows apache web-deployment mod-wsgi

我需要托管多个站点,这些站点的子域都指向同一个Windows10服务器。不同子域的虚拟主机指向不同的WSGI文件。 Venv激活已添加到wsgi文件中。发生的问题是,首先调用的Django设置文件将应用于所有子域。

例如 company1.example.com-> /company1_wsgi.py-> company1_settings.py company2.example.com-> /company2_wsgi.py-> company2_settings.py

现在,company1_settings应用于company1.example.com和company2.example.com。我看到正确的WSGI文件是基于子域调用的,但是由于某些原因,os.environ ['DJANGO_SETTINGS_MODULE']不会基于子域动态更新,并且始终使用第一个值

wsgi文件

python_home= 'E:/app/demo/env'
activate_this = 'E:/app/demo/env/Scripts/activate_this.py'
exec(open(activate_this).read(),dict(__file__=activate_this))
import os
import sys
from django.core.wsgi import get_wsgi_application
import site
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('E:/app/demo/env/Lib/site-packages')




# Add the app's directory to the PYTHONPATH
sys.path.append('E:/app/demo/crm')
sys.path.append('E:/app/demo/crm/appointment_scheduler')

os.environ['DJANGO_SETTINGS_MODULE'] = 'appointment_scheduler.settings.preprod'
application = get_wsgi_application()
<VirtualHost *:443>
ServerName demo.crmbaylyn.com
WSGIScriptAlias / "E:/app/demo/crm/appointment_scheduler/wsgi_win.py"
WSGIPassAuthorization On
WSGIScriptReloading On
<Directory "E:/app/demo/crm/appointment_scheduler">
   <Files wsgi.py>
      Order deny,allow
      Allow from all
      Require all granted
   </Files>
Order allow,deny
    Allow from all
    Require all granted
</Directory>
 Alias /static "E:/app/demo/crm/static"
    <Directory "E:/app/demo/crm/static">
        Require all granted
    </Directory>

SSLEngine on
SSLCertificateFile "C:/Apache24/conf/crmbaylyn.cert"
SSLCertificateKeyFile "C:/Apache24/conf/crmbaylyn.key"
</VirtualHost>
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule !/robots.txt https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]

</VirtualHost>
<VirtualHost *:443>
ServerName dev.crmbaylyn.com
WSGIScriptAlias / "E:/app/dev/crm/appointment_scheduler/wsgi_collection/dev_wsgi.py"
WSGIPassAuthorization On
WSGIScriptReloading On
<Directory "E:/app/dev/crm/appointment_scheduler">
   <Files wsgi.py>
      Order deny,allow
      Allow from all
      Require all granted
   </Files>
Order allow,deny
    Allow from all
    Require all granted
</Directory>
 Alias /static "E:/app/dev/crm/static"
    <Directory "E:/app/dev/crm/static">
        Require all granted
    </Directory>

SSLEngine on
SSLCertificateFile "C:/Apache24/conf/crmbaylyn.cert"
SSLCertificateKeyFile "C:/Apache24/conf/crmbaylyn.key"
</VirtualHost>

2 个答案:

答案 0 :(得分:1)

这可以通过mod_wsgi的WSGIDaemonProcess或WSGIProcessGroup解决(因为您将拥有单独的os.environ等),但是这些功能在Windows上不可用。

如果您确实需要在Windows上运行,我建议从mod_wsgi切换到使用a regular reverse proxy configuration将请求代理到例如Waitress或其他运行您的应用程序的HTTP / WSGI服务器。

答案 1 :(得分:0)

将您的项目部署到 IIS,然后根据需要配置到每个网站的绑定。

您可以使用 django-windowsauth 包通过几个简单的命令将您的项目部署到 IIS。 https://github.com/danyi1212/django-windowsauth

将网站添加到 IIS 后,在每个项目的绑定中指定不同的主机名。