How to serve static files for multiple Django projects via nginx to same domain

Posted by thanley on Server Fault See other posts from Server Fault or by thanley
Published on 2014-08-18T21:39:41Z Indexed on 2014/08/18 22:22 UTC
Read the original article Hit count: 475

Filed under:
|
|
|

I am trying to setup my nginx conf so that I can serve the relevant files for my multiple Django projects. Ultimately I want each app to be available at www.example.com/app1, www.example.com/app2 etc. They all serve static files from a 'static-files' directory located in their respective project root.

The project structure:

Home
  Ubuntu
    Web
        www.example.com
            ref
            logs
            app
                app1
                    app1
                        static
                            bower_components
                        templatetags
                    app1_project
                    templates
                    static-files

                app2
                    app2
                        static
                        templates
                        templatetags
                    app2_project
                    static-files

                app3
                    tests
                    templates
                    static-files
                    static
                    app3_project
                    app3
            venv

When I use the conf below, there are no problems for serving the static-files for the app that I designate in the /static/ location. I can also access the different apps found at their locations. However, I cannot figure out how to serve all of the static files for all the apps at the same time. I have looked into using the 'try_files' command for the static location, but cannot figure out how to see if it is working or not.

Nginx Conf - Only serving static files for one app:

server {
listen 80;

server_name example.com; 
server_name www.example.com;

access_log /home/ubuntu/web/www.example.com/logs/access.log;
error_log  /home/ubuntu/web/www.example.com/logs/error.log;

root /home/ubuntu/web/www.example.com/;

location /static/ {
        alias /home/ubuntu/web/www.example.com/app/app1/static-files/; 
}


location /media/ {
        alias /home/ubuntu/web/www.example.com/media/;
    }

location /app1/ {
    include      uwsgi_params;
    uwsgi_param SCRIPT_NAME /app1;
    uwsgi_modifier1 30;
    uwsgi_pass   unix:///home/ubuntu/web/www.example.com/app1.sock;
}

location /app2/ {
    include      uwsgi_params;
    uwsgi_param SCRIPT_NAME /app2;
    uwsgi_modifier1 30;
    uwsgi_pass   unix:///home/ubuntu/web/www.example.com/app2.sock;
}

location /app3/ {
    include      uwsgi_params;
    uwsgi_param SCRIPT_NAME /app3;
    uwsgi_modifier1 30;
    uwsgi_pass   unix:///home/ubuntu/web/www.example.com/app3.sock;
}
# what to serve if upstream is not available or crashes
error_page 400 /static/400.html;
error_page 403 /static/403.html;
error_page 404 /static/404.html;
error_page 500 502 503 504 /static/500.html;

# Compression
gzip on;
gzip_http_version 1.0;
gzip_comp_level 5;
gzip_proxied any;
gzip_min_length  1100;
gzip_buffers 16 8k;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
# Some version of IE 6 don't handle compression well on some mime-types, 
# so just disable for them
gzip_disable "MSIE [1-6].(?!.*SV1)";
# Set a vary header so downstream proxies don't send cached gzipped 
# content to IE6
gzip_vary on;
}

Essentially I want to have something like (I know this won't work)

location /static/ {
    alias /home/ubuntu/web/www.example.com/app/app1/static-files/; 
    alias /home/ubuntu/web/www.example.com/app/app2/static-files/; 
    alias /home/ubuntu/web/www.example.com/app/app3/static-files/; 
}

or (where it can serve the static files based on the uri)

location /static/ {
     try_files $uri $uri/ =404;
}

So basically, if I use try_files like above, is the problem in my project directory structure? Or am I totally off base on this and I need to put each app in a subdomain instead of going this route? Thanks for any suggestions

TLDR: I want to go to:
www.example.com/APP_NAME_HERE

And have nginx serve the static location:
/home/ubuntu/web/www.example.com/app/APP_NAME_HERE/static-files/;

© Server Fault or respective owner

Related posts about nginx

Related posts about django