0

I host my Djangoproject on Nginx with uwsgi. staticfiles need to be served separately by Nginx

server { 
    listen 80; 
    server_name blog.foo.de; 

    location /static { 
        root /home/user/blog/staticfiles; 
        access_log   off; 
        expires      30d; 
    } 

    location / {
       include uwsgi_params;
       uwsgi_pass unix:/tmp/blog.socket;
    }

    access_log      /var/log/nginx/blog.access.log combined;
    error_log       /var/log/nginx/blog.error.log warn;
}

My STATIC_URL is set to http://blog.foo.de/static/ The folder /home/user/blog/staticfiles contains a folder static, where the files are collected into. so according to other posts here everything sholud work -- but it doesn't. the uwsgi part works just fine, but I only get a 404 when I try to access the static files.

1
  • 1
    Are you sure you didn't forget to add another /static to the root? You claim to have another level, but it doesn't reflect in the config.
    – gertvdijk
    Dec 13, 2012 at 0:55

2 Answers 2

0

This needs some general cleanup:

  • Your server block has two location / blocks, which is not permitted; one of them will be ignored. Use only one location block for any given location.
  • Your server block has no root defined. Make sure that root is defined only in the server block, and not in the location blocks (unless a location needs a different root).

I also recommend not turning off the access_log or using expires while debugging, as this can make it more difficult to isolate the cause of a problem.


Though to really fix this, consider rewriting it entirely. A very short example:

server {
    location @django {
        include uwsgi_params; 
        uwsgi_pass unix:/tmp/blog.socket; 
    }

    location / {
        try_files $uri $uri/ @django;
    }
}
4
  • thx for your reply, but if there's only one location block allowed, how are configs like this supposed to work ? Dec 13, 2012 at 7:39
  • None of the location blocks in that question or answer are duplicates, as yours is. Dec 13, 2012 at 8:27
  • Ooops, the duplicate was just a mistake when I posted from my phone Dec 13, 2012 at 11:11
  • It still doesn't work, but this config should look a bit cleaner: server { listen 80; server_name blog.orlives.de; location @django { include uwsgi_params; uwsgi_pass unix:/tmp/blog.socket; } location / { try_files $uri $uri/ @django; } location /static { root /home/django/blog/staticfiles; #access_log off; expires 0d; } access_log /var/log/nginx/blog.access.log combined; error_log /var/log/nginx/blog.error.log warn; } Dec 13, 2012 at 11:21
0

Use this code in for your staticfiles location

location ^~ /static/ {
    alias  /home/user/blog/staticfiles;
}

This link might help you for more details- https://stackoverflow.com/questions/23226357/django-nginx-static-files-404

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .