简体   繁体   English

通过Gunicorn / Nginx使用Django的站点框架运行多个站点

[英]Running Multiple Sites with Django's Sites Framework through Gunicorn/Nginx

I have a Django based CMS that uses Django's sites framework and Nginx/Apache/mod_wsgi virtual hosts to run a number of websites on different domains. 我有一个基于Django的CMS,它使用Django的站点框架和Nginx / Apache / mod_wsgi虚拟主机来运行不同域上的许多网站。 We're assessing other options for a Django stack and have the CMS running with a single site on a new server with Nginx proxying to Gunicorn (gunicorn_django, specifically). 我们正在评估Django堆栈的其他选项,并让CMS在新服务器上运行单个站点,Nginx代理Gunicorn(具体来说是gunicorn_django)。

Although this works great for a single site, I'm not sure how to configure Gunicorn for multiple sites. 虽然这对单个站点很有用,但我不确定如何为多个站点配置Gunicorn。 The problem is that with Apache/mod_wsgi, we could set the DJANGO_SETTINGS_MODULE for mod_wsgi to the appropriate site's settings.py 问题是,使用Apache / mod_wsgi,我们可以将mod_wsgi的DJANGO_SETTINGS_MODULE设置为相应站点的settings.py

import os, sys

def inflight(filename):
    """
    Calculate absolute path to the folder containing "myfile.wsgi", then
    append to the PYTHONPATH.
    """
    ROOT = ('/').join(os.path.abspath(os.path.dirname(filename)).split('/')[0:-1])
    sys.path.append(ROOT)
    sys.path.append(os.path.join(ROOT, 'website'))

    sys.stdout = sys.stderr
    # Each website should have a settings file: /www/mysite.com/website/settings.py
    os.environ['DJANGO_SETTINGS_MODULE'] = 'website.settings'
    import django.core.handlers.wsgi
    return django.core.handlers.wsgi.WSGIHandler()

At the moment I'm thinking that I have to have a different instance of Gunicorn for each virtual host site we run but that seems overkill for the traffic we get to most of our sites. 目前我认为我必须为我们运行的每个虚拟主机站点安装一个不同的Gunicorn实例,但这对我们大多数站点的流量来说似乎有点过分。

Does anyone run Gunicorn with Django's sites framework and can give a hint to how it's configured? 有没有人用Django的网站框架运行Gunicorn并且可以暗示它是如何配置的?

I had the same problem and stumbled upon this question in search of the same answer. 我遇到了同样的问题,偶然发现了这个问题,寻找相同的答案。

I know the question is old and you've probably figured it out by now, but since it might be useful for someone else, here's how I solved it: 我知道这个问题已经很久了,你现在可能已经知道了,但是因为它可能对其他人有用,这就是我解决它的方法:

  • You do need to run separate gunicorn processes to make django sites framework work because you can only point a gunicorn instance at one settings.py file. 您需要运行单独的gunicorn进程以使django站点框架工作,因为您只能将gunicorn实例指向一个settings.py文件。 If you're sites don't get much traffic, I'd only create 1 or 2 gunicorn workers per site. 如果你的网站没有太多流量,我每个网站只会创建1或2个gunicorn工作者。 (I know, still probably overkill). (我知道,仍然可能是矫枉过正的)。

  • Ideally you would want to manage these different processes with something like supervisord to make it easier to manage starting/stoping/restarting your different sites, but I couldn't get it working. 理想情况下,您可能希望使用诸如supervisord之类的东西来管理这些不同的流程,以便更轻松地管理启动/停止/重新启动您的不同站点,但我无法使其正常运行。

First, start up your gunicorn servers on local host at different ports using the command line. 首先,使用命令行在不同端口的本地主机上启动gunicorn服务器。 Ex: 例如:

gunicorn_django -w 2 -b 127.0.0.1:1000 /path/to/my/django/project/site1_settings.py --daemon gunicorn_django -w 2 -b 127.0.0.1:1000 /path/to/my/django/project/site1_settings.py --daemon

gunicorn_django -w 2 -b 127.0.0.1:1001 /path/to/my/django/project/site2_settings.py --daemon gunicorn_django -w 2 -b 127.0.0.1:1001 /path/to/my/django/project/site2_settings.py --daemon

You now have 2 django sites running on localhost at ports 1000 and 1001 (you can use whatever ports suite you). 您现在在端口1000和1001上的localhost上运行了2个django站点(您可以使用任何端口套件)。

Now you need to create two separate nginx server configuration files to point each domain name at it's respective django site. 现在,您需要创建两个单独的nginx服务器配置文件,以便在其各自的django站点上指向每个域名。 Ex: 例如:

server {
    listen 80;
    server_name website1.com;
    client_max_body_size 4G;

    keepalive_timeout 4;

    location /media {
        root /path/to/my/django/project/media; #this servers your static files
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header HOST $http_host;
        proxy_redirect off;

        if (!-f $request_filename){
            proxy_pass http://127.0.0.1:1000; #point to the django site already running on local host
            break;
        }
    }

    #add in any additional nginx configuration such as support for 500 errors or proxy apache to server php files etc.
}

Then create a duplicate of the nginx configuration for your 2nd site, but change the server name and the proxy_pass to the values for site 2. 然后为第二个站点创建nginx配置的副本,但将服务器名称和proxy_pass更改为站点2的值。

Make sure your server configuration files are included in the main nginx.conf file. 确保您的服务器配置文件包含在主nginx.conf文件中。

Reload nginx and you should be good to go. 重新加载nginx,你应该好好去。

If anyone has an easier/better way to go about this please post it. 如果有人有更容易/更好的方法去发布,请发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM