Django,使用 nGinx 提供静态文件给出错误 404
Django, serving static files with nGinx gives error 404
我无法通过 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)
(静态url中添加了"static/",我不明白为什么)
试试这个
location /static/ {
alias /home/project/project/project/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)
(静态url中添加了"static/",我不明白为什么)
试试这个
location /static/ {
alias /home/project/project/project/staticfiles;
}