【发布时间】:2018-09-25 18:24:57
【问题描述】:
settings.py:
DEBUG = False
STATIC_ROOT = '/opt/app/statics/'
# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/statics/'
# Additional locations of static files
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'statics'),)
urls.py
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
import settings
if settings.DEBUG == True:
urlpatterns += staticfiles_urlpatterns()
/etc/nginx/sites-available/app
server {
server_name xxx.xxx.xxx.xxx;
access_log on;
location /statics/ {
alias /opt/app/statics/;
}
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}
/etc/nginx/sites-enabled/app 与 sites-available 中的完全相同 包含静态文件的文件夹已被授予最高权限 (chmod 777)。
每当我尝试使用命令运行服务器时:
gunicorn -b 0.0.0.0:8000 example.wsgi:application
它实际上运行服务器,但没有正确重定向到静态文件
【问题讨论】:
-
找不到了吗?还是别的什么?
-
你跑
collectstatic了吗? -
是的,我已经运行了 collectstatic。错误:未找到在此服务器上未找到请求的 URL /statics/css/bootstrap.css。
-
请确保在
collectstatic之后重启nginx -
是的,我已经重启了