用nGinx提供静态文件的Django给出了错误404

时间:2017-04-09 11:11:59

标签: python django nginx django-staticfiles

我无法通过nGinx提供静态文件,而是获得了404。我可以看到错误(显示在error.log中),我只是看不出如何解决它。

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

(一个collectstatic和所有静态文件现在都在'staticfiles /'中)

nginx配置

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' %}">

这是日志

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 /”,我看不清楚原因)

1 个答案:

答案 0 :(得分:5)

试试这个

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