3

I can't serve static files through nGinx and I am getting 404 instead. I can see the error (shown in the error.log), I just can't see how to fix it.

urls.py

urlpatterns += patterns('', (
        r'^static/(?P<path>.*)$',
        'django.views.static.serve',
        {'document_root': 'static'}
))

settings.py

STATIC_ROOT =  os.path.join(PROJECT_DIR, "staticfiles/")
STATIC_URL = '/static/'

DEBUG=False

(did a collectstatic and all static files are now in 'staticfiles/')

nginx configuration

server { 
    listen 80; 
    server_name my_ip;  
    location = /favicon.ico { access_log off; log_not_found off; } 

    location /static/ {
        root /home/project/project/project/staticfiles;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/project/project/project.sock;
    }
}

base.html

{% load staticfiles %}
<link rel="stylesheet" href="{% static '/css/main.css' %}">

and this is the log

error.log

2017/04/09 10:57:40 [error] 4719#4719: *182 open() "/home/project/project/project/staticfiles/static/images/home-lock.png" failed (2: No such file or directory)

("static/" is added in the static url and I can't see why)

xpanta
  • 8,124
  • 15
  • 60
  • 104
  • 5
    I just want to say that /home/project/project/project is not a very good directory name. – zmbq Apr 09 '17 at 11:38
  • I know. The first 'project's is actually the username. The username of each of my server's non-root users is the same as the project. Why? it is ugly but it is less error prone when dealing with various terminals on the same screen at the same time. I am sure you don't need explanation for the other two. – xpanta Apr 14 '17 at 13:22

1 Answers1

9

Try this

location /static/ {
    alias /home/project/project/project/staticfiles;
}
itzMEonTV
  • 19,851
  • 4
  • 39
  • 49
  • 1
    Will help you http://stackoverflow.com/questions/10631933/nginx-static-file-serving-confusion-with-root-alias – itzMEonTV Apr 09 '17 at 11:28