我已经部署了一个多容器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-out
或502 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
2条答案
按热度按时间2nc8po8w1#
您没有明确指定它,但如果您的
static
文件不在nginx
映像上,这些文件将无法正确提供。有两个选项可以修复它。
static
文件夹(类似nginx.conf
)nginx.conf
中删除/static/
位置,将静态文件请求传递到代理的上游(django
容器)。axkjgtzd2#
我认为你没有收集Django默认的静态文件
设置: