django 项目中每个应用程序具有子域的单个 django 实例
我有一个包含多个应用程序的 django 项目(django+apache+mod_wsgi+nginx),我想将每个应用程序映射为子域:
project/
app1 (domain.com)
app2 (sub1.domain.com)
app3 (sub3.domain.com)
我有一个为该项目提供服务的 .wsgi 脚本,该脚本存储在文件夹 /apache 中。下面是我的虚拟主机文件。我对每个子域使用单个虚拟主机文件,而不是单独的虚拟主机文件:
<VirtualHost *:8080>
ServerAdmin [email protected]
ServerName www.domain.com
ServerAlias domain.com
DocumentRoot /home/path/to/app/
Alias /admin_media/ /usr/local/lib/python2.6/dist-packages/django/contrib/admin/media
<Directory /home/path/to/wsgi/apache/>
Order deny,allow
Allow from all
</Directory>
LogLevel warn
ErrorLog /home/path/to/logs/apache_error.log
CustomLog /home/path/to/logs/apache_access.log combined
WSGIDaemonProcess domain.com user=www-data group=www-data threads=25
WSGIProcessGroup domain.com
WSGIScriptAlias / /home/path/to/apache/kcdf.wsgi
</VirtualHost>
<VirtualHost *:8081>
ServerAdmin [email protected]
ServerName sub1.domain.com
ServerAlias sub1.domain.com
DocumentRoot /home/path/to/app
Alias /admin_media/ /usr/local/lib/python2.6/dist-packages/django/contrib/admin/media
<Directory /home/path/to/wsgi/apache/>
Order deny,allow
Allow from all
</Directory>
LogLevel warn
ErrorLog /home/path/to/logs/apache_error.log
CustomLog /home/path/to/logs/apache_access.log combined
WSGIDaemonProcess sub1.domain.com user=www-data group=www-data threads=25
WSGIProcessGroup sub1.domain.com
WSGIScriptAlias / /home/path/to/apache/kcdf.wsgi
</VirtualHost>
我的domain.com的Nginx配置:
server {
listen 80;
server_name domain.com;
access_log off;
error_log off;
# proxy to Apache 2 and mod_wsgi
location / {
proxy_pass http://127.0.0.1:8080/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_max_temp_file_size 0;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}
sub.domain.com的配置:
server {
listen 80;
server_name sub.domain.com;
access_log off;
error_log off;
# proxy to Apache 2 and mod_wsgi
location / {
proxy_pass http://127.0.0.1:8081/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_max_temp_file_size 0;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}
此设置似乎不起作用,一切似乎都指向到主域。我尝试过 http://effbot.org/zone/django-multihost.htm 哪种有效,但似乎在加载我的 css、图像、js 文件时出现问题。
I have a django project (django+apache+mod_wsgi+nginx) with multiple apps, I'd like to map each app as a subdomain:
project/
app1 (domain.com)
app2 (sub1.domain.com)
app3 (sub3.domain.com)
I have a single .wsgi script serving the project, which is stored in a folder /apache. Below is my vhost file. I'm using a single vhost file instead of separate ones for each sub-domain:
<VirtualHost *:8080>
ServerAdmin [email protected]
ServerName www.domain.com
ServerAlias domain.com
DocumentRoot /home/path/to/app/
Alias /admin_media/ /usr/local/lib/python2.6/dist-packages/django/contrib/admin/media
<Directory /home/path/to/wsgi/apache/>
Order deny,allow
Allow from all
</Directory>
LogLevel warn
ErrorLog /home/path/to/logs/apache_error.log
CustomLog /home/path/to/logs/apache_access.log combined
WSGIDaemonProcess domain.com user=www-data group=www-data threads=25
WSGIProcessGroup domain.com
WSGIScriptAlias / /home/path/to/apache/kcdf.wsgi
</VirtualHost>
<VirtualHost *:8081>
ServerAdmin [email protected]
ServerName sub1.domain.com
ServerAlias sub1.domain.com
DocumentRoot /home/path/to/app
Alias /admin_media/ /usr/local/lib/python2.6/dist-packages/django/contrib/admin/media
<Directory /home/path/to/wsgi/apache/>
Order deny,allow
Allow from all
</Directory>
LogLevel warn
ErrorLog /home/path/to/logs/apache_error.log
CustomLog /home/path/to/logs/apache_access.log combined
WSGIDaemonProcess sub1.domain.com user=www-data group=www-data threads=25
WSGIProcessGroup sub1.domain.com
WSGIScriptAlias / /home/path/to/apache/kcdf.wsgi
</VirtualHost>
My Nginx configuration for the domain.com:
server {
listen 80;
server_name domain.com;
access_log off;
error_log off;
# proxy to Apache 2 and mod_wsgi
location / {
proxy_pass http://127.0.0.1:8080/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_max_temp_file_size 0;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}
Configuration for the sub.domain.com:
server {
listen 80;
server_name sub.domain.com;
access_log off;
error_log off;
# proxy to Apache 2 and mod_wsgi
location / {
proxy_pass http://127.0.0.1:8081/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_max_temp_file_size 0;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}
This set up doesn't seem to work, everything seems to point to the main domain. I've tried http://effbot.org/zone/django-multihost.htm which kind of worked but seems to have issues with loading my css,images,js files.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设 nginx 工作正常,您确实在 Apache 中设置了适当的 NameVirtualHost 指令,以便它能够正确映射虚拟主机。如果不这样做,那么所有请求都将发送到配置文件中找到的第一个虚拟主机。
Assuming nginx is working okay, you do have appropriate NameVirtualHost directives set in Apache so that it will map virtual hosts correctly. If you don't, then all requests will got to first virtual host found in configuration file.
默认情况下,Django 并未设计为每个主机名具有不同的行为。 django-multihost 中间件解决方案非常适合我启用此功能。我发现您在 Apache 配置中配置了 admin_media 位置,但在 Apache 或 Nginx 配置中没有配置媒体位置,这可能就是您遇到媒体问题的原因?
By default Django isn't designed to have different behavior per hostname. The django-multihost middleware solution works fine for me to enable this functionality. I see that you have configured your admin_media location in your Apache configuration, but not a media location in either the Apache or Nginx configurations, this may be why you are having issues with media?