[Solved]-How to run multiple Django sites on Nginx and uWSGI?

8👍

You can create create multiple virtual hosts that allow you to host multiple sites, independent from each other. More info here: http://wiki.nginx.org/VirtualHostExample.

A bit more detailed info here as well on how to setup virtual hosts http://projects.unbit.it/uwsgi/wiki/RunOnNginx#VirtualHosting.

5👍

You can run multiple instances of uwsgi using Emperor Mode.

This handles the creation of new worker instances. These instances are brilliantly and hilariously named vassals. Each vassal just needs a config file which is usually placed (or symlinked) in /etc/uwsgi/vassals

For nginx you’ll need to create a server block for each host you wish to serve. Just change the server_name directive for each host you want to serve. Here’s an example:

#Simple HTTP server
server {
    listen   80; 
    root /usr/share/nginx/www;
    server_name host1.example.com;
}

#Django server
server {
    listen   80; 
    server_name host2.example.com;

    #...upstream config...
}

Important: Make sure you have specified your host names in /etc/hosts. I found that without doing this my django site was also served on the default server IP despite specifying that it should only be served on a specific host name within my nginx configuration.

2👍

I see many suggestions like @donturner’s answer. i.e. set two or more different server in nginx configure file. But the problem is each server needs an unique server_name either different domain name or sub-domain name. How about this kind of situation: I want to server two different Django project like this:

www.ourlab.cn/site1/  # first Django project
www.ourlab.cn/site2/  # second Django project

In this way, we can configure all of the settings in one server.

This is my setting in /etc/nginx/nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

}

This is my setting in /etc/nginx/conf.d/self_configure.conf

# /etc/nginx/conf.d/self_configure.conf
server {
    listen       80;
    server_name  www.ourlab.cn;

    # note that these lines are originally from the "location /" block
    root   /mnt/data/www/ourlab;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # Django media
    location /media  {
        # your Django project's media files - amend as required
        alias /mnt/data/www/metCCS/metCCS/static/media;
    }

    location /static {
        # your Django project's static files - amend as required
        # first project's static files path
        alias /mnt/data/www/metCCS/metCCS/static/static_dirs;
    }
    location /static_lip {
        # second project's static files path
        alias /mnt/data/www/lipidCCS/lipidCCS/static/static_dirs;
    }

    # match www.ourlab.cn/metccs/*
    location ~* ^/metccs {
        include     uwsgi_params;
        uwsgi_pass  unix:/run/uwsgi/metCCS.sock;
    }
    # match www.ourlab.cn/lipidccs/*
    location ~* ^/lipidccs {
        include     uwsgi_params;
        uwsgi_pass  unix:/run/uwsgi/lipidCCS.sock;
    }
}

You also need to change one of the Django project’s settings.py file as STATIC_URL = '/static_lip/', so two projects can use their static files separately.

A new finding is nginx can server static files by itself. Even we close uwsgi and Django, we also can use these files through browser.

👤Belter

Leave a comment