【问题标题】:Setup two websites using Django apps and Nginx, but after enter site1.com it keeps showing content of site2.com使用 Django 应用程序和 Nginx 设置两个网站,但进入 site1.com 后,它一直显示 site2.com 的内容
【发布时间】:2015-01-23 18:59:05
【问题描述】:

在我的浏览器中输入 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;
    }
}

etc/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

etc/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 之后我也没有任何错误。

【问题讨论】:

  • 为什么第一个没有服务器名?我在 uWSGI 和 Mod-WSGI 之前使用 NGINX,所以我的熟悉度不是那么强。我已经被浏览器缓存烧毁了,请确保您清楚,以防您的浏览器只是指向第一个地址的缓存副本。
  • 您的 DNS 设置是否正确配置?

标签: django nginx gunicorn


【解决方案1】:

在您项目的 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 更改为设置文件所在文件夹的名称。更改后重新启动服务器。

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

【讨论】:

    猜你喜欢
    • 2015-06-09
    • 2019-05-24
    • 1970-01-01
    • 2012-11-11
    • 1970-01-01
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-10
    相关资源
    最近更新 更多