在同一个 windows 10 服务器 运行 django 应用程序上托管多个子域
Hosting multiple sub domains on the same windows 10 server running django application
我需要托管多个子域都指向同一个 windows10 服务器的站点。不同子域的虚拟主机指向不同的 WSGI 文件。在 wsgi 文件中添加了 Venv 激活。发生的问题是首先调用的 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>
这可以通过 mod_wsgi 的 WSGIDaemonProcess 或 WSGIProcessGroup 解决(因为那时你会有单独的 os.environ
s 等等),但这些功能在 [=19= 上不可用].
如果您确实需要在 Windows 上 运行,我建议您从 mod_wsgi 切换到使用 a regular reverse proxy configuration to proxy requests to e.g. Waitress 或其他 HTTP/WSGI 服务器 运行宁你的应用程序。
将项目部署到 IIS,然后根据需要配置到每个网站的绑定。
您可以使用 django-windowsauth
包通过几个简单的命令将您的项目部署到 IIS。 https://github.com/danyi1212/django-windowsauth.
将网站添加到 IIS 后,在每个项目的绑定中指定不同的主机名。
我需要托管多个子域都指向同一个 windows10 服务器的站点。不同子域的虚拟主机指向不同的 WSGI 文件。在 wsgi 文件中添加了 Venv 激活。发生的问题是首先调用的 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>
这可以通过 mod_wsgi 的 WSGIDaemonProcess 或 WSGIProcessGroup 解决(因为那时你会有单独的 os.environ
s 等等),但这些功能在 [=19= 上不可用].
如果您确实需要在 Windows 上 运行,我建议您从 mod_wsgi 切换到使用 a regular reverse proxy configuration to proxy requests to e.g. Waitress 或其他 HTTP/WSGI 服务器 运行宁你的应用程序。
将项目部署到 IIS,然后根据需要配置到每个网站的绑定。
您可以使用 django-windowsauth
包通过几个简单的命令将您的项目部署到 IIS。 https://github.com/danyi1212/django-windowsauth.
将网站添加到 IIS 后,在每个项目的绑定中指定不同的主机名。