【发布时间】:2021-02-26 06:58:03
【问题描述】:
我正在尝试在无法访问的 EC2 实例上配置已部署的应用程序 应用程序在 ec2 公共 IP 上启动时。我检查了安全组并允许所有 到端口的入站流量只是为了查看我是否可以访问 django 的主页或管理页面。
假设我的 ec2 IP 地址是 34.245.202.112 我如何映射我的应用程序以便 nginx 服务
前端(nuxt)位于 34.245.202.112
后端(django)位于 34.245.202.112/admin
API(DRF) 位于 34.245.202.112/api
当我尝试这样做时,我从 nginx 得到的错误是
nginx | 2020-11-14T14:15:35.511973183Z 2020/11/14 14:15:35 [emerg] 1#1: host not found in upstream "nuxt:3000" in /etc/nginx/conf.d/autobets_nginx.conf:9
这是我的配置
docker-compose
版本:“3.4”
services:
db:
restart: always
image: postgres
volumes:
- pgdata:/var/lib/postgresql/data
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- "5432:5432"
expose:
- 5432
networks:
- random_name
django:
container_name: django
build:
context: ./backend
env_file: .env
environment:
- DEBUG=True
command: >
sh -c "./wait-for-it.sh db:5432 &&
./autobets/manage.py collectstatic --no-input &&
./autobets/manage.py makemigrations &&
./autobets/manage.py migrate --no-input &&
./autobets/manage.py runserver_plus 0.0.0.0:8001
"
- "8001"
volumes:
- ./backend:/app
depends_on:
- db
restart: on-failure
nginx:
image: nginx
container_name: nginx
ports:
- "80:80"
restart: always
depends_on:
- nuxt
- django
volumes:
- ./nginx/conf:/etc/nginx/conf.d
- ./nginx/uwsgi_params:/etc/nginx/uwsgi_params
- ./backend/static:/static
networks:
- random_name
nuxt:
build:
context: ./frontend
environment:
- API_URI=http://django:8001/api
command: sh -c "npm install && npm run dev"
volumes:
- ./frontend:/app
ports:
- "3000:3000"
depends_on:
- django
networks:
- random_name
volumes:
pgdata:
networks:
random_name:
nginx.conf
# the upstream component nginx needs to connect to
upstream django {
ip_hash;
server django:8001;
}
upstream nuxt {
ip_hash;
server nuxt:3000;
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name 34.245.202.112; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
location /static/ {
alias /static/;
}
# Finally, send all non-media requests to the Django server.
location / {
proxy_pass django;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $host;
}
}
【问题讨论】:
-
即使你修复了那个错误,你的 nginx 配置也不会工作。
proxy_pass和uwsgi_pass都是 内容处理程序。一个位置内不能有多个内容处理程序。 -
@IvanShatsky 我现在已经解决了这个问题,但仍然收到同样的错误。你知道我可以参考的好的配置模板吗?
标签: django docker nginx amazon-ec2 docker-compose