目前,我尝试将 Django 项目部署到 AWS EB,但我面临很多问题。我可以将项目 dockerize 并将其部署在 AWS 弹性 beanstalk 上。但是当我尝试访问该站点时,我总是看到:502 Bad Gateway。在当地,项目运行顺利。我不是很喜欢 nginx,也不知道如何解决这个问题。
这是我的项目结构:

这是我的 Dockerfile:
# Creating image based on official python3 image
FROM python:3
MAINTAINER Jaron Bardenhagen
# Sets dumping log messages directly to stream instead of buffering
ENV PYTHONUNBUFFERED 1
# Creating and putting configurations
RUN mkdir /config
ADD config/app /config/
# Installing all python dependencies
RUN pip install -r /config/requirements.txt
# Open port 8000 to outside world
EXPOSE 8000
# When container starts, this script will be executed.
# Note that it is NOT executed during building
CMD ["sh", "/config/on-container-start.sh"]
# Creating and putting application inside container
# and setting it to working directory (meaning it is going to be default)
RUN mkdir /app
WORKDIR /app
ADD app /app/
这是我的 docker-compose 文件:
# File structure version
version: '3'
services:
db:
image: postgres
environment:
POSTGRES_DB_PORT: "5432"
POSTGRES_DB_HOST: "*******"
POSTGRES_PASSWORD: "*******"
POSTGRES_USER: Jaron
POSTGRES_DB: ebdb
# Build from remote dockerfile
# Connect local app folder with image folder, so changes will be pushed to image instantly
# Open port 8000
app:
build:
context: .
dockerfile: config/app/Dockerfile
hostname: app
volumes:
- ./app:/app
expose:
- "8000"
depends_on:
- db
# Web server based on official nginx image
# Connect external 8000 (which you can access from browser)
# with internal port 8000(which will be linked to app port 8000 in configs)
# Connect local nginx configuration with image configuration
nginx:
image: nginx
hostname: nginx
ports:
- "8000:8000"
volumes:
- ./config/nginx:/etc/nginx/conf.d
depends_on:
- app
这是 Dockerrun.aws 文件:
{
"AWSEBDockerrunVersion": "1",
"Image": {
"Name": "******/******:latest",
"Update": "true"
},
"Ports": [
{
"ContainerPort": "8000"
}
]
}
On-container-start.sh 文件:
# Create migrations based on django models
python manage.py makemigrations
# Migrate created migrations to database
python manage.py migrate
# Start gunicorn server at port 8000 and keep an eye for app code changes
# If changes occur, kill worker and start a new one
gunicorn --reload project.wsgi:application -b 0.0.0.0:8000
这是 nginx 设置的文件(app.conf):
# define group app
upstream app {
# balancing by ip
ip_hash;
# define server app
server app:8000;
}
# portal
server {
# all other requests proxies to app
location / {
proxy_pass http://app/;
}
# only respond to port 8000
listen 8000;
# domain localhost
server_name localhost;
}
我真的很感激任何帮助!