【问题标题】:Trouble loading static files in Nginx + Digital Ocean Deployment在 Nginx + Digital Ocean 部署中加载静态文件时遇到问题
【发布时间】:2021-05-03 22:25:36
【问题描述】:

这里是 Django 初学者..

在 Digital Ocean Droplet 上的最终部署期间加载我的静态文件时遇到了一些问题

我正在使用 Nginx 和 Gunicorn。

我遵循 Traversy Media 的教程,但我无法通过我的 Digital Ocean ipv4 让我的静态文件显示在浏览器中。经过检查,它抛出了这些多个错误。

Refused to apply style from 'http://162.243.162.127/btre/static/css/all.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
GET 162.243.162.127/:510 GET http://162.243.162.127/btre/static/js/bootstrap.bundle.min.js net::ERR_ABORTED 404 (Not Found)

这是我的 nginx 设置

server {
    listen 80;
    server_name 162.243.162.127;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/djangoadmin/pyapps/realestate_project;
    }

    location /media/ {
        root /home/djangoadmin/pyapps/realestate_project;
    }

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

这是我的独角兽设置

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=djangoadmin
Group=www-data
WorkingDirectory=/home/djangoadmin/pyapps/realestate_project
ExecStart=/home/djangoadmin/pyapps/venv/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          btre.wsgi:application

[Install]
WantedBy=multi-user.target

我已尝试在终端中多次运行 collectstatic,但它什么也没做。说它有 '0 to collect'。

【问题讨论】:

  • 你有文件直接在realestate_projectrealestate_project/static
  • 您定义了location /static/,但错误显示http://162.243.162.127/btre/static/ - 至于我应该是location /btre/static/
  • 网站管理员端不加载静态文件的任何原因?

标签: python python-3.x django nginx gunicorn


【解决方案1】:

我在我的本地主机中重新创建了问题,结果我需要在 Nginx 中的“/static/”URL 位置之前添加项目文件夹应用程序的名称,就像它在我的项目文件夹应用程序的“settings.py”中一样。

sudo nano /etc/nginx/sites-available/btre_project

改变:

/静态/

到:

/my_app/静态

【讨论】:

    猜你喜欢
    • 2021-03-29
    • 2021-02-28
    • 2018-11-10
    • 2018-01-16
    • 2021-01-24
    • 2022-07-29
    • 2020-10-02
    • 1970-01-01
    • 2016-07-26
    相关资源
    最近更新 更多