nginx 部署Django应用后没有静态文件

iaqfqrcu  于 2023-01-25  发布在  Nginx
关注(0)|答案(2)|浏览(186)

我已经部署了一个多容器Django应用程序到AWS EB的ECS running on 64bit Amazon Linux 2/3.2.3平台。

{
    "AWSEBDockerrunVersion": "2",
    "containerDefinitions": [
      {
        "essential": true,
        "image": "${AWS_ACOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com/${IMAGE_NAME}:${IMAGE_TAG}",
        "mountPoints": [
          {
            "containerPath": "/code/static",
            "sourceVolume": "web"
          }
        ],
        "name": "web",
        "hostname": "web",
        "memoryReservation": 1200,
        "portMappings": [
          {
            "containerPort": 8000,
            "hostPort": 8000
          }
        ]
      },
      {
        "name": "nginx-proxy",
        "image": "nginx",
        "essential": true,
        "memoryReservation": 128,
        "portMappings": [
          {
            "containerPort": 80,
            "hostPort": 80
          }
        ],
        "mountPoints": [
          {
            "sourceVolume": "nginx-proxy-conf",
            "containerPath": "/etc/nginx/nginx.conf"
          }
        ],
        "links": ["web"]
      }

    ],
    "volumes": [
      {
        "name": "web"
      },
      {
        "name": "nginx-proxy-conf",
        "host": {
          "sourcePath": "/var/app/current/nginx.conf"
      }
    }
    ]
}

这是nginx.conf

user                    nginx;
events {
    worker_connections        1024;
}
http {
    server {
        listen 80;
        location /static/ {
            alias /static/;
        }

        location / {
            proxy_pass http://web:8000;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
}

这是Dockerfile

FROM python:3.10

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN apt-get update
RUN apt-get upgrade -y

COPY ./requirements.txt requirements.txt
RUN apt-get install -y realmd
RUN pip install -r requirements.txt

WORKDIR /code

EXPOSE 8000

COPY . .

RUN chmod +x entrypoint.sh
ENTRYPOINT ["./entrypoint.sh"]

entrypoint.sh

#!/bin/bash
python manage.py migrate --noinput >/tmp/migrate 2> /tmp/migrate_err

python manage.py collectstatic --no-input

yum install realmd

gunicorn \
    --bind 0.0.0.0:8000 \
    pos.wsgi:application

因此,CodePipieline构建映像并部署到EB(使用t3.small EC2).部署后状态为“OK”,当我输入由EB生成的链接并在末尾添加/admin时,我可以看到管理面板,但没有样式。[![在此处输入图像描述][1]][1]。现在django应用程序连接到RDS数据库,当我尝试登录管理面板时,我得到504 Gateway Time-out502 Bad Gateway。我尝试过从nginx.conf添加或删除一些设置,但仍然无法'我看不到静态文件。当我在ssh到EC2示例之后执行到Docker容器时,我可以看到/static/目录。
我的settings.py中有这些行

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

如果还有什么要分享的,请告诉我。[1]:https://i.stack.imgur.com/cyA3z.png

2nc8po8w

2nc8po8w1#

您没有明确指定它,但如果您的static文件不在nginx映像上,这些文件将无法正确提供。
有两个选项可以修复它。

  • 在容器之间Mapstatic文件夹(类似nginx.conf
  • nginx.conf中删除/static/位置,将静态文件请求传递到代理的上游(django容器)。
axkjgtzd

axkjgtzd2#

我认为你没有收集Django默认的静态文件

设置:

- run command python manage.py collectstatic

 - urlpatterns = [
    path("", include("myapp.urls")),
    path('admin/', admin.site.urls),

]

urlpatterns+=static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns+=static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

相关问题