DNN
DNN

Reputation: 871

Serving multiple Django sites with Nginx with UWSGI

I'm trying to serve two Django sites using Nginx.

I can serve either one no problem, but if I activate both it sends both urls to one site. This is my first time using Nginx, I usually use Apache so bear with me.

I've got two sites in sites enabled that look like this:

site1.com:

server{
    server_name www.site1.com;
    listen 69.164.211.85:80;
    access_log /var/www/site1.env/logs/access.log;
    error_log /var/www/site1.env/logs/error.log;

    location /static/ {
            # Point this wherever the static files for your django app are $
            autoindex on;
            alias /var/www/site1.env/Site1/static/;
    }

    location / {
        uwsgi_pass    127.0.0.1:3031;
        include       uwsgi_params;
        uwsgi_param   UWSGI_APPID site1;
        uwsgi_param UWSGI-FILE /var/www/site1.env/Site1/wsgi/site1_wsgi.py;
     }
}

site2.net

server{
        server_name www.site2.net;
        listen 69.164.211.85:80;
        access_log /var/www/site2.env/logs/access.log;
        error_log /var/www/site2.env/logs/error.log;

        location /static/ {
                # Point this wherever the static files for your django app are $
                autoindex on;
                alias /var/www/site2.env/Site2/static/;
        }

        location / {
            uwsgi_pass    127.0.0.1:3032;
            include       uwsgi_params;
            uwsgi_param   UWSGI_APPID site2;
            uwsgi_param UWSGI-FILE /var/www/site2.env/Site2/wsgi/site2.py;
         }

}

I'm also running two instances of UWSGI which get started with this scripts:

Site 1:

description "uWSGI server"

start on runlevel [2345]
stop on runlevel [!2345]

respawn
exec /usr/local/bin/uwsgi \
--home /var/www/site1.env/Site1/ \
--socket 127.0.0.1:3031 \
--chmod-socket \
--module site1_wsgi \
--pythonpath /var/www/site1.env/Site1/wsgi \
-H /var/www/site1.env

Site 2:

description "uWSGI server"

start on runlevel [2345]
stop on runlevel [!2345]

respawn
exec /usr/local/bin/uwsgi \
--home /var/www/site2.env/Site2/ \
--socket 127.0.0.1:3032 \
--chmod-socket \
--module site2 \
--pythonpath /var/www/site2.env/Site2/wsgi \
-H /var/www/sit2.env

This is what my nginx.conf file looks like:

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

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

   ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/$

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

   ##
# Logging Settings
    ##
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/$

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

I would have thought uwsgi_pass being set to different ports would prevent them going to the same one but clearly I'm missing something else. I'd appreciate any help, thanks!

Upvotes: 5

Views: 3964

Answers (2)

DNN
DNN

Reputation: 871

Ah, it may have been because I didn't include the server names without www. when I do that it seems to work.

Upvotes: 3

Tisho
Tisho

Reputation: 8482

The nginx config seems fine to me. I've made a test with the same code(two different sites pointing to different ports), and started a listener on the ports - and each site goes to the specified port.

I have some doubts on the uwsgi config, and more especially to the environment/path settings. Try the following - create a uwsgi1.conf file, and paste the following there:

[uwsgi]
socket = 127.0.0.1:3031
master = true
processes = 8
env = DJANGO_SETTINGS_MODULE=site1.settings
chdir = /var/www/site1.env/Site1/
pythonpath = /var/www/site1.env/
module = django.core.handlers.wsgi:WSGIHandler()
-H /var/www/site1.env

and a second uwsgi2.conf with the following contents:

[uwsgi]
socket = 127.0.0.1:3032
master = true
processes = 8
env = DJANGO_SETTINGS_MODULE=site2.settings
chdir = /var/www/site2.env/Site2/
pythonpath = /var/www/site2.env/
module = django.core.handlers.wsgi:WSGIHandler()
-H /var/www/site2.env

and change the values fror env/, chdir, pythonpath if needed. Then start the sites as:

respawn
exec /usr/local/bin/uwsgi --ini /path/to/uwsgi1.conf

and uwsgi2.conf for the second.

Upvotes: 1

Related Questions