CSS未与Nginx和Uwsgi出现在Django中

发布于 2025-02-06 20:03:55 字数 1314 浏览 3 评论 0原文

这是我的第一个真正的Django项目,我正在尝试使用Nginx和Uwsgi配置它以进行生产。它在数字海洋Ubuntu服务器上运行。除了服务静态CSS文件外,还设置了所有内容。这奇怪的是它提供静态图像和JavaScript文件,唯一不提供的是CSS。

这是我的nginx的网站配置文件('nebula'是ubuntu用户和django project的名称):

# configuration of the server
server {
    server_name example.com www.example.com;
    charset     utf-8;
    # max upload size
    client_max_body_size 75M;
    # Django media and static files
    location /media  {
        alias /home/nebula/nebula/media;
    }
    location /assets {
        alias /home/nebula/nebula/assets;
    }
    # Send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /home/nebula/nebula/uwsgi_params;
    }
}

我的设置。

BASE_DIR = Path(__file__).resolve().parent.parent

STATIC_URL = '/assets/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
STATIC_ROOT = os.path.join(BASE_DIR, "assets/")

STATICFILES_DIRS = ( os.path.join(BASE_DIR,'assets/'),)

这就是 静态是相同的,我将其复制以解决该问题):

assets      demo.py    media   nebula.sock       static  uwsgi_params
db.sqlite3  manage.py  nebula  nebula_uwsgi.ini  set     store

这是“资产/”的内部:

admin  css  images  jazzmin  js  vendor

This is my first real Django project and I am trying to configure it for production using NGINX and uWSGI. It is running on a Digital Ocean Ubuntu server. Everything is set up and working besides serving static CSS files. The strange thing about this is that its serving static images and JavaScript files fine, the only thing that isn't being served is the CSS.

This is what my site configuration file for NGINX looks like ('nebula' is the Ubuntu User and the name of the Django project):

# configuration of the server
server {
    server_name example.com www.example.com;
    charset     utf-8;
    # max upload size
    client_max_body_size 75M;
    # Django media and static files
    location /media  {
        alias /home/nebula/nebula/media;
    }
    location /assets {
        alias /home/nebula/nebula/assets;
    }
    # Send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /home/nebula/nebula/uwsgi_params;
    }
}

This is what my Settings.py file looks like:

BASE_DIR = Path(__file__).resolve().parent.parent

STATIC_URL = '/assets/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
STATIC_ROOT = os.path.join(BASE_DIR, "assets/")

STATICFILES_DIRS = ( os.path.join(BASE_DIR,'assets/'),)

This is what my base directory looks like (assets and static are the same, I duplicated it in an attempt to solve the issue):

assets      demo.py    media   nebula.sock       static  uwsgi_params
db.sqlite3  manage.py  nebula  nebula_uwsgi.ini  set     store

This is inside of 'assets/':

admin  css  images  jazzmin  js  vendor

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

似梦非梦 2025-02-13 20:03:56

nginx配置中静态文件的位置路径应匹配static_url设置

    location /assets {
        alias /home/nebula/nebula/assets;
    }

The location path for your static files in your nginx config should match your STATIC_URL setting

    location /assets {
        alias /home/nebula/nebula/assets;
    }
霊感 2025-02-13 20:03:56

似乎这里可能有一个问题,

您可以尝试更改

STATICFILES_DIRS = ( os.path.join(BASE_DIR,'media/'),)

STATICFILES_DIRS = ( os.path.join(BASE_DIR,'assets/'),)

Seems like there could be an issue here

Could you try Changing

STATICFILES_DIRS = ( os.path.join(BASE_DIR,'media/'),)

to

STATICFILES_DIRS = ( os.path.join(BASE_DIR,'assets/'),)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文