nicksil
By:
nicksil

How to direct to particular app based upon "subdirectory" or URL trailing slash suffix using Nginx and Gunicorn

May 13, 2016 980 views
Nginx Django Configuration Management Linux Basics Applications Linux Commands Python Frameworks Python

I suck at titles, but let me explain what I'm trying to have happen.

  • I have a sub-domain pointed to my server's IP, say preview.example.com.
  • I have two Django applications running on my server
  • I'm trying to configure things in such a way that if this url: preview.example.com/aa/ is entered, the request gets routed to one Django app. and if this url: preview.example.com/bb/ is entered the request gets routed to the other.

From what I've read so far, it doesn't seem like you're able to include forward slashed within the server_name directive within the Nginx configuration.

The way it's setup now works somewhat how I want things to eventually work out. When the user enters preview.example.com, a Django app is rendered. It's just that I don't want a Django app to render when that URL is fetched, I only want a Django app to render when either preview.example.com/aa/ or preview.example.com/bb/ is fetched.

My Nginx config:

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

upstream app_two {
    server 127.0.0.1:7000 fail_timeout=0;
}


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

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

    client_max_body_size 4G;
    server_name preview.example.com/aa;

    keepalive_timeout 5;

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

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

    # Proxy the static assests for the Django Admin panel
    location /static/admin {
       alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
    }

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

server {
    listen 80;
    listen [::]:80;

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

    client_max_body_size 4G;
    server_name preview.example.com/bb;

    keepalive_timeout 5;

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

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

    # Proxy the static assests for the Django Admin panel
    location /static/admin {
       alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
    }

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

Hopefully this makes some sense.

I appreciate your time and any help you may be able to lend,
Cheers

Be the first one to answer this question.