我有一个包含多个应用程序的django项目(django + apache + mod_wsgi + nginx),我想将每个应用程序映射为子域名:
project/
app1 (domain.com)
app2 (sub1.domain.com)
app3 (sub3.domain.com)
我有一个.wsgi脚本服务于项目,它存储在一个文件夹/ apache中。下面是我的vhost文件。我正在为每个子域使用单个vhost文件而不是单独的vhost文件:
<VirtualHost *:8080>
ServerAdmin xxx@gmail.com
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 xxx@gmail.com
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,images,js文件方面存在问题。
答案 0 :(得分:1)
假设nginx工作正常,您确实在Apache中设置了适当的NameVirtualHost指令,以便它能正确映射虚拟主机。如果不这样做,那么所有请求都将在配置文件中找到第一个虚拟主机。
答案 1 :(得分:0)
默认情况下,Django的设计不是每个主机名具有不同的行为。 django-multihost中间件解决方案适用于我启用此功能。我看到您已在Apache配置中配置了admin_media位置,但在Apache或Nginx配置中没有配置媒体位置,这可能就是您遇到媒体问题的原因了?