如何使用外部子域在VPS上部署django。

时间:2017-02-05 17:19:16

标签: django nginx subdomain

美好的一天。 我有一个使用django开发的网络应用程序。我在当地测试得很好,我很高兴它的工作方式。 但是,我遇到了将其联机的问题,我使用这两个指南来实现我的部署:

但是我的页面给了我一个禁止的页面。

我怀疑我的问题与我处理子域的方式有关。所以该网站。已经开发使用PHP,我已经与django合作,并提供了一个子域名member.domain.com,所以我在VPS上部署它,并且必须使它使用子域。< / p>

这是我允许的主机在settings.py

中的显示方式
ALLOWED_HOSTS = ['member.domain.com']

在我的nginx中:

    upstream app_server {
  # fail_timeout=0 means we always retry an upstream even if it failed
  # to return a good HTTP response (in case the Unicorn master nukes a
  # single worker for timing out).

  server unix:/home/path/project/run/gunicorn.sock fail_timeout=0;
}

server {

    listen   80;
    server_name member.domain.com;

    client_max_body_size 4G;

    access_log /home/path/project/logs/nginx-access.log;
    error_log /home/path/project/logs/nginx-error.log;

    location /static/ {
        alias   /home/path/project/src/static/;
    }

    location /media/ {
        alias   /home/path/project/src/media/;
    }

    location / {
        # an HTTP header important enough to have its own Wikipedia entry:
        #   http://en.wikipedia.org/wiki/X-Forwarded-For
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        # enable this if and only if you use HTTPS, this helps Rack
        # set the proper protocol for doing redirects:
        # proxy_set_header X-Forwarded-Proto https;

        # pass the Host: header from the client right along so redirects
        # can be set properly within the Rack application
        proxy_set_header Host $http_host;

        # we don't want nginx trying to do something clever with
        # redirects, we set the Host: header above already.
        proxy_redirect off;

        # set "proxy_buffering off" *only* for Rainbows! when doing
        # Comet/long-poll stuff.  It's also safe to set if you're
        # using only serving fast clients with Unicorn + nginx.
        # Otherwise you _want_ nginx to buffer responses to slow
        # clients, really.
        # proxy_buffering off;

        # Try to serve static files from nginx, no point in making an
        # *application* server like Unicorn/Rainbows! serve static files.
        if (!-f $request_filename) {
            proxy_pass http://app_server;
            break;
        }
    }

    # Error pages
    error_page  502 503 504 /500.html;
    location = /500.html {
        root /home/path/project/src/static/;
    }
}

我不确定我做错了什么。

我将不胜感激任何帮助

1 个答案:

答案 0 :(得分:0)

回复&#39; example.com&#39;和任何子域,用点

启动域

ALLOWED_HOSTS = [&#39; .example.com&#39;,&#39; 203.0.113.5&#39;]

我甚至没有尝试过如何在子域名上运行django,但是从你分享的文章链接中,你错过了settings.py中的一些配置

ALLOWED_HOSTS = [&#39; member.domain.com&#39;]

更改

ALLOWED_HOSTS = [&#39; .domain.com&#39;]

希望这能解决您的问题