Several Django applications using nginx and gunicorn on Ubuntu 14.04 trusty server

advertisements

I am new to server configuration. I do some google and config django app using gunicorn and nginx on ubuntu 14.04 trusty server. For first django app I use port number 80 and my configfiles are :

/etc/init/gunicorn.conf :-

description "Gunicorn application server handling myproject"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
setuid
setgid www-data
chdir /home/myserver/my_virtualenv_path/myproject
exec /home/myserver/my_virtualenv_path/myproject/gunicorn --workers 2 --bind unix:/home/myserver/my_virtualenv_path/myproject/myproject.sock myproject.wsgi:application

My nginx configuration file for first django app:

/etc/nginx/site-available :-

server {
    listen 80;
    server_name myapp.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/myserver/my_virtualenv_path/myproject;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/myserver/my_virtualenv_path/myproject/myproject.sock;
    }
}

after that i link site to site-enabled . Next i create a new django app inside the first djngo app virtualenv like:

FirstApp_Virtual_Env\first_djangoapp\app files

FirstApp_Virtual_Env\Second_djangoapp\app files

Now i configure gunicorn for second app like :

/etc/init/gunicorn_t :-

description "Gunicorn application server handling myproject2"
    start on runlevel [2345]
    stop on runlevel [!2345]
    respawn
    setuid
    setgid www-data
    chdir /home/myserver/my_virtualenv_path/myproject2
    exec /home/myserver/my_virtualenv_path/myproject/gunicorn --workers 2 --bind unix:/home/myserver/my_virtualenv_path/myproject2/myproject2.sock myproject2.wsgi:application

My nginx configuration file for second django app:

/etc/nginx/site-available :-

server {
    listen 8000;
    server_name myapp2.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/myserver/my_virtualenv_path/myproject2;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/myserver/my_virtualenv_path/myproject2/myproject2.sock;
    }
}

after that i link site to site-enabled .

Now here is my problem when i type myapp.com then my first django app is working fine but for second app when i type myapp2.com its showing nginx page and when i type myapp2.com:8000 it's working fine . I do some google for that but i am unable to find solution. I am newbie to this so please give me a hint for that how to correct my problem. Thanks for your time.


You configured nginx to serve myapp2.com on port 8000:

server {
    listen 8000;
    server_name myapp2.com;
    # ...
}

so why would you expect nginx to serve it on port 80 ?

[edit] I thought the above was enough to make the problem clear but obviously not, so let's start again:

You configured nginx to serve myapp2.com on port 8000 (the listen 8000; line in your conf, so nginx do what you asked for: it serves myapp2.com on port 8000.

If you want nginx to serve myapp2.com on port 80 (which is the implied default port for http so you don't have to specify it explicitely in your url - IOW 'http://myapp2.com/' is a shortcut for 'http://myapp2.com:80/'), all you have to do is to configure nginx to serve it on port 80 just like you did for 'myapp.com': replace listen 8000; by listen 80;.