使用Django应用程序和Nginx设置两个网站,但在进入site1.com后,它会一直显示site2.com的内容

时间:2015-01-23 18:59:06

标签: django nginx gunicorn

在我的浏览器www.site2.com上输入后,打开www.site1.com的页面。 我认为我对nginx和gunicorn的配置都很好。

etc / nginx / site-available / site1 - 我的默认网站www.site1.com

upstream app_server {
    server 127.0.0.1:9000 fail_timeout=0;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    client_max_body_size 4G;
    server_name _;

    keepalive_timeout 5;

    # Your Django project's media files - amend as required
    location /media  {
        alias /home/django/site1/media;
    }

    # your Django project's static files - amend as required
    location /static {
        alias /home/django/site1/static_root;
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server;
    }
}

etc / nginx / site-available / site2 - 我的新网站www.site2.com

upstream app_server2 {
    server 127.0.0.1:9500 fail_timeout=0;
}

server {
    listen 80;
    listen [::]:80;

    root /usr/share/nginx/html;
    index index.html index.htm;

    client_max_body_size 4G;
    server_name www.site2.com site2.com;

    keepalive_timeout 5;

    # Your Django project's media files - amend as required
    location /media  {
        alias /home/django/test-django/site2/media;
    }

    # your Django project's static files - amend as required
    location /static {
        alias /home/django/test-django/site2/static;
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server2;
    }
}

等/ INIT / site1.conf

description "Gunicorn daemon for Django project"

start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]

# If the process quits unexpectedly trigger a respawn
respawn

setuid django
setgid django
chdir /home/django

exec gunicorn \
    --name=site1\
    --pythonpath=site1 \
    --bind=0.0.0.0:9000 \
    --config /etc/gunicorn.d/gunicorn.py \
    site1.wsgi:application

等/ INIT / site2.conf

description "Gunicorn daemon for Django project"

start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]

# If the process quits unexpectedly trigger a respawn
respawn

setuid django
setgid django
chdir /home/django/test-django/

exec gunicorn \
    --name=site2\
    --pythonpath=site2 \
    --bind=127.0.0.1:9500 \
    --config /etc/gunicorn.d/gunicorn.py \
    site2.wsgi:application

我没有忘记service nginx restartservice site2 restart,也没有nginx -t我没有任何错误。

1 个答案:

答案 0 :(得分:0)

在项目的wsgi.py文件中,您是否为django.conf.ENVIRONMENT_VARIABLE使用相同的变量?通常该行是:os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project_site.settings")

尝试更改那些行:

django.conf.ENVIRONMENT_VARIABLE = "DJANGO_SITEA_SETTINGS_MODULE"
os.environ.setdefault("DJANGO_SITEA_SETTINGS_MODULE", "project_site.settings")

为每个项目更改SITEA以及设置文件所在文件夹名称的project_site.settings。此更改后重新启动服务器。

我不知道这是否是答案,但它可能有所帮助,因为它为我解决了你描述的同样问题。