0

I want to run two Django applications on the same server, with Nginx and Gunicorn. I can run them separately, but not both at the same time: When I try, one is OK, but for the other one I receive 502. Here are my configuration files:

First nginx config for the first project:

upstream app_server {
    server 127.0.0.1:9000 fail_timeout=0;
}

server {
    listen 80;
    # listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    client_max_body_size 4G;
    server_name domain1.com;

    keepalive_timeout 5;

    # Your Django project's media files - amend as required
    location /media  {
        alias /home/django/domain1/media;
    }

    # your Django project's static files - amend as required
    location /static {
        alias /home/django/domain1/static;
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server;
    }
}

And for the second django project:

upstream app_server_2 {
    server 127.0.0.1:8000 fail_timeout=0;
}

server {
    listen 80;
    # listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    client_max_body_size 4G;
    server_name domain2.com;

    keepalive_timeout 5;

    # Your Django project's media files - amend as required
    location /media  {
        alias /home/django/domain2/media;
    }

    # your Django project's static files - amend as required
    location /static {
        alias /home/django/domain2/static;
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server_2;
    }
}

And Gunicorn upstart script:

description "Gunicorn daemon for Django project"

start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]

# If the process quits unexpectedly trigger a respawn
respawn

setuid django
setgid django
chdir /home/django

exec gunicorn \
    --name=domain1 \
    --pythonpath=domain1 \
    --bind=127.0.0.1:9000 \
    --config /etc/gunicorn.d/gunicorn.py \
    domain1.wsgi:application

exec gunicorn \
    --name=domain2 \
    --pythonpath=domain2 \
    --bind=127.0.0.1:8000 \
    --config /etc/gunicorn.d/gunicorn.py \
    domain2.wsgi:application

Any suggestions?

Update 24.01.2016

While I am debugging, I have separated Gunicorn upstart scripts into two (one for each project), and renamed them. I have figured out that I am getting 4 of the following error (2 for each, one for starting, one for stopping on Gunicorn daemon restart):

/etc/gunicorn.d/gunicorn.py:2: RuntimeWarning: Parent module '/etc/gunicorn.d/gunicorn' not found while handling absolute import
    from os import environ

Here is my gunicorn.py:

"""gunicorn WSGI server configuration."""
from multiprocessing import cpu_count
from os import environ


def max_workers():
    return cpu_count() * 2 + 1

max_requests = 1000
worker_class = 'gevent'
workers = max_workers()
  • Are both `gunicorn` daemons up and running? – gxx Jan 14 '16 at 22:55
  • @gf_ I have just checked and no, it is not running. Why do you think? – Hasan Can Saral Jan 18 '16 at 17:11
  • Hm...well, because this could explain your `502 bad gateway` error... – gxx Jan 18 '16 at 17:52
  • @gf_ I know, I meant why doesn't it run? As you see, I have 2 exec statements, but one of them is not running. – Hasan Can Saral Jan 24 '16 at 17:25
  • You've to debug this. If you need help, you've to provide details: logfiles, content of `/etc/gunicorn.d/gunicorn.py`, etc. – gxx Jan 24 '16 at 18:07
  • @gf_ I have updated the question. I cannot thank you enough for this :) – Hasan Can Saral Jan 24 '16 at 19:27
  • My guess is that the user 'django' cannot access the python modules. So, two questions: 1- How can it access the required Python modules? 2- Is it ok for two web applications to share the same user? Thanks. – Hasan Can Saral Jan 24 '16 at 19:33
  • This is not a `gunicorn` question, but a `python` problem. 1.) It depends how and where these modules are installed. Are you using a virtualenv? Have a look at [this](http://stackoverflow.com/questions/2267984/dynamic-class-loading-in-python-2-6-runtimewarning-parent-module-plugins-not). I think SO is better suited for this type of question, thus I've flagged it so it gets migrated. 2.) It's not best practice, but yes, it's possible. – gxx Jan 25 '16 at 08:49

0 Answers0