nginx Django和Gunicorn中的css和JavaScript文件的HTTP 404错误

kqlmhetl  于 2023-06-21  发布在  Nginx
关注(0)|答案(1)|浏览(97)

实际误差为

Refused to apply style from 'http://127.0.0.1:8000/static/css/home.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
GET http://127.0.0.1:8000/static/js/home.js net::ERR_ABORTED 404 (Not Found)

当我开始使用Gunicorn作为应用服务器时,这个错误就开始发生了。css的第一个错误是误导。文件未开始。Chrome开发者工具显示

Request URL:http://127.0.0.1:8000/static/css/home.css
Request Method:GET
Status Code:404 Not Found
Referrer Policy:same-origin
Connection:close
Content-Length:8559
Content-Type:text/html
Date:Wed, 07 Jun 2023 19:32:29 GMT
Referrer-Policy:same-origin
Server:gunicorn

状态码我看到所有的css和JavaScript文件都有相同的状态码。
以下是我的相关设置

settings.py

INSTALLED_APPS = [
    .
    .
    'django.contrib.staticfiles',
    'bootstrap4',
    .
    .
]

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "kubera/static"),
]

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, "static")

import mimetypes
mimetypes.add_type("text/css", ".css", True)

manage.py collectstatic命令正确复制了文件

~/workspace/djangoapp/src$ python3 manage.py collectstatic
161 static files copied to '/home/<myhomedir>/workspace/djangoapp/src/static'.

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    {% load static %}
    {% load bootstrap4 %}
    {% bootstrap_css %}
    {% bootstrap_javascript jquery='full' %}
    {% bootstrap_messages %}
    <link href="{% static 'css/home.css' %}"
      type="text/css" rel="stylesheet">

    <title>{% block title %}Parier Config Central {% endblock %}</title>
  </head>
  <body>
    <main>
      <div class="container-fluid headDiv">
        Main div content shown here
      </div>
    </main>
    <script type="text/javascript" src="{% static 'js/home.js' %}"></script>
    <script type="text/javascript" src="{% static 'js/main.js' %}"></script>
  </body>
</html>

我可以正确地看到所有的HTML内容。在服务器端控制台上,我看到以下错误调试消息

~/workspace/djangoapp/src$ gunicorn --bind 127.0.0.1:8000 <myappname>.wsgi
[2023-06-07 12:32:24 -0700] [68057] [INFO] Starting gunicorn 20.1.0
[2023-06-07 12:32:24 -0700] [68057] [INFO] Listening at: http://127.0.0.1:8000 (68057)
[2023-06-07 12:32:24 -0700] [68057] [INFO] Using worker: sync
[2023-06-07 12:32:24 -0700] [68058] [INFO] Booting worker with pid: 68058
Not Found: /static/css/home.css
Not Found: /static/js/home.js
Not Found: /static/js/main.js
Not Found: /static/images/BlankPicture.png
Not Found: /static/css/home.css
Not Found: /favicon.ico

编辑:Gunicorn套接字和服务配置/etc/systemd/system/gunicorn.socket

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

/etc/systemd/system/gunicorn.socket

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

[Service]
User=<username>
Group=<usergroup>
WorkingDirectory=/absolute-path-to-your-wsgi-file-directory
ExecStart=/usr/local/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          <projectname>.wsgi:application

[Install]
WantedBy=multi-user.target

一旦配置了gunicorn套接字和服务,就可以启动它们。

~/workspace/djangoapp/src$sudo systemctl start gunicorn.socket
~/workspace/djangoapp/src$sudo systemctl enable gunicorn.socket
~/workspace/djangoapp/src$sudo systemctl status gunicorn.socket
~/workspace/djangoapp/src$sudo systemctl daemon-reload
~/workspace/djangoapp/src$sudo systemctl restart gunicorn

NGINX配置文件**/etc/nginx/sites-available/**

server {
    listen 80;
    server_name 127.0.0.1;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /absolute-path-to-the-parent-of-static-directory;
    }

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

启用应用

sudo ln -s /etc/nginx/sites-available/<app-name> /etc/nginx/sites-enabled/
hk8txs48

hk8txs481#

这个答案是给像我这样的菜鸟的。我只让 Django 和古独角兽在一起。我没有任何Web服务器。Django不提供静态文件。Gunicorn是一个应用服务器,也不提供静态文件。你必须有一个像NGINX这样的Web服务器来提供静态文件。当我提到NGINX时,一切都开始工作了。我打算用Gunicorn和Nginx设置更新我原来的问题,以防有人想要快速设置参考。

相关问题