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

前端 未结 1 1449
一向
一向 2021-02-06 13:12

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. We\'re assessing other op

相关标签:
1条回答
  • 2021-02-06 14:05

    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. If you're sites don't get much traffic, I'd only create 1 or 2 gunicorn workers per site. (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.

    First, start up your gunicorn servers on local host at different ports using the command line. 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: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).

    Now you need to create two separate nginx server configuration files to point each domain name at it's respective django site. 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.

    Make sure your server configuration files are included in the main nginx.conf file.

    Reload nginx and you should be good to go.

    If anyone has an easier/better way to go about this please post it.

    0 讨论(0)
提交回复
热议问题