使用nginx的实时服务器中的Django静态文件

Django static files in live server using nginx

我有一个实时服务器运行ning django,地址是http://179.188.3.54/。如您所见,该应用程序可以运行,但看起来静态文件无法运行。此外,如果我单击任何其他 link,也不起作用。

本网站运行开发版没有任何问题。我不确定我应该怎么做才能解决这个问题。

这是我的 nginx 配置文件和我的 settings.py

STATIC_URL = '/static/'
STATIC_ROOT = '/cinegloria/cinegloria/cinegloria/static/'

PS:我尝试 运行 收集静态数据 ;)

server {
    root /usr/share/nginx/www;
    index index.html index.htm;
    access_log /var/log/nginx/domain-access.log;
    server_name 0.0.0.0;

location / {
    try_files $uri $uri/ /index.html;
    proxy_pass_header Server;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_set_header X-Forwarded-For  $remote_addr;
    proxy_set_header X-Scheme $scheme;
    proxy_connect_timeout 10;
    proxy_read_timeout 10;
    proxy_pass http://0.0.0.0:8000/;
}
}

任何想法或示例代码将不胜感激!

/ 模式之前将静态服务添加到 nginx conf:

location /static {
    alias /cinegloria/cinegloria/cinegloria/static/;
}

location / {
    ...
}

或者设置STATIC_ROOT到www根目录下:

STATIC_ROOT = '/usr/share/nginx/www/static'

或者从 www root 添加符号链接到你的静态目录:

$ ln -s /cinegloria/cinegloria/cinegloria/static /usr/share/nginx/www/static

为静态文件添加另一个 nginx 指令。静态文件应该由 nginx 提供,而不是 Django 服务器。

location  /static/ {
    alias  /cinegloria/cinegloria/cinegloria/static/;
}

如果仍然不起作用,您可能需要添加 mime 类型指令。我昨天不得不这样做,因为出于某种原因,当我使用别名时 nginx 没有提供正确的 mime 类型。

作为有用的指示,每当您 运行 遇到此类问题时,请查看您的 nginx 错误日志并粘贴最后几行以进行调试。它位于 /var/log/nginx/error.log 或类似路径。