21

I'm running Django on Ubuntu Server 9.04.

Django works well, but nginx doesn't return static files - always 404.

Here's the config:

server {
    listen 80;
    server_name localhost;

    #site_media - folder in uri for static files
    location /static  {
        root /home/user/www/oil/oil_database/static_files;
        autoindex on;
    }

    #location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov) {
    #   root /home/user/www/oil/oil_database/static_files;
    #   access_log off;
    #   expires 30d;
    #}

    location / {
        root html;
        index index.html index.htm;
        # host and port to fastcgi server
        #fastcgi_pass 127.0.0.1:8080;
        fastcgi_pass unix:/home/user/www/oil/oil_database/oil.sock;
        fastcgi_param PATH_INFO $fastcgi_script_name;
        fastcgi_param REQUEST_METHOD $request_method;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_param CONTENT_TYPE $content_type;
        fastcgi_param CONTENT_LENGTH $content_length;
        fastcgi_pass_header Authorization;
        fastcgi_intercept_errors off;
    }

    access_log /var/log/nginx/localhost.access_log;
    error_log /var/log/nginx/localhost.error_log;
}

Nginx version is 0.6.35.

All directories exist and made 777 (debugging paranoia). The commented-out block doesn't help when I uncomment it.

Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
DataGreed
  • 13,245
  • 8
  • 45
  • 64

2 Answers2

62

How is your directory setup? Do you have a folder static in /home/user/www/oil/oil_database/static_files? In that case, the directive should look like this (note the trailing slash in /static/):

location /static/  {
    autoindex    on;
    root /home/user/www/oil/oil_database/static_files;
}

If you want to map the path /home/user/www/oil/oil_database/static_files to the URL /static/, you have to either

  • rename the folder static_files to static and use this directive:

    location /static/  {
        autoindex    on;
        root /home/user/www/oil/oil_database/;
    }
    
  • use an alias:

    location /static/  {
        autoindex    on;
        alias /home/user/www/oil/oil_database/static_files/;
    }
    

See the documentation on the root and alias directives.

Benjamin Wohlwend
  • 30,958
  • 11
  • 90
  • 100
  • no, there is no "static" dir. Thank you, i'll try out the alias. – DataGreed Sep 27 '09 at 19:34
  • I'm having this very same issue however the solutions here do not seem to be working for me. I've posted my own question here http://stackoverflow.com/questions/24131830/django-using-nginx-to-serve-static-content/24131959#24131959 – apardes Jun 10 '14 at 03:02
2

I have a similar config for my Django sites, but I think you want to use alias instead of root for your media. For example:

location /static {
    alias /home/user/www/oil/oil_database/static_files;
}
Ryan Duffield
  • 18,497
  • 6
  • 40
  • 40