在AWS lightsail上使用Django nginx gunicorn加载静态文件时出现问题

jw5wzhpr  于 4个月前  发布在  Nginx
关注(0)|答案(1)|浏览(67)

我在lightsail上安装了我的django应用程序,在Ubuntu服务器上从头开始。网页工作正常,但静态文件没有加载。我在web_app.conf(cht_web.conf)上读到添加一些信息,它看起来像这样:

`server {

    listen 80;
    
    server_name xxx.xxx.xxx.xxx;
    
    location / {
    
    include proxy_params;
    `
    proxy_pass http://localhost:8000/;
    
    }
    
    location /static/ {
    
    autoindex on;
    root /home/ubuntu/cht_web/cht_web/static/;
    }

字符串
我不确定我是否必须安装Supervisor,所以我还没有安装它,
我运行gunicorn与:
gunicorn cht_web.wsgi --bind 0.0.0.0:8000 --workers 4,log为:

\[2023-11-20 12:47:13 +0000\] \[38774\] \[INFO\] Starting gunicorn 21.2.0
\[2023-11-20 12:47:13 +0000\] \[38774\] \[INFO\] Listening at: http://0.0.0.0:8000 (38774)
\[2023-11-20 12:47:13 +0000\] \[38774\] \[INFO\] Using worker: sync
\[2023-11-20 12:47:13 +0000\] \[38775\] \[INFO\] Booting worker with pid: 38775
\[2023-11-20 12:47:13 +0000\] \[38776\] \[INFO\] Booting worker with pid: 38776
\[2023-11-20 12:47:13 +0000\] \[38777\] \[INFO\] Booting worker with pid: 38777
\[2023-11-20 12:47:13 +0000\] \[38778\] \[INFO\] Booting worker with pid: 38778


我的settings.py静态配置是:

STATIC_URL = '/static/'
    STATIC_ROOT = BASE_DIR / 'staticfiles' #representa la raiz del directorio
    STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),


我的基本目录是:

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


(也许额外的.parent是一个问题,但它加载媒体!)
在debug = True模式下
媒体配置为:

MEDIA_ROOT = BASE_DIR /'media' MEDIA_URL = '/media/'


如果我在静态文件夹上做pwd:/home/ubuntu/cht_web/奇怪的是,我的媒体文件除了请帮助之外没有配置:)
我尝试将配置文件更改为root,尝试将STATIC_ROOT更改为/home/ubuntu/cht_web/

am46iovg

am46iovg1#

如果DEBUG设置为True,则需要在项目级别将以下代码段添加到urls.py

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

字符串
参考:Django官方文档

相关问题