【问题标题】:static files do not show on a live django site served via nginx静态文件不会显示在通过 nginx 提供的实时 django 站点上
【发布时间】:2019-10-17 13:34:09
【问题描述】:

我正在按照 digitalocean 教程使用 django、gunicorn 和 nginx 在服务器上构建站点。该网站是实时的,但静态文件不显示。

语法简洁 我得出的结论是,问题必须来自 django 或 nginx(因为任何静态文件的配置都与它们中的任何一个有关

我尝试修改 django 和 nginx 配置,因为它们看起来像是源代码。

1)django settings.py文件django_project/django_project/settings.py中的静态位置定义

STATIC_URL = '/static/',
STATIC_ROOT = "/dir1/dir2/dir3/django_project/static"

2) /etc/nginx/nginx.conf 中的 nginx 配置文件

http {

    server {
           listen       800  default_server;
           server_name  mysite.com;
           return       444;
           location /static/ {
                  alias /dir1/dir2/dir3/django_project/static;
                            }
           }

3) /etc/nginx/sites-availables 中的默认文件:

server {
    listen 80;
    server_name mysite.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
    root  /dir1/dir2/dir3/django_project/static;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

我希望显示静态文件

============================== 编辑:

我尝试打开静态文件时收到的错误消息是:

找不到

在此服务器上找不到请求的资源。

【问题讨论】:

  • 您是否运行了python manage.py collectstatic(它将所有静态文件复制到/dir1/dir2/dir3/django_project)?
  • 是的,并检查了文件是否存在
  • 检查 nginx 日志。可能是权限问题。
  • /etc/nginx/sites-availables 中将root /dir1/dir2/dir3/django_project/static; 更改为alias /dir1/dir2/dir3/django_project/static;
  • 你可以看到 django herenginx.conf 的完整 nginx 配置文件

标签: python django nginx server


【解决方案1】:

该网站现在可以运行了。

问题很多,但都与 nginx 或 gunicorn 设置有关。

始终检查 nginx 和 gunicorn 错误:

此处详细讨论了 Nginx 和 Gunicorn 的故障排除:

https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04

【讨论】:

    猜你喜欢
    • 2011-01-27
    • 1970-01-01
    • 2021-12-02
    • 2020-09-14
    • 1970-01-01
    • 1970-01-01
    • 2019-06-27
    • 2022-12-17
    • 2016-01-28
    相关资源
    最近更新 更多