Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx conf for two gunicorn applications (django and tilestache)

I'm trying to host a site that consists of a django app and map tiles served by tilestache. I can get them running and serving content separately by using either

gunicorn_django -b 0.0.0.0:8000 

for the django app, or

gunicorn "TileStache:WSGITileServer('tilestache.cfg')"

for tilestache. I've tried daemonizing the django app and running them at the same time with the tilestache process on a different port (8080), but tilestache doesn't work. I assume the issue lies in my nginx conf:

server {
    listen   80;
    server_name localhost;

    access_log /opt/django/logs/nginx/vc_access.log;
    error_log  /opt/django/logs/nginx/vc_error.log;

    # no security problem here, since / is alway passed to upstream
    root /opt/django/;
    # serve directly - analogous for static/staticfiles
    location /media/ {
        # if asset versioning is used
        if ($query_string) {
            expires max;
        }
    }
    location /static/ {
        # if asset versioning is used
        if ($query_string) {
            expires max;
        }
    }
    location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 10;
        proxy_read_timeout 10;
        proxy_pass http://localhost:8000/;
    }
    # what to serve if upstream is not available or crashes
    error_page 500 502 503 504 /media/50x.html;
}

Can I just add another server block in the conf for proxy_pass http://localhost:8080/? Additionally, I'm very new to this stack (I've relied greatly on Adrián Deccico's tutorial here to get the django part up and running) so any "woah that's an obvious mistake" or suggestions would be greatly appreciated.

like image 372
Chris Avatar asked Feb 19 '23 20:02

Chris


1 Answers

As far as I see - you have mapped location / to go to localhost:8000. When you have 2 different upstreams, you'll need two different location mappings, one for each upstream. So assuming that the django app is the primary site on your domain, you'll have the default location as it is now:

location / {
    proxy_pass_header Server;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Scheme $scheme;
    proxy_connect_timeout 10;
    proxy_read_timeout 10;
    proxy_pass http://localhost:8000/;
}

but then add another location for the other app:

location /tilestache {
    proxy_pass_header Server;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Scheme $scheme;
    proxy_connect_timeout 10;
    proxy_read_timeout 10;
    proxy_pass http://localhost:8080/;
}

The only difference here is the port. This way the domain.com/tilestache will be processed by localhost:8080, while all other addresses will default to the django app at localhost:8000. Place the location /tilstache before location /.

For clarity you can define your upstreams like this:

upstream django_backend  {
  server localhost:8000;
}

upstream tilestache_backend  {
  server localhost:8080;
}

and then in location section, use:

location / {
    .....
    proxy_pass  http://django_backend;
}

location /tilestache {
    .....
    proxy_pass  http://tilestache_backend;
}
like image 85
Tisho Avatar answered Mar 05 '23 16:03

Tisho