Search code examples
pythondjangonginxfastcgistatic-files

Serving static files from nginx with django + fastcgi debian


I'm a little rusty when it comes to the new way of static file serving with django 1.3 however I'm sure it cannot be django that is at fault here.

I'm trying to run a django app with nginx + fastcgi on a cloud server with debian installed. We only have one server at the moment (while we develop) and will be looking to run multiple servers with a load balancer to make this a little simpler however I'm having trouble actually getting nginx to serve static files.

I've followed various tutorials for setting up nginx.conf to serve the files

server {
    listen 80;
    server_name 127.0.0.1;

    location /static  {
        autoindex on;
        root /static;
    }
}

The above is an extract from nginx.conf. Now no matter what I set root to, nginx throws a 404 not found error when trying to access http://127.0.0.1/static/.

The file structure is as follows:

/home/user/site/project
/home/user/site/static
/home/user/site/templates

Django settings.py has the following set up as STATIC_ROOT and STATIC_URL

STATIC_ROOT = "/home/user/site/static/"
STATIC_URL  = "http://127.0.0.1/static/"

If anyone could point us in the right direction of where to do with this it would be fantastic.


Solution

  • Resolution found, based on Jim's answer above.

    root /static 
    

    Has now been changed to:

    root /home/user/site/static
    

    However then checking the logs it seems that the path nginx was trying to locate at http://127.0.0.1/static was /home/user/site/static/static/ which is evidently incorrect.

    I'm unsure if this is the correct method however i'm now pointing all static files into root (removing the need for /static in the url. so for /static/images I now just point it to http://127.0.0.1/images/.