Tuesday, November 25, 2014

Configuring nginx to serve static files in django

The setup that I use right now relies on Nginx to handle static files and goes to Gunicorn when there's a need to interface with Python.

I update the files on the server by using 'git push live master,' where 'live' is the name of a repository on the server that has hooks set to update a directory that lives in Django.  That directory includes the static files (css, js) that my pages rely on. 

It is possible to configure Django to automatically copy all static files to a single location. Then all you need to do make sure that that location is the one that your nginx configuration points at.  See the docs here.

In my case, I keep all my static files gathered under a directory named "static" so I just pointed Nginx at that directory.  It's reasonably likely that I'll need to change that pointer at some point, so I'm writing a reminder of what I had to do right here.


First I found the relevant configuration file for nginx.  On my Ubuntu VPS that file was under

/etc/nginx/sites-available/username


where 'username' was the user that I was set up under.  There is also a default configuration file, which would be used in the absence of the more specific username configuration. 

Second, in that file I altered the following address to point directly to my static directory

     location /static {
        alias /home/path/to/my/static/directory;
    }




Third, I refreshed nginx:
sudo nginx -reload


Fourth: There was no fourth step.  That's it!



No comments:

Post a Comment